<!--
 * @Author: zwq
 * @Date: 2020-12-29 15:41:11
 * @LastEditors: fzq
 * @LastEditTime: 2022-03-19 10:20:40
 * @Description:
-->
<template>
  <div class="app-container">
    <head-form
      :placeholder-name="placeholderName"
      :key-name="keyName"
      @getDataList="getList"
      @add="addNew"
    />
    <base-table
      :page="listQuery.current"
      :limit="listQuery.size"
      :table-config="tableProps"
      :table-data="list"
      :is-loading="listLoading"
    >
      <method-btn
        slot="handleBtn"
        :width="trueWidth"
        :method-list="tableBtn"
        @clickBtn="handleClick"
      />
    </base-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="getList()"
    />
    <staff-add v-if="addOrUpdateVisible" ref="addOrUpdate" :role-list="roleList" @refreshDataList="getList" />
  </div>
</template>

<script>
import i18n from '@/lang'
import { staffList, staffDelete, getRoleList } from '@/api/basicData/GroupModule/staff'
import staffAdd from './components/staff-add.vue'
import HeadForm from '@/components/basicData/HeadForm'
import BaseTable from '@/components/BaseTable'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import MethodBtn from '@/components/BaseTable/subcomponents/MethodBtn'
import basicData from '@/filters/basicData'
// import DictFilter from '@/components/BaseTable/subcomponents/DataDictFilter'
// import { workerRoleList } from '@/api/dict'
import { timeFormatter } from '@/filters'
/**
 * 表格表头配置项 TypeScript接口注释
 * tableConfig<ConfigItem> = []
 *
 * Interface ConfigItem = {
 *  prop: string,
 *  label: string,
 *  width: string,
 *  align: string,
 *  subcomponent: function,
 *  filter: function
 * }
 *
 *
 */

const tableBtn = [
  {
    type: 'edit',
    btnName: 'btn.edit'
  },
  {
    type: 'delete',
    btnName: 'btn.delete'
  }
]
const tableProps = [
  {
    prop: 'createTime',
    label: i18n.t('module.basicData.factory.createTime'),
    filter: timeFormatter,
    align: 'center'
  },
  {
    prop: 'name',
    label: i18n.t('module.basicData.staff.Name'),
    align: 'center'
  },
  {
    prop: 'code',
    label: i18n.t('module.basicData.staff.EmployeeID'),
    align: 'center'
  },
  {
    prop: 'roleName',
    label: i18n.t('module.basicData.staff.role'),
    align: 'center'
    // subcomponent: DictFilter,
    // filter: workerRoleList
  },
  {
    prop: 'sex',
    label: i18n.t('module.basicData.staff.Gender'),
    filter: basicData('sex'),
    align: 'center'
  },
  {
    prop: 'entryTime',
    label: i18n.t('module.basicData.staff.EntryTime'),
    filter: timeFormatter,
    align: 'center'
  },
  {
    prop: 'telephone',
    label: i18n.t('module.basicData.staff.Telephone'),
    align: 'center'
  },
  {
    prop: 'email',
    label: i18n.t('module.basicData.staff.Email'),
    align: 'center'
  },
  {
    prop: 'wechatCode',
    label: i18n.t('module.basicData.staff.Wechat'),
    align: 'center'
  },
  {
    prop: 'workshop',
    label: i18n.t('module.basicData.staff.Workshop'),
    align: 'center'
  },
  {
    prop: 'onDuty',
    label: i18n.t('module.basicData.staff.onDuty'),
    filter: basicData('onDuty'),
    align: 'center'
  },
  {
    prop: 'description',
    label: i18n.t('module.basicData.visual.Remarks'),
    align: 'center'
  }
]

export default {
  name: 'Staff',
  components: { Pagination, BaseTable, MethodBtn, HeadForm, staffAdd },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      keyName: i18n.t('module.basicData.visual.keyword'),
      placeholderName: this.$t('module.basicData.staff.Name'),
      addOrUpdateVisible: false,
      tableBtn,
      trueWidth: 200,
      tableProps,
      list: [],
      total: 0,
      listLoading: true,
      roleList: [],
      listQuery: {
        current: 1,
        size: 10
      }
    }
  },
  created() {
    this.getRole()
  },
  methods: {
    handleClick(raw) {
      console.log(raw)
      if (raw.type === 'delete') {
        this.$confirm(`${this.$t('module.basicData.visual.TipsBefore')}[${raw.data.name}]?`, this.$t('module.basicData.visual.Tips'), {
          confirmButtonText: this.$t('module.basicData.visual.confirmButtonText'),
          cancelButtonText: this.$t('module.basicData.visual.cancelButtonText'),
          type: 'warning'
        }).then(() => {
          staffDelete(raw.data.id).then(response => {
            this.$message({
              message: this.$t('module.basicData.visual.success'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getList()
              }
            })
          })
        }).catch(() => {})
      } else {
        this.addNew(raw.data.id)
      }
    },
    getList(key) {
      this.listLoading = true
      this.listQuery.key = key
      staffList(this.listQuery).then(response => {
        if (response.data.records) {
          this.list = response.data.records
          this.list.forEach(item => {
            if (item.major) { item.majorArr = item.major.toString() }
          })
        } else {
          this.list.splice(0, this.list.length)
        }
        this.total = response.data.total
        this.listLoading = false
      })
    },
    async getRole() {
      const res = await getRoleList()
      this.roleList = res.data.records
      this.getList()
    },
    // 新增 / 修改
    addNew(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
