<!-- 查询关联委托方的并且是已收入状态的样品 -->
<script setup lang="ts" name="SelectSample">
import { ref } from 'vue'
import type { Ref } from 'vue'
import { ElMessage } from 'element-plus'
import type { ISampleListQuery } from './receiptList-interface'
import type { ISampleList } from '@/views/customer/sample/list/sample_list_interface'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getSampleList } from '@/api/business/schedule/interchangeReceipt'

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

// 查询条件
const listQuery: Ref<ISampleListQuery> = ref({
  customerId: '', // 委托方id
  sampleName: '', // 样品名称
  sampleNo: '', // 样品编号
  customerName: '', // 委托方名称
  offset: 1,
  limit: 20,
})
// 表头
const columns = ref<TableColumn[]>([
  { text: '样品编号', value: 'sampleNo', width: '160', align: 'center', fixed: true },
  { text: '样品名称', value: 'sampleName', width: '120', align: 'center', fixed: true },
  { text: '型号', value: 'sampleModel', align: 'center' },
  { text: '出厂编号', value: 'manufacturingNo', align: 'center' },
  { text: '委托方代码', value: 'customerNo', align: 'center' },
  { text: '委托方名称', value: 'customerName', align: 'center' },
  // { text: '检定周期', value: 'measurePeriod', align: 'center' },
  // { text: '上次检定时间', value: 'measureLastTime', align: 'center', width: '180px' },
  // { text: '样品状态', value: 'sampleSatusName', align: 'center', width: '80px' },
  // { text: '备注', value: 'remark', align: 'center', width: '180px' },
])
// 表格数据
const list = ref<ISampleList[]>([])
// 总数
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
  }
  listQuery.value.customerId = props.customerId
  getSampleList(listQuery.value).then((response) => {
    list.value = response.data.rows
    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 = {
    customerId: '', // 委托方id
    sampleName: '', // 样品名称
    sampleNo: '', // 样品编号
    customerName: '', // 委托方名称
    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 clickConfirmSample = () => {
  if (!checkoutList.value.length) {
    ElMessage({
      message: '请选中',
      type: 'warning',
    })
  }
  // 将选择好的样品传给父组件
  emits('clickConfirmSample', checkoutList.value)
  cancle()
}

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

<template>
  <el-dialog v-model="visible" title="选择样品" width="65%" @close="cancle">
    <search-area
      style="padding: 0;"
      :need-clear="true"
      @search="searchList" @clear="clearList"
    >
      <search-item>
        <el-input
          v-model.trim="listQuery.sampleNo"
          placeholder="样品编号"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.sampleName"
          placeholder="样品名称"
          clearable
        />
      </search-item>
    </search-area>
    <normal-table
      :data="list"
      :total="total"
      :columns="columns"
      :query="listQuery"
      :list-loading="loadingTable"
      is-showmulti-select
      :is-multi="true"
      :height="260"
      :page-sizes="[20]"
      @change="changePage"
      @multiSelect="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="clickConfirmSample">
        确定
      </el-button>
      <el-button @click="cancle">
        取消
      </el-button>
    </template>
  </el-dialog>
</template>
