<template>
  <div>
    <!-- 数据表格 -->
    <ele-pro-table
      ref="table"
      :columns="columns"
      :datasource="datasource"
      height="calc(100vh - 265px)"
      full-height="calc(100vh - 116px)"
      tool-class="ele-toolbar-form"
      cache-key="systemOrgUserTable"
    >
      <!-- 表头工具栏 -->
      <template v-slot:toolbar>
        <gro-user-search @search="reload">
          <el-button
            v-if="data.userFrom == 0 && data.ifBiz == 1"
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="synchronous()"
          >
            同步原始门店
          </el-button>
          <el-button
            v-else
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openAdd()"
          >
            添加
          </el-button>
        </gro-user-search>
      </template>
      <!-- 角色列 -->
      <template v-slot:roleName="{ row }">
        <el-tag type="primary" size="mini">
          {{ row.roleName }}
        </el-tag>
      </template>
      <!-- 状态列 -->
      <template v-slot:status="{ row }">
        <el-switch
          :active-value="0"
          :inactive-value="1"
          v-model="row.status"
          @change="editStatus(row)"
        />
      </template>
      <!-- 操作列 -->
      <template v-slot:action="{ row }">
        <el-popconfirm
          class="ele-action"
          title="确定要删除此用户吗？"
          @confirm="remove(row)"
        >
          <template v-slot:reference>
            <el-link type="danger" :underline="false" icon="el-icon-delete">
              删除
            </el-link>
          </template>
        </el-popconfirm>
      </template>
    </ele-pro-table>
    <!-- 编辑弹窗 -->
    <gro-user-add
      :visible.sync="showAdd"
      :user-from="data.userFrom"
      :groups-id="groupsId"
      :activity-id="data.activityId"
      :organization-list="organizationList"
      :team-list="teamList"
      @done="reload"
    />
  </div>
</template>

<script>
  import GroUserSearch from './gro-user-search.vue';
  import GroUserAdd from './gro-user-add.vue';
  import {
    pageGroupsUser,
    removeGroupsUser,
    synchronousGroupsUser,
    updateGroupsUserStatus
  } from '@/api/sylive/groups-user';

  export default {
    components: { GroUserSearch, GroUserAdd },
    props: {
      data: Object,
      // 分组id
      groupsId: Number,
      // 全部分组
      groupsList: Array,
      // 全部团队
      teamList: Array,
      // 全部机构
      organizationList: Array
    },
    data() {
      return {
        // 表格列配置
        columns: [
          {
            prop: 'userId',
            label: 'ID',
            minWidth: 45,
            align: 'center',
            showOverflowTooltip: true
          },
          {
            prop: 'uname',
            label: '姓名',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'nickname',
            label: '微信昵称',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'mobile',
            label: '手机号',
            showOverflowTooltip: true,
            minWidth: 110
          },
          {
            prop: 'roleName',
            label: '角色',
            showOverflowTooltip: true,
            minWidth: 70,
            slot: 'roleName'
          },
          {
            prop: 'createTime',
            label: '创建时间',
            showOverflowTooltip: true,
            minWidth: 120,
            formatter: (_row, _column, cellValue) => {
              return this.$util.toDateString(cellValue);
            }
          },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            width: 110,
            resizable: false,
            slot: 'status'
          },
          {
            columnKey: 'action',
            label: '操作',
            width: 120,
            align: 'center',
            resizable: false,
            slot: 'action',
            showOverflowTooltip: true
          }
        ],
        // 当前编辑数据
        current: null,
        // 是否显示添加弹窗
        showAdd: false,
        // 是否显示编辑弹窗
        showEdit: false
      };
    },
    methods: {
      /* 表格数据源 */
      datasource({ page, limit, where, order }) {
        return pageGroupsUser({
          ...where,
          ...order,
          page,
          limit,
          groupsId: this.groupsId,
          activityId: this.data.activityId
        });
      },
      /* 同步原始门店 */
      synchronous() {
        if (!this.data.originalBizId) {
          this.$message.error('分组未选择原始门店');
          return;
        }
        const loading = this.$loading({ lock: true });
        synchronousGroupsUser(this.groupsId)
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            this.$message.error(e.message);
          });
      },
      /* 刷新表格 */
      reload(where) {
        this.$refs.table.reload({ page: 1, where: where });
      },
      /* 显示添加 */
      openAdd() {
        this.showAdd = true;
      },
      /* 删除 */
      remove(row) {
        const loading = this.$loading({ lock: true });
        removeGroupsUser(row.groupsUserId)
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
            this.reload();
          })
          .catch((e) => {
            loading.close();
            this.$message.error(e.message);
          });
      },
      /* 更改状态 */
      editStatus(row) {
        const loading = this.$loading({ lock: true });
        updateGroupsUserStatus(row.groupsUserId, row.status)
          .then((msg) => {
            loading.close();
            this.$message.success(msg);
          })
          .catch((e) => {
            loading.close();
            row.status = !row.status ? 1 : 0;
            this.$message.error(e.message);
          });
      }
    },
    watch: {
      // 监听机构id变化
      groupsId() {
        this.reload();
      }
    }
  };
</script>
