<template>
  <a-list-layout
    ref="pageRef"
  >
    <template #search>
      <pro-query :searchParam="searchParam" @on-search="search"></pro-query>
    </template>
      <!-- 搬运类型列表 -->
    <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>
    <save :visible="state.visibleSave" @close="closeSave"></save>
    <edit :visible="state.visibleEdit" @close="closeEdit" :record="state.recordEdit"></edit>
    <info :visible="state.visibleInfo" @close="closeInfo" :record="state.recordInfo"></info>
  </a-list-layout>
</template>

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


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

  export default {
    components: {
      save,
      edit,
      info,
    },
    setup() {

      const tableRef = ref();

      /// 列配置
      const columns = [{
        dataIndex: "index",
        key: "index",
        title: "序号",
        width: 60,
        fixed: 'left',
        align: 'center'
      },
      {
          dataIndex: "deliver",
          key: "deliver",
          title: "搬运类型",
          showSearch:true,
          width: 150,
        },
        {
          dataIndex: "deliverDesc",
          key: "deliverDesc",
          title: "搬运类型描述",
          width: 150,
        },
        {
          dataIndex: "directFlag",
          key: "directFlag",
          title: "方向标志",
          width: 150,
          dictionary: {
            type: 'tag',
            code: 'directFlag'
          }
        },
        {
          dataIndex: "sourceLoc",
          key: "sourceLoc",
          title: "源位置",
          width: 150,
          showSearch:true,
          defaultSortOrder: 'ascend',
          sorter: (a, b) => a.sourceLoc - b.sourceLoc,

        },
        {
          dataIndex: "targetLoc",
          key: "targetLoc",
          title: "目标位置",
          showSearch:true,
          width: 200,
          // defaultSortOrder: 'ascend',
          // sorter: (a, b) => a.targetLoc - b.targetLoc,

        },
        {
          dataIndex: "sourceStactics",
          key: "sourceStactics",
          title: "源策略",
          showSearch:true,
          width: 150,
          // defaultSortOrder: 'ascend',
          // sorter: (a, b) => a.sourceStactics - b.sourceStactics,

        },
        {
          dataIndex: "targetStactics",
          key: "targetStactics",
          title: "目标策略",
          showSearch:true,
          width: 150,
        },
        {
          dataIndex: "pltFlag",
          key: "pltFlag",
          title: "删除托盘标记",
          width: 150,
          dictionary: {
            type: 'tag',
            code: 'pltFlag'
          }
        },
        {
          dataIndex: "priority",
          key: "priority",
          title: "优先级",
          width: 100,
          dictionary: {
            type: 'tag',
            code: 'priority'
          }
        },
        {
          dataIndex: "jobFlag",
          key: "jobFlag",
          title: "产生任务标志",
          width: 150,
          dictionary: {
            type: 'tag',
            code: 'jobFlag'
          }
        },
        {
          dataIndex: "createBy",
          key: "createBy",
          title: "录入人",
          width: 150,
        },
        {
          dataIndex: "updateBy",
          key: "updateBy",
          title: "修改人",
          width: 150,
        },
        {
          dataIndex: "createTime",
          key: "createTime",
          title: "录入日期",
          width: 200,
        },
        {
          dataIndex: "updateTime",
          key: "updateTime",
          title: "修改日期",
          width: 200,
        },
        {
          dataIndex: "remark",
          key: "remark",
          title: "备注",
          width: 100,
        },
      ];

      /// 数据来源 [模拟]
      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 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 = [{
          label: "新增",
          code: 'wms:deliverType:add',
          icon: 'PlusOutlined',
          event: function() {
            state.visibleSave = true
          }
        },
        {
          label: "删除",
          code: 'wms:deliverType:remove',
          icon: 'DeleteOutlined',
          danger: true,
          event: function() {
            removeBatchMethod(state.selectedRowKeys)
          },
          type: 'danger'
        },
      ];

      /// 行操作
      const operate = [{
          label: "修改",
          code: 'wms:deliverType:edit',
          event: function(record) {
            state.visibleEdit = true, state.recordEdit = record
          }
        },
        /* {
          label: "删除",
          isDel: false,
          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,
        visibleInfo: false,
        recordEdit: {},
        recordInfo: {},
      })

      const searchParam = [{
          key: "priority",
          type: "select",
          label: "优先级",
          options: [{
              value: '1',
              text: '中'
            },
            {
              value: '0',
              text: '低',
            }, {
              value: '2',
              text: '高',
            }
          ]
        },

        {
          key: "jobFlag",
          type: "select",
          label: "是否产生任务",
          options: [{
            value: '1',
            text: '是',
          }, {
            value: '0',
            text: '否',
          }]
        },
        {
          key: "deliver",
          type: "input",
          label: "搬运类型"
        },
      ]

      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 closeInfo = function() {
        state.visibleInfo = false
      }

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

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

        search,
        searchParam, // 查询参数

        closeSave,
        closeEdit,
        closeInfo,

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