<!-- 选择委托书dialog -->
<script setup lang="ts" name="SelectSample">
import { ref } from 'vue'
import type { Ref } from 'vue'
import { ElMessage } from 'element-plus'
import type { IListQuery, IOrderList, dictType } from '@/views/business/schedule/order/orderList_interface'
import type { TableColumn } from '@/components/NormalTable/table_interface'
import { getOrderList } from '@/api/business/schedule/order'
import { getDictByCode } from '@/api/system/dict'

const props = defineProps({
  visible: {
    type: Boolean,
    default: false,
  },
})
const emits = defineEmits(['changeVisible', 'confirmCheckout'])
const isUrgentMap = ref<dictType[]>([]) // 是否加急
const statusMap = ref<dictType[]>([]) // 接收状态

// 获取字典值
function getDict() {
  // 是否加急
  getDictByCode('isUrgent').then((response) => {
    isUrgentMap.value = response.data.map((item: dictType) => {
      return {
        ...item,
        value: parseInt(item.value as string),
      }
    })
    // 在已有的字典的基础上增加一个全部0进去
    isUrgentMap.value.push({
      id: '',
      name: '全部',
      value: 2, // 是否加急全部后端定为2
    })
  })
  // 接收状态
  getDictByCode('orderStatus').then((response) => {
    statusMap.value = response.data.map((item: dictType) => {
      return {
        ...item,
        value: parseInt(item.value as string), // 后端需要int型，在此转换
      }
    })
    // 在已有的字典的基础上增加一个全部0进去
    statusMap.value.push({
      id: '',
      name: '全部',
      value: 0,
    })
    console.log(statusMap.value)
  })
}
getDict()

// 查询条件
const listQuery: Ref<IListQuery> = ref({
  orderCode: '', // 委托书编号
  customerNo: '', // 委托方代码
  customerName: '', // 委托方名称
  deliverer: '', // 送样人
  isUrgent: 2, // 是否加急 2全部 1是 0否
  status: 0, // 接收状态
  offset: 1,
  limit: 20,
})

const columns = ref<TableColumn[]>([
  { text: '委托书编号', value: 'orderCode', align: 'center', width: '160px', fixed: true },
  { text: '委托方代码', value: 'customerNo', align: 'center', width: '160px' },
  { text: '委托方名称', value: 'customerName', align: 'center' },
  { text: '送样人', value: 'deliverer', align: 'center' },
  { text: '联系方式', value: 'customerPhone', align: 'center' },
  { text: '样品数量', value: 'sampleCount', align: 'center' },
  { text: '是否加急', value: 'isUrgent', align: 'center', width: '55px', filter: (row: IOrderList) => { return row.isUrgent == '1' ? '是' : '否' }, styleFilter: (row: IOrderList) => { return row.isUrgent == '1' ? 'color: red' : '' } },
  { text: '接收状态', value: 'statusName', align: 'center', width: '80px' },
  { text: '创建时间', value: 'createTime', align: 'center', width: '180px' },
  { text: '备注', value: 'remark', align: 'center' },
])
// 表格数据
const list = ref<IOrderList[]>([])
// 总数
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
  }
  getOrderList(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
}
// 点击搜索
const searchList = () => {
  fetchData(true)
}
// 点击重置
const clearList = () => {
  listQuery.value = {
    orderCode: '', // 委托书编号
    customerNo: '', // 委托方代码
    customerName: '', // 委托方名称
    deliverer: '', // 送样人
    isUrgent: 2, // 是否加急
    status: 0, // 接收状态
    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 clickConfirmOrder = () => {
  if (!checkoutList.value.length) {
    ElMessage({
      message: '请选中',
      type: 'warning',
    })
  }
  // 将选择好的样品传给父组件
  emits('confirmCheckout', checkoutList.value)
  cancle()
}

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

<template>
  <div>
    <el-dialog v-model="visible" title="选择委托书" @close="cancle">
      <search-area
        :need-clear="true"
        @search="searchList" @clear="clearList"
      >
        <search-item>
          <el-input
            v-model.trim="listQuery.orderCode"
            placeholder="委托书编号"
            class="short-input"
            clearable
          />
        </search-item>
        <search-item>
          <el-input
            v-model.trim="listQuery.customerNo"
            placeholder="委托方代码"
            class="short-input"
            clearable
          />
        </search-item>
        <search-item>
          <el-input
            v-model.trim="listQuery.deliverer"
            placeholder="送样人"
            class="short-input"
            clearable
          />
        </search-item>
        <search-item>
          <el-select v-model="listQuery.isUrgent" placeholder="是否加急" style="width: 120px;" clearable>
            <el-option
              v-for="item in isUrgentMap"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </search-item>
        <search-item>
          <el-select v-model="listQuery.status" placeholder="接收状态" style="width: 120px;" clearable>
            <el-option
              v-for="item in statusMap"
              :key="item.value"
              :label="item.name"
              :value="item.value"
            />
          </el-select>
        </search-item>
      </search-area>
      <normal-table
        :data="list"
        :total="total"
        :columns="columns"
        :query="listQuery"
        :list-loading="loadingTable"
        is-showmulti-select
        :is-multi="false"
        @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="clickConfirmOrder">
          确定
        </el-button>
        <el-button @click="cancle">
          取消
        </el-button>
      </template>
    </el-dialog>
  </div>
</template>
