<!-- 交接单管理 -->
<script name="InterchangeReceiptList" lang="ts" setup>
import type { Ref } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import type { IList, IListQuery } from './interchangeReceipt-interface'
import { printJSON } from '@/utils/printUtils'
import { exportFile } from '@/utils/exportUtils'
import {
  deleteReceiptList,
  getReceiptList,
} from '@/api/business/manager/interchangeReceipt'
import type { TableColumn } from '@/components/NormalTable/table_interface'
const $router = useRouter()
const operateWidth = ref('160') // 操作栏的宽度
const { proxy } = getCurrentInstance() as any
// 查询条件
const listQuery: Ref<IListQuery> = ref({
  interchangeNo: '', // 交接单编号
  orderNo: '', // 任务单编号
  customerName: '', // 委托方名称
  deliverer: '', // 送样人
  offset: 1,
  limit: 20,
})
// 表头
const columns = ref<TableColumn[]>([
  { text: '交接单编号', value: 'interchangeNo', align: 'center', width: '160' },
  { text: '任务单编号', value: 'orderNo', align: 'center', width: '160' },
  { text: '委托方名称', value: 'customerName', align: 'center' },
  { text: '送检人', value: 'deliverer', align: 'center' },
  { text: '创建人', value: 'createUserName', align: 'center' },
  { text: '创建时间', value: 'createTime', align: 'center', width: '180' },
])
// 表格数据
const list = ref<IList[]>([])
// 总数
const total = ref(0)
// 表格加载状态
const loadingTable = ref(false)
// 选中的内容
const checkoutList = ref<IList[]>([])

// ---------------------------------表格----------------------------------------------
// 数据查询
function fetchData(isNowPage = false) {
  loadingTable.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.value.offset = 1
  }
  getReceiptList(listQuery.value).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    loadingTable.value = false
  })
}

// 点击搜索
const searchList = () => {
  fetchData(true)
}

// 点击重置
const clearList = () => {
  listQuery.value = {
    interchangeNo: '', // 交接单编号
    orderNo: '', // 任务单编号
    customerName: '', // 委托方名称
    deliverer: '', // 送样人
    offset: 1,
    limit: 20,
  }
  fetchData(true)
}

// 多选发生改变时
function handleSelectionChange(e: any) {
  checkoutList.value = e.map((item: { id: string }) => item.id)
}

// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
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 handleEdit = (row: IList, pageType: 'edit' | 'detail') => {
  $router.push(`/interchangeReceipt/${pageType}/${row.id}`)
}

// 点击打印
const print = () => {
  ElMessage.info('敬请期待')
}

// 点击删除
const del = (row: IList) => {
  ElMessageBox.confirm(
    '确认删除吗？',
    '提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    deleteReceiptList({ id: row.id }).then(() => {
      ElMessage.success('删除成功')
      fetchData()
    })
  })
}

// ------------------------------------按钮-------------------------------------------
// 点击新建
const add = () => {
  $router.push('/interchangeReceipt/add')
}

// 点击批量打印
const batchPrint = () => {
  ElMessage.info('敬请期待')
}

// ---------------------------------------钩子----------------------------------------
onMounted(() => {
  fetchData(true)
})
</script>

<template>
  <app-container>
    <search-area
      :need-clear="true"
      @search="searchList" @clear="clearList"
    >
      <search-item>
        <el-input
          v-model.trim="listQuery.interchangeNo"
          placeholder="交接单编号"
          class="short-input"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.orderNo"
          placeholder="任务单编号"
          class="short-input"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.customerName"
          placeholder="委托方"
          class="short-input"
          clearable
        />
      </search-item>
      <search-item>
        <el-input
          v-model.trim="listQuery.deliverer"
          placeholder="送检人"
          class="short-input"
          clearable
        />
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <icon-button icon="icon-add" title="新建" type="primary" @click="add" />
        <icon-button icon="icon-batchPrint" title="批量打印" type="primary" @click="batchPrint" />
      </template>
      <normal-table
        :data="list" :total="total" :columns="columns" :query="listQuery"
        :list-loading="loadingTable" is-showmulti-select @change="changePage" @multi-select="handleSelectionChange"
      >
        <template #preColumns>
          <el-table-column label="序号" width="55" align="center">
            <template #default="scope">
              {{ (listQuery.offset - 1) * listQuery.limit + scope.$index + 1 }}
            </template>
          </el-table-column>
        </template>
        <template #columns>
          <el-table-column label="操作" align="center" fixed="right" :width="operateWidth">
            <template #default="{ row }">
              <!-- 有样品状态的不允许编辑 -->
              <el-button
                size="small"
                link
                type="primary"
                @click="handleEdit(row, 'detail')"
              >
                详情
              </el-button>
              <el-button
                size="small"
                type="primary"
                link
                @click="handleEdit(row, 'edit')"
              >
                编辑
              </el-button>
              <el-button
                size="small"
                link
                type="primary"
                @click="print"
              >
                打印
              </el-button>
              <el-button
                size="small"
                link
                type="danger"
                @click="del(row)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
    </table-container>
  </app-container>
</template>

<style lang="scss" scoped>
// 样式
</style>

