<template>
  <div>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="店铺名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户名称" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="备注名" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="queryParams.role" placeholder="请选择角色" clearable @change="handleQuery">
          <el-option
            v-for="dict in dict.type.store_staff_role"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="enabled">
        <el-radio-group v-model="queryParams.enabled" @change="handleQuery">
          <el-radio :label="null">全部</el-radio>
          <el-radio :label="true">已启用</el-radio>
          <el-radio :label="false">已禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <slot name="table-operator"></slot>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeStaffList" v-on="$listeners" :default-sort="defaultSort"  @sort-change="onSortChange">
      <el-table-column type="selection" width="55" align="center" />
      <template v-for="column of showColumns">
        <el-table-column
          :key="column.key"
          :label="column.label"
          :prop="column.key"
          :align="column.align"
          :min-width="column.minWidth"
          :sort-orders="orderSorts"
          :sortable="column.sortable"
          :show-overflow-tooltip="column.overflow"
          :width="column.width"
        >
          <template slot-scope="d">
            <template v-if="column.key === 'employId'">
              {{d.row[column.key]}}
            </template>
            <template v-else-if="column.key === 'role'">
              <dict-tag :options="dict.type.store_staff_role" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'permissions'">
              <dict-tag :options="dict.type.store_staff_permissions" :value="d.row[column.key]"/>
            </template>
            <template v-else-if="column.key === 'enabled'">
              <boolean-tag :value="d.row.enabled"/>
            </template>
            <template v-else-if="column.key === 'point'">
              {{d.row.point | money | defaultValue}} %
            </template>
            <template v-else-if="column.key === 'userId'">
              <user-link v-if="d.row.userId != null" :id="d.row.userId" :name="d.row.userName"/>
              <template v-else>
                <el-popover
                  placement="top"
                  width="180"
                  trigger="hover">
                  <div class="qr-code-box">
                    <qr-code :text="qrCodeText(d.row)" :width="150" :height="150" />
                    <p style="text-align: center">扫描二维码绑定用户</p>
                  </div>
                  <el-button slot="reference" type="text" icon="el-icon-picture">绑定二维码</el-button>
                </el-popover>
              </template>
            </template>
            <template v-else-if="column.key === 'storeName'">
              <store-link :id="d.row.storeId" :name="d.row.storeName"/>
            </template>
            <template v-else>
              {{d.row[column.key] | defaultValue}}
            </template>
          </template>
        </el-table-column>
      </template>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <slot name="row-operator" :row="scope.row"/>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import UserLink from '@/components/Business/SmUser/UserLink.vue'
import BooleanTag from '@/components/BooleanTag/index.vue'
import StoreLink from '@/components/Business/Store/StoreLink.vue'
import UserInput from '@/components/Business/SmUser/UserInput.vue'
import StoreInput from '@/components/Business/Store/StoreInput.vue'
import { $showColumns, $view } from '@/utils/mixins'
import { listStoreStaff } from '@/api/ss/storeStaff'
import QrCode from '@/components/QrCode/index.vue'
import { getStaffUrl, getWxIndexUrl } from '@/utils/wx'


// 默认排序字段
const defaultSort = {
  prop: "createTime",
  order: "descending"
}

export default {
  name: "StoreStaffTable",
  components: { QrCode, StoreLink, UserLink, UserInput, StoreInput, BooleanTag },
  mixins: [$showColumns, $view],
  dicts: ['store_staff_permissions', 'store_staff_role'],
  props: {
    query: {
      type: Object,
      default: () => {
        return {}
      }
    },
    listApi: {
      type: Function,
      default: listStoreStaff
    }
  },
  data() {
    return {
      // 字段列表
      columns: [
        {key: 'employId', visible: false, label: 'ID', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'storeName', visible: true, label: '店铺', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'userId', visible: true, label: '用户', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'remark', visible: true, label: '备注名', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'role', visible: true, label: '角色', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'point', visible: true, label: '分成比例', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'enabled', visible: true, label: '是否启用', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'createBy', visible: true, label: '创建人', minWidth: null, sortable: true, overflow: false, align: 'center', width: null},
        {key: 'createTime', visible: true, label: '创建时间', minWidth: null, sortable: true, overflow: false, align: 'center', width: "100"},
        {key: 'permissions', visible: true, label: '权限', minWidth: null, sortable: false, overflow: false, align: 'center', width: "300"},
      ],
      // 排序方式
      orderSorts: ['ascending', 'descending', null],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 店铺员工表格数据
      storeStaffList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      defaultSort,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderByColumn: defaultSort.prop,
        isAsc: defaultSort.order,
        employId: null,
        storeId: null,
        userId: null,
        remark: null,
        role: null,
        point: null,
        enabled: null,
        createId: null
      },
    };
  },
  computed: {
    // 二维码文本
    qrCodeText() {
      return (row) => {
        return getStaffUrl({ i: row.employId});
      }
    },
  },
  created() {
    this.queryParams = {
      ...this.queryParams,
      ...this.query
    }
    this.getList();
  },
  methods: {
    /** 当排序按钮被点击时触发 **/
    onSortChange(column) {
      if (column.order == null) {
        this.queryParams.orderByColumn = defaultSort.prop;
        this.queryParams.isAsc = defaultSort.order;
      } else {
        this.queryParams.orderByColumn = column.prop;
        this.queryParams.isAsc = column.order;
      }
      this.getList();
    },
    /** 查询店铺员工列表 */
    getList() {
      this.loading = true;
      this.listApi(this.queryParams).then(response => {
        this.storeStaffList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.employId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
  }
};
</script>
