<!-- 选择标准装置 -->
<script lang="ts" setup name="StandardDialog">
import { ElMessage } from 'element-plus'
import { listPageApi } from '@/api/measure/file'
import { getStandardList } from '@/api/device/standard'
// import type { TreeStructure } from '@/views/system/user/user-interface'
import comTreeSelect from '@/views/system/user/selecTree.vue'
import { toTreeList } from '@/utils/structure'
import type { TableColumn } from '@/components/NormalTable/table_interface'
const props = defineProps({
  titleType: { // 申请类型
    type: String,
    default: '',
  },
})
const emits = defineEmits(['add'])
const dialogFormVisible = ref(false)
// 查询条件
const listQuery = ref({
  category: '', // 类别
  id: '',
  managerState: '', // 管理状态
  preparationEndDate: '', // 筹建日期结束
  preparationStartDate: '', // 筹建日期开始
  standardLaboratory: '', // 标准所在实验室
  standardName: '', // 标准名称
  standardNo: '', // 标准代码
  validEndTime: '', // 到期结束时间
  validStartTime: '', // 到期开始时间
  offset: 1, // 多少页
  limit: 5, // 一页多少条
})
const loadingTable = ref<boolean>(false)
const list = ref([]) // 表格数据
const total = ref<number>(0)
const select = ref<number>(-1)
const columns = ref<TableColumn[]>([
  {
    text: '标准代码',
    value: 'standardNo',
    align: 'center',
    width: '160',
  },
  {
    text: '标准名称',
    value: 'standardName',
    align: 'center',
  },
  {
    text: '类别',
    value: 'categoryName',
    align: 'center',
    width: '90',
  },
  {
    text: '标准等级',
    value: 'standardLevelName',
    align: 'center',
  },
  {
    text: '传递范围',
    value: 'transmitRangeName',
    align: 'center',
    width: '90',
  },
  {
    text: '管理状态',
    value: 'managerStateName',
    align: 'center',
    width: '90',
  }])

// 获取数据列表(在用1、暂停2、撤销3)
const fetchData = () => {
  loadingTable.value = true
  select.value = -1
  if (props.titleType === '更换申请' || props.titleType === '暂停申请') {
    listQuery.value.managerState = '1' // 在用
  }
  else if (props.titleType === '恢复申请') {
    listQuery.value.managerState = '2' // 暂停
  }
  else { // 撤销和复查只能操作在用和暂停的
    listQuery.value.managerState = ''
  }
  getStandardList(listQuery.value).then((res) => {
    if (res.code === 200) {
      list.value = res.data.rows
      total.value = res.data.total
      loadingTable.value = false
    }
  }).catch(() => {
    loadingTable.value = false
  })
}
// 搜索
const search = () => {
  fetchData()
}
const selectTreeRef = ref()
// 重置
const reset = () => {
  listQuery.value = {
    category: '', // 类别
    id: '',
    managerState: '', // 管理状态
    preparationEndDate: '', // 筹建日期结束
    preparationStartDate: '', // 筹建日期开始
    standardLaboratory: '', // 标准所在实验室
    standardName: '', // 标准名称
    standardNo: '', // 标准代码
    validEndTime: '', // 到期结束时间
    validStartTime: '', // 到期开始时间
    offset: 1, // 多少页
    limit: 5, // 一页多少条
  }
  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 submitForm = () => {
  if (select.value >= 0) {
    const person = list.value.filter((item, index) => index == select.value)[0]
    emits('add', person)
    dialogFormVisible.value = false
    reset()
  }
  else {
    ElMessage.warning('请先选择设备')
  }
}
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  reset()
}
// 初始化
const initDialog = () => {
  // reset()
  dialogFormVisible.value = true
  fetchData()
}
defineExpose({ initDialog })
</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.standardName"
          placeholder="标准名称"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.standardNo"
          placeholder="标准代码"
          clearable
        />
      </search-item>
    </search-area>
    <!-- 查询结果Table显示 -->
    <div style="padding: 12px;">
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery" :page-sizes="[5]"
        :list-loading="loadingTable" @change="changePage"
      >
        <template #preColumns>
          <el-table-column label="#" width="55" align="center" fixed="left">
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
          <el-table-column label="" width="55" align="center" fixed="left">
            <template #default="scope">
              <el-radio v-model="select" :disabled="scope.row.managerStateName === '撤销'" :label="scope.$index" class="radio" />
            </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>
