<!-- 选择计量人员弹窗 -->
<script setup lang="ts" name="SelectStaffsDialog">
import { computed, onMounted, reactive, ref, watch } from 'vue'
import type { IEmployees } from './dialog'
import { getStaffList } from '@/api/measure/person'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getDictByCode } from '@/api/system/dict'
import type { dictType } from '@/global'
const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  data: {
    type: Array,
    default: () => [],
  },
  // 默认查询组织
  deptId: {
    type: String,
    default: '',
  },
  // 过滤组织
  deptName: {
    type: String,
    default: '',
  },
  // 是否显示部门选择下拉菜单
  showDeptSelect: {
    type: Boolean,
    default: true,
  },
  // 是否多选
  multi: {
    type: Boolean,
    default: true,
  },
})
const emits = defineEmits(['update:visible', 'change'])
const table = ref() // 表格ref

// 表格表头
const columns = ref<TableColumn[]>([
  { text: '姓名', value: 'name', align: 'center' },
  { text: '人员编号', value: 'staffNo', align: 'center' },
  { text: '所在部门', value: 'deptId', align: 'center' },
  { text: '行政职务', value: 'administrationJob', align: 'center' },
])

// 表格数据
const list = ref<IEmployees[]>([])

const loading = ref(false)
const total = ref(0)
// 查询参数
const listQuery = reactive({
  name: '', // 姓名
  deptId: '', // 部门
  administrationJob: '', // 行政职务
  major: '', // 专业
  staffNo: '', // 人员编号
  limit: 5,
  offset: 1,
})
// 默认给deptId赋值
listQuery.deptId = props.deptId
const checkedList = ref([])
const jobList = ref<dictType[]>([]) // 职务列表
// 多选的数据
const multipleTable = ref([])
const singleChecked = ref('') // 单选选中id
const closeDialog = () => {
  emits('update:visible', false)
}
const visibleDialog = computed({
  get() {
    return props.visible
  },
  set() {
    closeDialog()
  },
})

watch(() => props.visible, (val) => {
  // 清除上次选中
  if (!val) {
    table.value.clearMulti()
  }
})
function getDict() {
  // 获取样品属性
  getDictByCode('administrationJob').then((response) => {
    jobList.value = response.data
  })
}
// 点击确定，保存选择的成员配置
const saveDialog = () => {
  // 多选
  if (props.multi) {
    const checkedList = multipleTable.value.map((item: IEmployees) => {
      return {
        name: item.name,
        id: item.id,
      }
    })
    emits('change', checkedList)
  }
  else {
    const checkValue = list.value.find(item => item.id == singleChecked.value)
    emits('change', checkValue)
  }
}

const select = ref('')
// 获取列表数据
const fetchData = (isNowPage = false) => {
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.offset = 1
  }
  listQuery.deptId = props.deptId
  loading.value = true
  // list.value = []
  getStaffList(listQuery).then((res) => {
    list.value = res.data.records.filter((item: any) => {
      if (props.deptName) {
        return item.deptId === props.deptName
      }
      else {
        return item
      }
    })
    console.log(props.deptName)
    console.log(props.deptId)
    console.log(list.value)
    total.value = parseInt(res.data.total)
    loading.value = false
  })
}

// 多选
const multiSelect = (val: any) => {
  multipleTable.value = val
}

// 查询数据
const search = () => {
  fetchData(true)
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData(true)
}

onMounted(() => {
  // 获取列表数据
  fetchData(true)
  getDict()
})

defineExpose({ fetchData })
</script>

<template>
  <div class="select-emplotees-dialog">
    <el-dialog v-model="visibleDialog" title="选择成员" width="800px" append-to-body custom-class="chengyuan-dialog promoter_person">
      <!-- 右侧表格 -->
      <div ref="tableContainer" class="table-area">
        <!-- 筛选条件 -->
        <search-area @search="search">
          <search-item>
            <el-input v-model.trim="listQuery.name" class="short-input" placeholder="姓名" clearable />
          </search-item>
          <search-item>
            <dept-select v-if="!props.deptId" v-model="listQuery.deptId" class="short-input" placeholder="所在部门" :dept-show="true" />
          </search-item>
          <search-item>
            <el-select v-model="listQuery.administrationJob" class="short-input" placeholder="行政职务" clearable>
              <el-option v-for="item in jobList" :key="item.id" :label="item.name" :value="item.value" />
            </el-select>
          </search-item>
        </search-area>
        <!-- 表头标题 -->
        <table-container :title-show="false" style="margin-top: 0;padding-top: 0;">
          <!-- 查询结果Table显示 -->
          <normal-table
            ref="table"
            :data="list"
            :total="total"
            :columns="columns"
            :query="listQuery"
            :list-loading="loading"
            :is-showmulti-select="multi"
            @change="changePage"
            @multi-select="multiSelect"
          >
            <!-- 序号 -->
            <template #preColumns>
              <el-table-column v-if="!props.multi" label="" width="40" align="center">
                <template #default="scope">
                  <el-radio v-model="singleChecked" :label="scope.row.id" class="radio" />
                </template>
              </el-table-column>
              <el-table-column label="#" width="55" align="center">
                <template #default="scope">
                  {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
                </template>
              </el-table-column>
            </template>
          </normal-table>
        </table-container>
      </div>
      <!-- </div> -->
      <template #footer>
        <el-button @click="closeDialog">
          取 消
        </el-button>
        <el-button type="primary" @click="saveDialog">
          确 定
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style lang="scss">
@import "@/css/dialog.css";

.chengyuan-dialog {
  .el-table .cell {
    white-space: nowrap;
  }

  .el-dialog__body {
    display: flex;
    overflow: auto;
  }
}
</style>

<style lang="scss" scoped>
.chengyuan-dialog {
  width: 100%;

  .left-container {
    white-space: nowrap;
    height: 100%;
  }

  .table-area {
    flex: 1;
    overflow: auto;

    :deep(.el-radio__label) {
      display: none;
    }

    .pagination {
      margin-top: 10px;
      width: 100%;
      display: flex;
      flex-direction: column;
      align-items: center;
    }
  }
}
</style>
