<template>
  <div class="store-employee-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-button icon="el-icon-s-tools" type="primary" @click="showSetTbale">
          {{ __('部门设置') }}
        </el-button>
        <el-button
          icon="el-icon-s-tools"
          type="primary"
          @click="JurisdictionTable"
        >
          {{ __('角色权限设置') }}
        </el-button>
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
        <el-button icon="el-icon-refresh" type="primary" @click="queryData">
          {{ __('刷新') }}
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('用户编号')"
        prop="user_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('用户名')"
        prop="user_account"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('用户昵称')"
        prop="user_nickname"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('权限组')"
        prop="rights_group_id"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <el-row v-if="row.rights_group_names" :gutter="20">
            <el-button type="success" @click="showRightGroupTable(row)">
              {{ row.rights_group_names }}
            </el-button>
          </el-row>
          <el-row v-else :gutter="20">
            <el-button type="danger" @click="showRightGroupTable(row)">
              {{ '添加权限组' }}
            </el-button>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('是否管理员')"
        prop="employee_is_admin"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.employee_is_admin"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('是否客服')"
        prop="employee_is_kefu"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.employee_is_kefu"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
    <rightsGroupTable ref="rightsGroupTable" @fetch-data="fetchData" />
    <SetTable ref="SetTable" />
    <JurisdictionTable ref="JurisdictionTable" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doDelete, doEdit, getList } from '@/api/store/employee'
  import Edit from './components/StoreEmployeeEdit'
  import SetTable from './components/SetTable'
  import JurisdictionTable from './components/JurisdictionTable'
  import RightsGroupTable from './components/StoreEmployeeRightsGroupTable'

  export default {
    name: 'StoreEmployee',
    components: { Edit, SetTable, JurisdictionTable, RightsGroupTable },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      showRightGroupTable(row) {
        if (row.user_id) {
          this.$refs['rightsGroupTable'].showEdit(row)
        }
      },
      handleEdit(row) {
        if (row.user_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      showSetTbale() {
        this.$refs['SetTable'].showTable()
      },
      JurisdictionTable() {
        this.$refs['JurisdictionTable'].showTable()
      },
      handleDelete(row) {
        if (row.user_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDelete({ user_id: row.user_id })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            await this.fetchData()
          })
        }
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      async sChange(row) {
        const param = {
          employee_id: row.employee_id,
          employee_is_admin: row.employee_is_admin,
          employee_is_kefu: row.employee_is_kefu,
        }
        const { msg, status } = await doEdit(param)
        this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
