<template>
  <a-list-layout ref="pageRef">
    <pro-table ref="tableRef" :fetch="fetch" :columns="columns" :toolbar="toolbar" :operate="operate"
      :param="state.param" :pagination="pagination"
      :row-selection="{ selectedRowKeys: state.selectedRowKeys, onChange: onSelectChange }">
    </pro-table>
  </a-list-layout>
</template>

<script>
  import {
    message,
    Modal as modal
  } from '@hwork/ant-design-vue';
  import {
    ExclamationCircleOutlined
  } from '@ant-design/icons-vue';
  import {
    page,
    remove,
    cancel,
    removeBatch
  } from "@/api/wms/outbound";
  import {
    reactive,
    createVNode,
    ref
  } from 'vue';

  const removeKey = "remove";
  const removeBatchKey = "removeBatch";
  const cancelKey = "cancel";

  export default {
    components: {
    },
    setup() {

      const tableRef = ref();

      /// 列配置
      const columns = [{
        dataIndex: "index",
        key: "index",
        title: "序号",
        width: 60,
        fixed: 'left',
        align: 'center'
      },
      {
          dataIndex: "outboundNo",
          key: "outboundNo",
          title: "需求编号",
          width: 180,
          showSearch: true,
        },
        {
          dataIndex: "outboundStatus",
          key: "outboundStatus",
          title: "状态",
          width: 100,
          showSearch: true,
        },
        {
          dataIndex: "houseCode",
          key: "houseCode",
          title: "仓库编号",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "palletCode",
          key: "palletCode",
          title: "托盘编号",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "sourceLoc",
          key: "sourceLoc",
          title: "起始地址",
          width: 100,
          showSearch: true,
        },
        {
          dataIndex: "sourceChannelCode",
          key: "sourceChannelCode",
          title: "起始通道编号",
          width: 150,
          showSearch: true,
        },
        {
          dataIndex: "sourceLocParent",
          key: "sourceLocParent",
          title: "源位置父节点",
          width: 140,
          showSearch: true,
        },
        {
          dataIndex: "targetLoc",
          key: "targetLoc",
          title: "目标位置",
          width: 100,
          showSearch: true,
        },
        {
          dataIndex: "targetChannelCode",
          key: "targetChannelCode",
          title: "目标通道编号",
          width: 100,
          showSearch: true,
        },
        {
          dataIndex: "targetLocParent",
          key: "targetLocParent",
          title: "目标位置父节点",
          width: 180,
          showSearch: true,
        },
        {
          dataIndex: "taskNo",
          key: "taskNo",
          title: "任务号",
          width: 180,
          showSearch: true,
        },
        {
          dataIndex: "directFlag",
          key: "directFlag",
          title: "直接标记",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "taskType",
          key: "taskType",
          title: "任务类型",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "formNo",
          key: "formNo",
          title: "表单编号",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "formType",
          key: "formType",
          title: "表单类型",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "businessNo",
          key: "businessNo",
          title: "业务编号",
          width: 120,
          showSearch: true,
        },
        {
          dataIndex: "businessType",
          key: "businessType",
          title: "业务类型",
          width: 100,
          showSearch:true,
        },
        {
          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) => {
        var response = await page(param);
        return {
          total: response.data.total,
          data: response.data.record,
        };
      };

      /// 删除配置
      const removeMethod = (record) => {
        message.loading({
          content: "提交中...",
          key: removeKey
        });
        remove({
          "id": record.id
        }).then((response) => {
          if (response.success) {
            message.success({
              content: "删除成功",
              key: removeKey,
              duration: 1
            })
            tableRef.value.reload()
          } else {
            message.error({
              content: "删除失败",
              key: removeKey,
              duration: 1
            })
          }
        })
      }

    // 取消
    const cancelMethod = (record) => {
      modal.confirm({
        title: '您是否确认取消该出库需求?',
        icon: createVNode(ExclamationCircleOutlined),
        okText: '确定',
        cancelText: '取消',
        onOk() {
          message.loading({
            content: "取消中...",
            key: cancelKey
          });
          cancel({
            'id': record.id
          }).then((response) => {
            if (response.success) {
              message.success({
                content: "取消成功",
                key: cancelKey,
                duration: 1
              })
              tableRef.value.reload()
            } else {
              message.destroy()
              message.error({
                message: '需求取消失败',
                description: response.msg
              })
            }
          })
        }
      });
    }


      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
                })
                tableRef.value.reload()
              } else {
                message.error({
                  content: "删除失败",
                  key: removeBatchKey,
                  duration: 1
                })
              }
            })
          }
        });
      }

      /// 工具栏
      const toolbar = [];

      /// 行操作
      const operate = [{
          label: "取消",
          event: function(record) {
            cancelMethod(record)
          }
        },
        {
          label: "删除",
          isDel: true,
          delEvent: 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) // 改变每页条数时更新显示
      }

      /// 外置参数 - 当参数改变时, 重新触发 fetch 函数
      const state = reactive({
        selectedRowKeys: [],
        param: {},
        visibleSave: false,
        visibleEdit: false,
        recordEdit: {},
        recordInfo: {},
      })

      //界面头部-查询框
      const searchParam = [
      ]

      const search = function(value) {
        state.param = value
      }

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

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

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

      return {
        state, // 状态共享
        fetch, // 数据回调
        toolbar, // 工具栏
        columns, // 列配置
        operate, // 行操作
        pagination, // 分页配置

        search,
        searchParam, // 查询参数

        closeSave,
        closeEdit,

        onSelectChange,
        tableRef
      };
    },
  };
</script>
