<!-- 选择分包项目申请 -->
<script setup lang="ts" name="checkSelectApply">
import { ref } from 'vue'
import type { Ref } from 'vue'
import { ElMessage } from 'element-plus'
import type { IListQuery } from '@/views/business/subpackage/subpackage-interface'
import type { IList } from '@/views/business/subpackage/apply/apply-interface'
import type { ISampleList } from '@/views/customer/sample/list/sample_list_interface'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getSamplesByOderId } from '@/api/business/lab/primitiveLogList'
import { SCHEDULE } from '@/utils/scheduleDict'
import { getListPage } from '@/api/business/subpackage/apply'

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

// 查询条件
const listQuery: Ref<IListQuery> = ref({
  applicantEndTime: '', // 开始时间
  applicantName: '', // 申请人名称
  applicantStartTime: '', // 结束时间
  approvalStatus: '4', // 申请状态 4已通过
  formId: SCHEDULE.BUSINESS_SUBPACKAGE_APPLY, // 表单id
  outsourcerName: '', // 	分包方名称
  projectName: '', // 分包项目名称
  projectNo: '', // 分包项目编号
  offset: 1,
  limit: 20,
})
// 表头
const columns = ref<TableColumn[]>([
  { text: '分包项目编号', value: 'projectNo', align: 'center', width: '160px' },
  { text: '分包项目名称', value: 'projectName', align: 'center' },
  { text: '申请人', value: 'applicantName', align: 'center' },
  { text: '分包方名称', value: 'outsourcerName', align: 'center' },
  { text: '分包原因', value: 'outsourceReasonName', align: 'center' },
  { text: '申请时间', value: 'applicantTime', align: 'center', width: '180px' },
  { text: '审批状态', value: 'applyApprovalStatusName', align: 'center' },
])
// 表格数据
const list = ref<IList[]>([])
// 总数
const total = ref(0)
// 表格加载状态
const loadingTable = ref(false)
// 选中的内容
const checkoutList = ref<string[]>([])

// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.value.offset = 1
  }
  // 模拟数据
  getListPage(listQuery.value).then((response) => {
    list.value = response.data.rows.map((item: IList) => {
      return {
        ...item,
        approvalStatusName: item.applyApprovalStatusName,
      }
    })
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  })
}

// 多选发生改变时
function handleSelectionChange(e: any) {
  checkoutList.value = e.map((item: any) => {
    return {
      ...item,
      isEdit: false,
    }
  })
}
// 点击搜索
const searchList = () => {
  fetchData(true)
}
// 点击重置
const clearList = () => {
  listQuery.value = {
    applicantEndTime: '', // 开始时间
    applicantName: '', // 申请人名称
    applicantStartTime: '', // 结束时间
    approvalStatus: '4', // 申请状态 4已通过
    formId: SCHEDULE.BUSINESS_SUBPACKAGE_APPLY, // 表单id
    outsourcerName: '', // 	分包方名称
    projectName: '', // 分包项目名称
    projectNo: '', // 分包项目编号
    offset: 1,
    limit: 20,
  }
  fetchData(true)
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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
  }
  fetchData(true)
}

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

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

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

<template>
  <div class="select-kehu-dialog">
    <el-dialog :modelValue="visible" title="选择样品" width="65%" @close="cancle">
      <search-area
        style="padding-left: 0;padding-right: 0;"
        :need-clear="true"
        @search="searchList" @clear="clearList"
      >
        <search-item>
          <el-input v-model.trim="listQuery.projectNo" placeholder="分包项目编号" class="short-input" clearable />
        </search-item>
        <search-item>
          <el-input v-model.trim="listQuery.projectName" placeholder="分包项目名称" class="short-input" clearable />
        </search-item>
        <!-- <search-item>
          <el-input v-model.trim="listQuery.applicantName" placeholder="申请人" class="short-input" clearable />
        </search-item> -->
        <search-item>
          <el-input v-model.trim="listQuery.outsourcerName" placeholder="分包方名称" class="short-input" clearable />
        </search-item>
      </search-area>
      <normal-table
        :data="list"
        :total="total"
        :columns="columns"
        :query="listQuery"
        :list-loading="loadingTable"
        is-showmulti-select
        :is-multi="false"
        :page-sizes="[20]"
        :height="260"
        @change="changePage"
        @multi-select="handleSelectionChange"
      >
        <!-- 序号 -->
        <template #preColumns>
          <el-table-column label="#" width="55" align="center" fixed>
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.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>
