<!-- 设备列表弹窗-多选 -->
<script lang="ts" setup name="SelectDeviceMultiple">
import { ElMessage } from 'element-plus'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getDictByCode } from '@/api/system/dict'
import { getInfoListPage } from '@/api/eqpt/device/info'
import { getDeptTreeList } from '@/api/system/dept'
import { toTreeList } from '@/utils/structure'
const emits = defineEmits(['add'])
const dialogFormVisible = ref(false)
// 查询条件
const listQuery = ref({
  equipmentName: '', // 设备名称
  equipmentNo: '', // 统一编号
  equipmentType: '1',
  usageStatus: '', // 使用状态
  deptIds: '', // 使用部门
  certificateValidStart: '',
  certificateValidEnd: '',
  limit: 5,
  offset: 1,
})
const loadingTable = ref<boolean>(false)
const list = ref([]) // 表格数据
const total = ref(0)
const select = ref(-1)
// 证书有效期开始结束时间
const datetimerange = ref()
watch(() => datetimerange.value, (newVal) => {
  listQuery.value.certificateValidStart = ''
  listQuery.value.certificateValidEnd = ''
  if (Array.isArray(newVal)) {
    if (newVal.length) {
      listQuery.value.certificateValidStart = `${newVal[0]} 00:00:00`
      listQuery.value.certificateValidEnd = `${newVal[1]} 23:59:59`
    }
  }
})
// 表头
const columns = ref<TableColumn[]>([
  {
    text: '统一编号',
    value: 'equipmentNo',
    align: 'center',
  },
  {
    text: '设备名称',
    value: 'equipmentName',
    align: 'center',
  },
  {
    text: '规格型号',
    value: 'model',
    align: 'center',
  },
  {
    text: '出厂编号',
    value: 'manufactureNo',
    align: 'center',
  },
  {
    text: '使用岗位',
    value: 'usePosition',
    align: 'center',
  },
  {
    text: '计量标识',
    value: 'meterIdentifyName',
    align: 'center',
  },
  {
    text: '使用状态',
    value: 'usageStatusName',
    align: 'center',
  },
  {
    text: '证书有效期',
    value: 'certificateValid',
    align: 'center',
  },
])
// 获取使用状态列表
const useStatusList = ref()
const fetchUseStatus = () => {
  getDictByCode('eqptDeviceUseStatus').then((res) => {
    useStatusList.value = res.data
  })
}
fetchUseStatus()
// 获取数据列表
const fetchData = () => {
  select.value = -1
  loadingTable.value = true
  getInfoListPage(listQuery.value, '全部').then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  })
}
// 搜索
const search = () => {
  fetchData()
}
const selectTreeRef = ref()
// 重置
const reset = () => {
  datetimerange.value = []
  listQuery.value = {
    equipmentName: '', // 设备名称
    equipmentNo: '', // 统一编号
    equipmentType: '1',
    usageStatus: '', // 使用状态
    deptIds: '', // 使用部门
    certificateValidStart: '',
    certificateValidEnd: '',
    limit: 5,
    offset: 1,
  }
  search()
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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 multi = ref(false)
// 表格多选中的行
const selectList = ref<any[]>([])
// 表格多选
const multiSelect = (row: any[]) => {
  selectList.value = row
}
const showTable = ref
// 点击保存
const submitForm = () => {
  if (!multi.value) {
    // 单选
    if (selectList.value.length) {
      emits('add', selectList.value[0])
      dialogFormVisible.value = false
      reset()
    }
    else {
      ElMessage.warning('请先选择设备')
    }
  }
  else {
    // 多选
    if (selectList.value.length) {
      emits('add', selectList.value)
      dialogFormVisible.value = false
      reset()
    }
    else {
      ElMessage.warning('请先选择设备')
    }
  }
}
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  reset()
}

// 初始化
const initDialog = (isMulti: boolean) => {
  multi.value = isMulti
  // reset()
  dialogFormVisible.value = true
  fetchData() // 获取人员列表
}
defineExpose({ initDialog })
// 加载组织机构树形下拉
const deptTreeList = ref()
const fetchDeptTree = () => {
  getDeptTreeList().then((res) => {
    if (res.data) { // 将列表转树结构
      deptTreeList.value = toTreeList(res.data, '0', true)
    }
  })
}

fetchDeptTree()
</script>

<template>
  <el-dialog v-model="dialogFormVisible" title="选择设备" width="65%">
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.equipmentName" placeholder="设备名称" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.equipmentNo" placeholder="统一编号" clearable />
      </search-item>
      <search-item>
        <!-- <el-input v-model.trim="listQuery.deptIds" placeholder="使用岗位" clearable /> -->
        <dept-select v-model="listQuery.deptIds" :data="deptTreeList" placeholder="使用岗位" style="width: 100%;" />
      </search-item>
      <search-item>
        <el-select v-model="listQuery.usageStatus" placeholder="使用状态" clearable>
          <el-option v-for="item in useStatusList" :key="item.value" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-date-picker
          v-model="datetimerange" type="daterange" value-format="YYYY-MM-DD"
          format="YYYY-MM-DD" range-separator="至" start-placeholder="证书有效期开始时间" end-placeholder="证书有效期结束时间"
        />
      </search-item>
    </search-area>
    <!-- 查询结果Table显示 -->
    <div style="padding: 12px;">
      <normal-table
        v-if="dialogFormVisible"
        :page-sizes="[5, 10, 20]"
        :data="list" :total="total" :columns="columns" :query="listQuery"
        :list-loading="loadingTable" :is-showmulti-select="true" :is-multi="true" @change="changePage" @multi-select="multiSelect"
      >
        <template #preColumns>
          <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>
