<template>
  <div class="gi_table_page">
    <GiTable
      title="整箱领取记录管理"
      row-key="id"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #toolbar-left>
	    <a-input-search v-model="queryForm.orderNo" placeholder="请输入任务工单号" allow-clear @search="search" />
	    <a-input-search v-model="queryForm.materialCode" placeholder="请输入物料编码" allow-clear @search="search" />
	    <a-input-search v-model="queryForm.imgUrl" placeholder="请输入图片地址" allow-clear @search="search" />
	    <a-input-search v-model="queryForm.createUser" placeholder="请输入创建人" allow-clear @search="search" />
        <a-range-picker
            v-model="queryForm.createTime"
            :show-time="true"
            format="YYYY-MM-DD HH:mm:ss"
            style="height: 32px"
            :allow-clear="true"
            @change="search"
        />
        <a-button @click="reset">
          <template #icon><icon-refresh /></template>
          <template #default>重置</template>
        </a-button>
      </template>
      <template #toolbar-right>
        <a-button v-permission="['fullWorkOrder:fullWorkOrder:add']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
        <a-button v-permission="['fullWorkOrder:fullWorkOrder:export']" @click="onExport">
          <template #icon><icon-download /></template>
          <template #default>导出</template>
        </a-button>
      </template>

      <template #imgUrl="{ record }">
        <a-image
            width="60"
            :src="record.imgUrl"
        />
      </template>

      <template #action="{ record }">
        <a-space>
          <a-link
            v-permission="['fullWorkOrder:fullWorkOrder:delete']"
            status="danger"
            :disabled="record.disabled"
            :title="record.disabled ? '不可删除' : '删除'"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <FullWorkOrderAddModal ref="FullWorkOrderAddModalRef" @save-success="search" />
  </div>
</template>

<script setup lang="ts">
import FullWorkOrderAddModal from './FullWorkOrderAddModal.vue'
import { type FullWorkOrderResp, type FullWorkOrderQuery, deleteFullWorkOrder, exportFullWorkOrder, listFullWorkOrder } from '@/apis/fullWorkOrder/fullWorkOrder'
import type { TableInstanceColumns } from '@/components/GiTable/type'
import { useDownload, useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'

defineOptions({ name: 'FullWorkOrder' })


const queryForm = reactive<FullWorkOrderQuery>({
  orderNo: undefined,
  materialCode: undefined,
  imgUrl: undefined,
  createUser: undefined,
  createTime: undefined,
  sort: ['id,desc']
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete
} = useTable((page) => listFullWorkOrder({ ...queryForm, ...page }), { immediate: true })
const columns = ref<TableInstanceColumns[]>([
  { title: '标题', dataIndex: 'title', slotName: 'title' },
  { title: '任务工单号', dataIndex: 'orderNo', slotName: 'orderNo' },
  { title: '物料编码', dataIndex: 'materialCode', slotName: 'materialCode' },
  { title: '抓拍图', dataIndex: 'imgUrl', slotName: 'imgUrl' },
  { title: '创建人', dataIndex: 'createUserString', slotName: 'createUser' },
  { title: '创建时间', dataIndex: 'createTime', slotName: 'createTime' },
  { title: '修改人', dataIndex: 'updateUserString', slotName: 'updateUser', show: false },
  { title: '修改时间', dataIndex: 'updateTime', slotName: 'updateTime', show: false },
  {
    title: '操作',
    dataIndex: 'action',
    slotName: 'action',
    width: 160,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['fullWorkOrder:fullWorkOrder:detail', 'fullWorkOrder:fullWorkOrder:update', 'fullWorkOrder:fullWorkOrder:delete'])
  }
]);

// 重置
const reset = () => {
  queryForm.orderNo = undefined
  queryForm.materialCode = undefined
  queryForm.imgUrl = undefined
  queryForm.createUser = undefined
  queryForm.createTime = undefined
  search()
}

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

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

const FullWorkOrderAddModalRef = ref<InstanceType<typeof FullWorkOrderAddModal>>()
// 新增
const onAdd = () => {
  FullWorkOrderAddModalRef.value?.onAdd()
}

</script>

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