<!-- 选择所依据的技术文件 -->
<script setup lang="ts" name="SelectFiles">
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import dayjs from 'dayjs'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { SCHEDULE } from '@/utils/scheduleDict'
import type { fileListType, fileSearchType } from '@/views/measure/file/file-interface'
import { deleteApi, exportFileApi, listPageApi, updateApi } from '@/api/measure/file'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
  isMulti: {
    type: Boolean,
    default: false,
  },
})
const emits = defineEmits(['changeVisible', 'confirmCheckout'])

// 查询条件
const searchQuery = ref<fileSearchType>({
  fileNo: '', // 编号
  fileName: '', // 名称
  // publishTime: '', // 发布时间
  fileCode: '', // 文件号
  // effectiveTime: '', // 实施时间
  effectiveStatus: '1', // 实施状态--1：在用
  effectiveEndTime: '',
  effectiveStartTime: '',
  publishEndTime: '',
  publishStartTime: '',
  limit: 5,
  offset: 1,
  fileType: '10', // 类型--10:计量规程
  ids: [] as string[],
  formId: SCHEDULE.FILE_APPROVAL,
})
// 表头
const columns = ref<TableColumn[]>([
  { text: '文件编号', value: 'fileNo', align: 'center', width: '160' },
  { text: '文件名称', value: 'fileName', align: 'center' },
  { text: '文件号', value: 'fileCode', align: 'center' },
  { text: '发布时间', value: 'publishTime', align: 'center', width: '180' },
  { text: '实施时间', value: 'effectiveTime', align: 'center', width: '180' },
  { text: '实施状态', value: 'effectiveStatusName', align: 'center', width: '100' },
])
// 表格数据
const list = ref<fileListType[]>([])
// 总数
const total = ref(0)
// 表格加载状态
const loadingTable = ref(false)
// 选中的内容
const checkoutList = ref<string[]>([])

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    searchQuery.value.offset = 1
  }
  listPageApi(searchQuery.value).then((res) => {
    if (res.code === 200) {
      list.value = res.data.rows
      total.value = res.data.total
    }
    loadingTable.value = false
  }).catch((_) => {
    loadingTable.value = false
  })
}

// 多选发生改变时
function handleSelectionChange(e: any) {
  checkoutList.value = e.map((item: any) => {
    return {
      ...item,
      isEdit: false,
    }
  })
}
// 点击搜索
const searchList = () => {
  fetchData(true)
}
// 点击重置
const clearList = () => {
  searchQuery.value = {
    fileNo: '', // 编号
    fileName: '', // 名称
    // publishTime: '', // 发布时间
    fileCode: '', // 文件号
    // effectiveTime: '', // 实施时间
    effectiveStatus: '1', // 实施状态--1：在用
    effectiveEndTime: '',
    effectiveStartTime: '',
    publishEndTime: '',
    publishStartTime: '',
    limit: 5,
    offset: 1,
    fileType: '10', // 类型--10:计量规程
    ids: [] as string[],
    formId: SCHEDULE.FILE_APPROVAL,
  }
  fetchData(true)
}

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

// 点击取消
const cancle = () => {
  emits('changeVisible', false)
}

// 点击确定
const clickConfirm = () => {
  if (!checkoutList.value.length) {
    ElMessage({
      message: '请选中',
      type: 'warning',
    })
  }
  // 将选择好的样品传给父组件
  emits('confirmCheckout', checkoutList.value)
  cancle()
}

watch(() => props.visible, (newValue) => {
  if (newValue) {
    fetchData(true)
  }
})
</script>

<template>
  <div class="select-kehu-dialog">
    <el-dialog v-model="visible" title="选择所依据的技术文件" width="65%" @close="cancle">
      <search-area
        :need-clear="true"
        @search="searchList" @clear="clearList"
      >
        <search-item>
          <el-input v-model="searchQuery.fileNo" placeholder="编号" clearable class="w-50 m-2" style="width: 187px;" />
        </search-item>
        <search-item>
          <el-input v-model="searchQuery.fileName" placeholder="名称" clearable class="w-50 m-2" style="width: 187px;" />
        </search-item>
        <search-item>
          <el-input v-model="searchQuery.fileCode" placeholder="文件号" clearable class="w-50 m-2" style="width: 187px;" />
        </search-item>
      </search-area>
      <normal-table
        :data="list"
        :total="total"
        :columns="columns"
        :query="searchQuery"
        :list-loading="loadingTable"
        is-showmulti-select
        :is-multi="$props.isMulti"
        :page-sizes="[5]"
        @change="changePage"
        @multi-select="handleSelectionChange"
      >
        <!-- 序号 -->
        <template #preColumns>
          <el-table-column label="#" width="55" align="center" fixed>
            <template #default="scope">
              {{ (searchQuery.offset - 1) * searchQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
      </normal-table>
      <template #footer>
        <el-button type="primary" @click="clickConfirm">
          确定
        </el-button>
        <el-button @click="cancle">
          取消
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>
