<!--
  Description: 选择人员穿戴设备弹窗
  Author: 李亚光
  Date: 2023-04-28
 -->
<script lang="ts" setup name="selectPerson">
import { ElMessage } from 'element-plus'
import { getDeviceListPage } from '@/api/device'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getDictByCode } from '@/api/system/dict'
import type { DictDetail } from '@/views/system/dict/dict-interface'
const props = defineProps({
  //  多选
  multiple: {
    type: Boolean,
    default: false,
  },
})
const emits = defineEmits(['add'])
const dialogFormVisible = ref(false)
// 查询条件
const listQuery = ref({
  deviceCode: '',
  deviceType: '',
  projectId: '',
  status: '0',
  offset: 1,
  limit: 5,
})
const loadingTable = ref<boolean>(false)
const list = ref([]) // 表格数据
const total = ref(0)
const deviceType = ref('') // 设备类型
// 表头
const columns = ref<TableColumn[]>([
  { text: '设备编号', value: 'deviceCode' },
  {
    text: '厂家',
    value: 'manufacturer',
  },
  { text: '是否供电', value: 'isPowerSupplyName' },
  { text: '批次', value: 'deviceBatch' },
  { text: '注册时间', value: 'createTime' },
])
// 获取设备数据列表
const fetchData = () => {
  getDeviceListPage(listQuery.value).then((res) => {
    if (res.code === 200) {
      list.value = res.data.rows
      total.value = res.data.rows.length
    }
  })
}
// 获取设备类型
const deviceTypeList = ref<DictDetail[]>([])
const fetchDeviceType = () => {
  getDictByCode('deviceType').then((response) => {
    deviceTypeList.value = response.data
  })
}
fetchDeviceType()
// 搜索
const search = () => {
  fetchData()
}
// 表格被选中的行
const selectList = ref<any[]>([])
// 表格多选
const multiSelect = (row: any[]) => {
  selectList.value = row
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size?: number; page?: number }) => {
  if (val && val.size) {
    listQuery.value.limit = val.size
  }
  if (val && val.page) {
    listQuery.value.offset = val.page
  }
  search()
}
// 重置
const reset = () => {
  listQuery.value.deviceCode = ''
  listQuery.value.offset = 1
  listQuery.value.limit = 5
  search()
  selectList.value = []
}

// 点击保存
const submitForm = () => {
  if (props.multiple) {
    // 多选
    if (selectList.value.length > 0) {
      emits('add', selectList.value)
      dialogFormVisible.value = false
      reset()
    }
    else {
      ElMessage.warning('请先选择设备')
    }
  }
  else {
    // 单选
    if (selectList.value.length > 0) {
      const device = selectList.value[0]
      emits('add', Object.assign(device, { Type: deviceType.value }))
      dialogFormVisible.value = false
      reset()
    }
    else {
      ElMessage.warning('请先选择设备')
    }
  }
}
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  reset()
}
const tableRef = ref()
// 初始化
const initDialog = (type: string, id: string) => {
  // 清空选中状态
  selectList.value = []
  tableRef.value?.clearRadio()
  tableRef.value?.clearMulti()
  listQuery.value.projectId = id
  // 设备类型
  deviceType.value = type
  let typeName = ''
  dialogFormVisible.value = true
  if (type === 'hatCode') {
    typeName = '安全帽'
    // 安全帽
  }
  else if (type === 'vastCode') {
    // 安全背心
    typeName = '背心'
  }
  else if (type === 'braceletCode') {
    // 安全手环
    typeName = '手环'
  }
  else if (type === 'SafetyTree') {
    // 安全手环
    typeName = '安全树'
  }
  // 设备类型参数
  const devicetype = deviceTypeList.value.filter(item => item.name === typeName)[0].value
  listQuery.value.deviceType = devicetype
  fetchData()
}
defineExpose({ initDialog })
</script>

<template>
  <el-dialog v-model="dialogFormVisible" title="选择设备" width="50%">
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.deviceCode" placeholder="设备编号" clearable />
      </search-item>
    </search-area>
    <!-- 查询结果Table显示 -->
    <div style="padding: 12px;">
      <normal-table ref="tableRef" :data="list" :total="total" :columns="columns" :query="listQuery" :page-sizes="[5]"
        :list-loading="loadingTable" @change="changePage" :is-showmulti-select="props.multiple" :is-multi="props.multiple"
        @multi-select="multiSelect">
        <template #preColumns>
          <!-- <el-table-column v-if="!props.multiple" label="选择" width="55" align="center">
            <template #default="scope">
              <el-radio v-model="select" :label="scope.$index" 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>
    </div>
    <template #footer>
      <span class="dialog-footer">
        <el-button type="primary" @click="submitForm">确认</el-button>
        <el-button @click="resetForm">
          取消
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
:deep(.el-radio__label) {
  display: none;
}
</style>
