<!--
  Description: 人员管理页面
  Author: 李亚光
  Date: 2023-04-23
 -->
<script lang="ts" setup name="person">
import { reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { dict } from './person-interface'
import { delPerson, getPersonListPage } from '@/api/person'
import { getDeptTreeList } from '@/api/system/dept'
import { toTreeList } from '@/utils/structure'
import { getDictByCode } from '@/api/system/dict'
const $router = useRouter()
const { proxy } = getCurrentInstance() as any
const listQuery = reactive({
  name: '',
  startDate: '',
  status: '',
  deptId: '',
  endDate: '',
  workerType: '1',
  // logType: '业务日志',
  offset: 1,
  limit: 20,
})
const columns = ref([
  {
    text: '姓名',
    value: 'workerName',
    align: 'center',
    // width: 170,
  },
  {
    text: '性别',
    value: 'genderName',
    align: 'center',
    // width: 100,
  },
  {
    text: '联系方式',
    value: 'phoneNumber',
    align: 'center',
  },
  {
    text: '所属部门',
    value: 'deptName',
    align: 'center',
  },
  {
    text: '注册时间',
    value: 'createTime',
    align: 'center',
  },
  {
    text: '当前状态',
    value: 'statusName',
    align: 'center',
  },
])
const list = ref([])
const total = ref(20)
const listLoading = ref(true)
// 获取列表
const fetchData = () => {
  listLoading.value = true
  getPersonListPage(listQuery).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}

fetchData()
// 查询数据
const search = () => {
  fetchData()
}
// 新增人员
const add = (row: any) => {
  $router.push({ path: '/person/personedit', query: { type: 'add' } })
}
// 人脸注册
const register = (row: any) => {
  // personRef.value.initDialog('update', {})
  $router.push({ path: '/person/personedit', query: { type: 'update', row: JSON.stringify(row) } })
}
// 编辑人员
const edit = (row: any) => {
  $router.push({ path: '/person/personedit', query: { type: 'update', row: JSON.stringify(row) } })
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData()
}
// 表格被选中的行
const selectList = ref<any[]>([])
// 表格多选
const multiSelect = (row: any[]) => {
  selectList.value = row
}
// 删除
const del = () => {
  if (selectList.value.length) {
    ElMessageBox.confirm('是否删除所选的数据?', '提示', {
      confirmButtonText: '是',
      cancelButtonText: '否',
      type: 'info',
    }).then((res) => {
      // 点击确认
      if (selectList.value.every(item => item.status === '0')) {
        const ids = selectList.value.map(item => item.id)
        delPerson(ids).then((res) => {
          if (res.code === 200) {
            selectList.value = []
            ElMessage.success('删除成功')
            fetchData()
          }
        })
      }
      else {
        ElMessage.warning('所选人员正在作业中，请先解绑')
      }
    }).catch(() => {
      // 点击取消
      selectList.value = []
    })
  }
  else {
    ElMessage.warning('请先选择需要删除的数据')
  }
}
const deptTreeList = ref([])
// 加载组织机构树形下拉
const fetchDeptTree = () => {
  getDeptTreeList().then((res) => {
    if (res.data) { // 将列表转树结构
      deptTreeList.value = toTreeList(res.data, '0', true)
    }
  })
}
// 人员状态列表
const workerStatusList = ref<dict[]>([])
const fetchStatusList = () => {
  getDictByCode('workerStatus').then((response) => {
    workerStatusList.value = response.data
  })
}
fetchStatusList()
fetchDeptTree()
</script>

<template>
  <div class="list-log">
    <!-- 布局 -->
    <app-container>
      <!-- <edit-person ref="personRef" /> -->
      <!-- 筛选条件 -->
      <search-area @search="search">
        <search-item>
          <el-input v-model.trim="listQuery.name" placeholder="姓名" clearable />
        </search-item>
        <search-item>
          <dept-select v-model="listQuery.deptId" :data="deptTreeList" placeholder="所属部门" style="width: 100%;" />
        </search-item>
        <search-item>
          <el-select v-model="listQuery.status" clearable placeholder="当前状态">
            <el-option v-for="item in workerStatusList" :key="item.id" :label="item.name" :value="item.value" />
          </el-select>
        </search-item>
        <search-item>
          <el-date-picker
            v-model="listQuery.startDate" type="datetime" value-format="YYYY/MM/DD HH:mm:ss"
            placeholder="注册开始时间"
          />
        </search-item>
        <search-item>
          <el-date-picker
            v-model="listQuery.endDate" type="datetime" value-format="YYYY/MM/DD HH:mm:ss"
            placeholder="注册结束时间"
          />
        </search-item>
        <template #btns>
          <el-button v-if="proxy.hasPerm('/person/add')" class="search-btn" type="primary" @click="add">
            新增
          </el-button>
          <el-button v-if="proxy.hasPerm('/person/del')" class="search-btn" type="info" @click="del">
            删除
          </el-button>
        </template>
      </search-area>
      <table-container>
        <!-- 查询结果Table显示 -->
        <normal-table
          :data="list" :total="total" :columns="columns" :query="listQuery" :is-showmulti-select="true"
          :is-multi="true" :list-loading="listLoading" @change="changePage" @multi-select="multiSelect"
        >
          <template #columns>
            <el-table-column v-if="proxy.hasPerm('/person/register')" label="人脸注册" width="90" align="center">
              <template #default="scope">
                <el-button
                  link type="primary" :disabled="Boolean(scope.row.workerAvatar)" size="small"
                  @click="register(scope.row)"
                >
                  注册
                </el-button>
              </template>
            </el-table-column>
            <el-table-column v-if="proxy.hasPerm('/person/update')" label="编辑" width="70" align="center">
              <template #default="scope">
                <el-button link type="primary" size="small" @click="edit(scope.row)">
                  编辑
                </el-button>
              </template>
            </el-table-column>
          </template>
        </normal-table>
      </table-container>
    </app-container>
  </div>
</template>

