<!-- 分包方弹窗 -->
<script lang="ts" setup name="SubpackageDialog">
import { ElMessage } from 'element-plus'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getDictByCode } from '@/api/system/dict'
import { getListPage } from '@/api/eqpt/subpackage/directory'
import { SCHEDULE } from '@/utils/scheduleDict'
const emits = defineEmits(['add'])
const dialogFormVisible = ref(false)
// 查询条件
const listQuery = ref({
  approvalStatus: '',
  companyName: '',
  createTimeEnd: '',
  createTimeStart: '',
  directorName: '',
  formId: SCHEDULE.SUBCONTRACTOR_APPROVAL,
  ids: [],
  subcontractorNo: '',
  offset: 1,
  limit: 5,
})
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.createTimeStart = ''
  listQuery.value.createTimeEnd = ''
  if (Array.isArray(newVal)) {
    if (newVal.length) {
      listQuery.value.createTimeStart = `${newVal[0]} 00:00:00`
      listQuery.value.createTimeEnd = `${newVal[1]} 23:59:59`
    }
  }
})
// 表头
const columns = ref([
  {
    text: '分包方编号',
    value: 'subcontractorNo',
    align: 'center',
  },
  {
    text: '单位名称',
    value: 'companyName',
    align: 'center',
  },
  {
    text: '联系人',
    value: 'directorName',
    align: 'center',
  },
  {
    text: '联系电话',
    value: 'contactNumber',
    align: 'center',
  },
  {
    text: '测试能力',
    value: 'testAbility',
    align: 'center',
  },
  {
    text: '地址',
    value: 'address',
    align: 'center',
  },
  {
    text: '创建时间',
    value: 'createTime',
    align: 'center',
  },
])

// 获取数据列表
const fetchData = () => {
  select.value = -1
  loadingTable.value = true
  getListPage(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.approvalStatus = ''
  listQuery.value.companyName = ''
  listQuery.value.createTimeEnd = ''
  listQuery.value.createTimeStart = ''
  listQuery.value.directorName = ''
  listQuery.value.formId = SCHEDULE.SUBCONTRACTOR_APPROVAL
  listQuery.value.ids = []
  listQuery.value.subcontractorNo = ''
  listQuery.value.offset = 1
  listQuery.value.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 device = list.value.filter((item, index) => index == select.value)[0]
    emits('add', device)
    dialogFormVisible.value = false
    reset()
  }
  else {
    ElMessage.warning('请先选择分包方')
  }
}
// 取消
const resetForm = () => {
  dialogFormVisible.value = false
  reset()
}
// 初始化
const initDialog = () => {
  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.subcontractorNo" placeholder="分包方编号" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.companyName" placeholder="单位名称" clearable />
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.directorName" placeholder="联系人" clearable />
      </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
        :data="list" :total="total" :columns="columns" :query="listQuery"
        :list-loading="loadingTable" @change="changePage"
      >
        <template #preColumns>
          <el-table-column label="" width="55" align="center">
            <template #default="scope">
              <el-radio v-model="select" :label="scope.$index" :disabled="scope.row.isRadioDisabled" 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>
