<template>
  <div>
    <page-layout>
      <a-row :gutter="[0,2]">
        <a-col style="width: 100%;">
          <pro-table v-if="visible" ref="tableChilrenRef" rowKey="id" :fetch="fetch" :columns="columns"
            :toolbar="toolbar" :operate="operate" :param="state.param" :pagination="pagination"
            :row-selection="{ selectedRowKeys:state.selectedRowKeys, onChange: onSelectChange }">
          </pro-table>
        </a-col>
      </a-row>
    </page-layout>
  </div>
</template>
<script>
  import {
    message,
    Modal as modal
  } from '@hwork/ant-design-vue';
  import {
    ExclamationCircleOutlined
  } from '@ant-design/icons-vue';
  import {
    getTallyPallet,
    createTallyTask,
    remove,
    removeBatch
  } from "@/api/wms/inventoryView";
  import {
    reactive,
    createVNode,
    watch,
    ref
  } from 'vue';

  const removeKey = "remove";
  const removeBatchKey = "removeBatch";
  const createTallyTaskKey = "createTallyTask";

  export default {
    props: {
      visible: {
        type: Boolean,
        default: false
      },
      record: {
        type: Object
      },
    },
    components: {},

    setup(props) {

      const tableChilrenRef = ref()

      watch(props, (props) => {
        state.param.palletCode = props.record.palletCode;
      })

      const columns = [{
        dataIndex: "index",
        key: "index",
        title: "序号",
        width: 60,
        fixed: 'left',
        align: 'center'
      },
      {
          dataIndex: "palletCode",
          key: "palletCode",
          title: "托盘号",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "skuCode",
          key: "skuCode",
          title: "物料编号",
          width: 150,
          showSearch: true,
        },
        {
          dataIndex: "skuName",
          key: "skuName",
          title: "物料描述",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "skuType",
          key: "skuType",
          title: "物料类型",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "batch",
          key: "batch",
          title: "批次",
          width: 100,
          showSearch: true,
        },
        {
          dataIndex: "serial",
          key: "serial",
          title: "序列号",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "master",
          key: "master",
          title: "货主",
          width: 100,
          showSearch: true,
        },
        {
          dataIndex: "producer",
          key: "producer",
          title: "厂家",
          width: 100,
          showSearch: true,
        },
        {
          dataIndex: "customer",
          key: "customer",
          title: "客户",
          width: 100,
          showSearch: true,
        },
        {
          dataIndex: "produceDate",
          key: "produceDate",
          title: "生产日期",
          width: 180,
          showSearch: true,
        },
        {
          dataIndex: "overdue",
          key: "overdue",
          title: "过期日期",
          width: 180,
          showSearch: true,
        },

        {
          dataIndex: "skuQty",
          key: "skuQty",
          title: "物料数量",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "lockQty",
          key: "lockQty",
          title: "锁定数量",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "baseUnit",
          key: "baseUnit",
          title: "物流单位",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "convertQty",
          key: "convertQty",
          title: "统计数量",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "primaryUnit",
          key: "primaryUnit",
          title: "统计单位",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "spInv",
          key: "spInv",
          title: "特殊库存",
          width: 100,
          dictionary: {
            type: 'tag',
            code: 'spInv'
          },
        },
        {
          dataIndex: "invType",
          key: "invType",
          title: "库存类型",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "qualityStatus",
          key: "qualityStatus",
          title: "质量状态",
          width: 100,
          dictionary: {
            type: 'tag',
            code: 'qualityStatus'
          }
        },
        {
          dataIndex: "createBy",
          key: "createBy",
          title: "录入人",
          width: 120,
        },
        {
          dataIndex: "createTime",
          key: "createTime",
          title: "录入时间",
          width: 200,
        },
        {
          dataIndex: "updateBy",
          key: "updateBy",
          title: "修改人",
          width: 120,
        },
        {
          dataIndex: "updateTime",
          key: "updateTime",
          title: "修改时间",
          width: 200,
        },
        {
          dataIndex: "remark",
          key: "remark",
          title: "备注",
          width: 200,
        },
      ];

      /// 数据来源 [模拟]
      const fetch = async (param) => {
        console.log("子表查询" + JSON.stringify(param))
        var response = await getTallyPallet(param);
        // console.log("子表查询response" + JSON.stringify(response))
        return {
          total: response.data.total,
          data: response.data.record,
        };
      };

      /// 删除数据
      const removeMethod = (record) => {
        modal.confirm({
          title: '您是否确定要删除此数据?',
          icon: createVNode(ExclamationCircleOutlined),
          okText: '确定',
          cancelText: '取消',
          onOk() {
            message.loading({
              content: "提交中...",
              key: removeKey
            });
            remove({
              "id": record.id
            }).then((response) => {
              if (response.success) {
                message.success({
                  content: "删除成功",
                  key: removeKey,
                  duration: 1
                }).then(() => {
                  tableChilrenRef.value.reload()
                })
              } else {
                message.error({
                  content: response.msg,
                  key: removeKey,
                  duration: 1
                })
              }
            })
          }
        });
      }

      /// 批量删除
      const removeBatchMethod = (ids) => {
        modal.confirm({
          title: '您是否确定要删除?',
          icon: createVNode(ExclamationCircleOutlined),
          okText: '确定',
          cancelText: '取消',
          onOk() {
            message.loading({
              content: "提交中...",
              key: removeBatchKey
            });
            removeBatch({
              "ids": ids
            }).then((response) => {
              if (response.success) {
                message.success({
                  content: "删除成功",
                  key: removeBatchKey,
                  duration: 1
                }).then(() => {
                  tableChilrenRef.value.reload()
                })
              } else {
                message.error({
                  content: response.msg,
                  key: removeBatchKey,
                  duration: 1
                })
              }
            })
          }
        });
      }

      ///创建理货
      const createTallyTaskMethod = (ids) => {
        modal.confirm({
          title: '您是否确定要创建理货任务?',
          icon: createVNode(ExclamationCircleOutlined),
          okText: '确定',
          cancelText: '取消',
          onOk() {
            message.loading({
              content: "提交中...",
              key: createTallyTaskKey
            });
            removeBatch({
              "ids": ids
            }).then((response) => {
              if (response.success) {
                message.success({
                  content: "创建成功",
                  key: createTallyTaskKey,
                  duration: 1
                }).then(() => {
                  tableChilrenRef.value.reload()
                })
              } else {
                message.error({
                  content: response.msg,
                  key: createTallyTaskKey,
                  duration: 1
                })
              }
            })
          }
        });
      }

      /// 工具栏
      const toolbar = [{
          label: "创建理货",
          event: function() {
            createTallyTaskMethod()
          }
        },
        {
          label: "删除",
          event: function() {
            removeBatchMethod(state.selectedRowKeys)
          },
          type: 'danger'
        },
      ];


      const operate = [{
        label: "删除",
        event: function(record) {
          removeMethod(record)
        }
      }, ];


      const pagination = {
        pageNum: 1,
        pageSize: 20,
        showSizeChanger: true, // 显示可改变每页条数
        pageSizeOptions: ["10", "20", "50", "100"], // 每页条数选项设置
        showTotal: total => `共 ${total} 条记录`, // 显示总条数
        showSizeChange: (current, pageSize) => (this.pageSize = pageSize) // 改变每页条数时更新显示
      }

      const state = reactive({
        selectedRowKeys: [],
        param: {
          // projectId: "",
        },
        visibleEdit: false,
        visibleSave: false,
        visibleInfo: false,
        recordEdit: {},
        recordSave: {},
        recordInfo: {},
      });

      const search = function(value) {
        console.log("子表查询" + JSON.stringify(value))
        console.log(value)
        state.param = value
        tableChilrenRef.value.reload()

      }

      const onSelectChange = selectedRowKeys => {
        state.selectedRowKeys = selectedRowKeys;
      };

      const closeSave = function() {
        state.visibleSave = false
        tableChilrenRef.value.reload()
      }

      const closeEdit = function() {
        state.visibleEdit = false
        tableChilrenRef.value.reload()
      }

      const closeInfo = function() {
        console.log("关闭", state.visibleInfo)
        console.log("关闭state", state)
        state.visibleInfo = false
        tableChilrenRef.value.reload()
      }

      return {
        state,
        fetch,
        search,
        toolbar,
        columns,
        operate,
        pagination,
        onSelectChange,
        closeSave,
        closeEdit,
        closeInfo,
        tableChilrenRef,

      };
    },
  };
</script>
