<template>
  <div>
    <page-layout>
      <a-row :gutter="[10, 10]">
        <a-col :span="24" style="height: 100%;">
          <a-card>
            <pro-query :searchParam="searchParam" @on-search="search"></pro-query>
            <!-- 列表 -->
            <pro-table rowKey="id" 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-card>
        </a-col>
        <a-col :span="24" style="height: 50%;">
          <a-card>
            <checkDetail :visible="state.visibleDetail" :record="state.recordDetail"></checkDetail>
          </a-card>
        </a-col>
      </a-row>
    </page-layout>
    <addCheckDetail :visible="state.visibleSave" @close="closeSave" :record="state.currentCheck"></addCheckDetail>
    <edit :visible="state.visibleEdit" @close="closeEdit" :record="state.recordEdit"></edit>
  </div>
</template>
<script>
  import addCheckDetail from "./addCheckDetail/addCheckDetail";
  // import save from "./modal/save";
  import edit from "./modal/edit";
  import data from "./modal/checkDetail";
  import {
    message,
    modal
  } from '@hwork/ant-design-vue';
  import {
    ExclamationCircleOutlined
  } from '@ant-design/icons-vue';
  import {
    page,
    remove,
    removeBatch
  } from "@/api/wms/check";
  import {
    reactive,
    createVNode,
    ref
  } from 'vue';
  const removeKey = "remove";
  const removeBatchKey = "removeBatch";

  export default {
    components: {
      addCheckDetail,
      edit,
      checkDetail: data,
    },
    setup() {

      const tableRef = ref()

      const columns = [{
          dataIndex: "invoiceCode",
          key: "invoiceCode",
          title: "单据号",
          showSearch:true,
          width: 200,
        },
        {
          dataIndex: "status",
          key: "status",
          title: "状态",
          width: 100,
          // filters: [{
          //   text: 'Over',
          //   value: 'Over',
          // }]
           // dictionary: {
           //   type: 'tag',
           //   code: 'status'
           // },
        },
        {
          dataIndex: "beginDate",
          key: "beginDate",
          title: "开始时间",
          width: 200,
        },
        {
          dataIndex: "endDate",
          key: "endDate",
          title: "结束时间",
          width: 200,
        },
        {
          dataIndex: "createBy",
          key: "createBy",
          title: "录入人",
          width: 100,
        },
        {
          dataIndex: "createTime",
          key: "createTime",
          title: "录入时间",
          width: 200,
        },
        {
          dataIndex: "updateBy",
          key: "updateBy",
          title: "修改人",
          width: 100,
        },
        {
          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) => {
        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(() => {
                  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
                }).then(() => {
                  tableRef.value.reload()
                })
              } else {
                message.error({
                  content: "删除失败",
                  key: removeBatchKey,
                  duration: 1
                })
              }
            })
          }
        });
      }


      /// 工具栏
      const toolbar = [{
          label: "新增",
          code: "wms:checkBill:add",
          event: function() {
            state.currentCheck = {}
            state.visibleSave = true
          }
        },
        {
          label: "删除",
          code: "wms:checkBill:removeBatch",
          event: function() {
            removeBatchMethod(state.selectedRowKeys)
          },
          type: 'danger'
        },
      ];

      /// 行操作
      const operate = [{
          label: "查看",
          code: "wms:checkBill:look",
          event: function(record) {
            console.log("record",record)
            state.visibleDetail = true, state.recordDetail = record
          }
        },
        {
          label: "修改",
          code: "wms:checkBill:edit",
          event: function(record) {
            console.log("这里")
            state.visibleEdit = true, state.recordEdit = record
          }
        },
        {
          label: "删除",
          code: "wms:checkBill:remove",
          event: function(record) {
            removeMethod(record)
          }
        },
        // {
        //   label: "新增明细",
        //   event: function(record) {
        //     state.visibleSave=true, state.recordSave = record
        //   }
        // },
      ];



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

      const state = reactive({
        selectedRowKeys: [],
        param: {},
        visibleEdit: false,
        visibleSave: false,
        visibleDetail: false,
        recordDetail: {},
        recordEdit: {},
        currentCheck: {},
      });

      const searchParam = [{
          key: "invoiceCode",
          type: "input",
          label: "单据号"
        },
        /* {
           key: "extCode",
           type: "input",
           label: "外部单据号"
         }, */
      ]

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

      }

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

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

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


      return {
        state,
        fetch,
        search,
        toolbar,
        columns,
        operate,
        closeSave,
        closeEdit,
        pagination,
        searchParam,
        onSelectChange,
        tableRef
      };
    },
  };
</script>
