<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>
            <inBillDetail :visible="state.visibleDetail" :record="state.recordDetail"></inBillDetail>
          </a-card>
        </a-col>
      </a-row>
    </page-layout>
    <addInBillDetail :visible="state.visibleSave" @close="closeSave" :record="state.currentBill"></addInBillDetail>
    <edit :visible="state.visibleEdit" @close="closeEdit" :record="state.recordEdit"></edit>
  </div>
</template>
<script>
  import addInBillDetail from './addInBillDetail/addInBillDetail.vue';
  import edit from './modal/edit.vue';
  import data from './modal/inBillDetail.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/inBill";
  import {
    reactive,
    createVNode,
    ref
  } from 'vue';
  const removeKey = "remove";
  const removeBatchKey = "removeBatch";

  export default {
    components: {
      addInBillDetail,
      edit,
      inBillDetail: data,
    },
    setup() {

      const tableRef = ref()

      const columns = [{
        dataIndex: "index",
        key: "index",
        title: "序号",
        width: 60,
        fixed: 'left',
        align: 'center'
      },
      {
          dataIndex: "invoiceCode",
          key: "invoiceCode",
          title: "单据号",
          width: 180,
          showSearch: true
        },
        {
          dataIndex: "extCode",
          key: "extCode",
          title: "外部单据号",
          width: 180,
          showSearch: true
        },
        {
          dataIndex: "businessType",
          key: "businessType",
          title: "业务类型",
          width: 150,
          dictionary: {
            type: 'tag',
            code: 'inOrderTypes'
          },
        },
        {
          dataIndex: "status",
          key: "status",
          title: "单据状态",
          width: 100,
          showSearch: true,
          dictionary: {
            type: 'tag',
            code: 'inBillStatus'
          },
        },
        {
          dataIndex: "createBy",
          key: "createBy",
          showSearch: true,
          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: "deliver",
          key: "deliver",
          title: "搬运类型",
          width: 100,
          dictionary: {
            type: 'tag',
            code: 'deliver'
          },
        }, */
        {
          dataIndex: "platform",
          key: "platform",
          title: "卸货站台",
          width: 100,
          showSearch: true,
        },
        {
          dataIndex: "truckCode",
          key: "truckCode",
          title: "车牌号",
          width: 100,
        },
        {
          dataIndex: "driver",
          key: "driver",
          title: "司机",
          width: 100,
        },
        {
          dataIndex: "telephone",
          key: "telephone",
          title: "联系电话",
          width: 200,
        },
        {
          dataIndex: "customer",
          key: "customer",
          title: "客户",
          width: 100,
          showSearch: true,
        },
        {
          dataIndex: "beginDate",
          key: "beginDate",
          title: "开始时间",
          width: 200,
        },
        {
          dataIndex: "endDate",
          key: "endDate",
          title: "结束时间",
          width: 200,
        },
        {
          dataIndex: "remark",
          key: "remark",
          title: "备注",
          width: 200,
          showSearch: true,
        },
      ];



      /// 数据来源 [模拟]
      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:inBill:add",
          event: function() {
            state.currentBill = {}
            state.visibleSave = true
          }
        },
        {
          label: "删除",
          code: "wms:inBill:removeBatch",
          event: function() {
            removeBatchMethod(state.selectedRowKeys)
          },
          type: 'danger'
        },
        {
          label: "取消",
          code: "wms:inBill:cancel",
          event: function() {
            state.visibleSave = false
          }
        },
        {
          label: "关闭",
          code: "wms:inBill:over",
          event: function() {
            state.visibleSave = false
          }
        },
      ];

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



      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: {},
        visibleEdit: false,
        visibleSave: false,
        visibleDetail: false,
        recordDetail: {},
        recordEdit: {},
        currentBill: {},
      });

      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>
