<template>
  <div class="gi_table_page">
    <GiTable
        row-key="id"
        :data="dataList"
        :columns="columns"
        :loading="loading"
        :scroll="{ x: '100%', y: '100%', minWidth: 1600 }"
        :pagination="pagination"
        :disabled-tools="['size']"
        :disabled-column-keys="['name']"
        :selected-keys="selectedKeys"
        :row-selection="{ type: 'checkbox', showCheckedAll: true }"
        @select-all="selectAll"
        @select="select"
        @refresh="search"
    >
      <template #toolbar-left>
        <a-input-search v-model="queryForm.userName" placeholder="请输入姓名" allow-clear @search="search" />
        <a-input-search v-model="queryForm.carNo" placeholder="请输入人员卡号" allow-clear @search="search" />
	      <a-input-search v-model="queryForm.orderNo" placeholder="请输入订单号" allow-clear @search="search" />
        <a-input-search v-model="queryForm.materialName" placeholder="请输入物料名称" allow-clear @search="search" />
        <a-input-search v-model="queryForm.encoding" placeholder="请输入物料编码" allow-clear @search="search" />
        <a-date-picker
          v-model="queryForm.startDate"
          placeholder="请选择开始时间"
          show-time
          format="YYYY-MM-DD HH:mm:ss"
          style="height: 32px"
          @change="search"
        />
        <a-date-picker
          v-model="queryForm.endDate"
          placeholder="请选择结束时间"
          show-time
          format="YYYY-MM-DD HH:mm:ss"
          style="height: 32px"
          @change="search"
        />

        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #toolbar-right>
        <a-button v-permission="['workOrder:record:delete']" type="outline" status="danger" @click="onDelete">
          <template #icon><icon-delete /></template>
          <template #default>删除</template>
        </a-button>
        <a-button v-permission="['workOrder:record:export']" @click="onExport">
          <template #icon><icon-download /></template>
          <template #default>导出</template>
        </a-button>
      </template>

      <template #photoUrl="{ record }">
        <a-image :src="record.photoUrl" width="55" />
      </template>
      <template #unitWeight="{ record }">
        {{ record.unitWeight + 'g' }}
      </template>
      <template #totalWeight="{ record }">
        {{ record.totalWeight + 'g' }}
      </template>

      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['workOrder:record:detail']" title="详情" @click="onDetail(record)">详情</a-link>
          <a-link v-permission="['workOrder:record:delete']" status="danger" :title="'删除'" @click="onDeleteOne(record.id)">删除</a-link>
        </a-space>
      </template>
    </GiTable>


  </div>
</template>

<script setup lang="ts">
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useDownload, useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import type GiTable from "@/components/GiTable/index.vue";
import {ref} from "vue";
import {Message} from "@arco-design/web-vue";
import {
  deleteWorkOrder,
  exportWorkOrder,
  listWorkOrder,
  type WorkOrderQuery,
  type WorkOrderResp
} from "@/apis/workOrder/workOrder";

defineOptions({ name: 'Record' })



const queryForm = reactive<WorkOrderQuery>({
  orderNo: undefined,
  materialName: undefined,
  encoding: undefined,
  userName: undefined,
  carNo: undefined,
  startDate: undefined,
  endDate: undefined,
  sort: ['w.id,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  selectedKeys,
  select,
  selectAll,
  search,
  handleDelete,
} = useTable((page) => listWorkOrder({ ...queryForm, ...page }), { immediate: true })

// 创建工具函数统一处理列配置
const processColumns = (columns: TableInstanceColumns[]): TableInstanceColumns[] => {
  return columns.map(column => {
    const defaultConfig = {
      ellipsis: true,
      tooltip: true
    };
    return { ...defaultConfig, ...column };
  });
};

// 定义列配置，使用工具函数处理
const columns = ref<TableInstanceColumns[]>(processColumns([
  { title: '创建人', dataIndex: 'createUserString' },
  { title: '卡号', dataIndex: 'cardNo' },
  { title: '标题', dataIndex: 'title', width: 180 },
  { title: '物料图片', dataIndex: 'photoUrl', slotName: 'photoUrl', minWidth: 180, ellipsis: true, tooltip: true },
  { title: '物料名称', dataIndex: 'materialName' },
  { title: '物料编码', dataIndex: 'encoding' },
  { title: '单位克重', dataIndex: 'unitWeight' ,slotName: 'unitWeight'},
  { title: '总重量', dataIndex: 'totalWeight' ,slotName: 'totalWeight'},
  { title: '称重次数', dataIndex: 'totalCount' },
  { title: '创建时间', dataIndex: 'createTime', width: 180 },
  {
    title: '操作',
    dataIndex: 'action',
    slotName: 'action',
    width: 160,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['workOrder:record:detail', 'workOrder:record:update', 'workOrder:record:delete'])
  }
]))

// 重置
const reset = () => {
  queryForm.orderNo = undefined
  queryForm.materialName = undefined
  queryForm.encoding = undefined
  queryForm.userName = undefined
  queryForm.carNo = undefined
  queryForm.startDate = undefined
  queryForm.endDate = undefined
  search()
}

// 详情
const onDetail = (record: WorkOrderResp) => {

}

// 删除
const onDeleteOne = (id) => {
  return handleDelete(() => deleteWorkOrder(id), {
    content: `是否确定删除该条数据？`,
    showModal: true
  })
}

// 删除
const onDelete = () => {
  if (!selectedKeys.value.length) {
    return Message.warning('请选择数据')
  }
  return handleDelete(() => deleteWorkOrder(selectedKeys.value), {
    content: `是否确定删除选中的 ${selectedKeys.value.length} 条数据？`,
    showModal: true
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportWorkOrder(queryForm))
}

</script>

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