<template>
  <div>
    <a-col>
      <pro-query v-if="visible" :searchParam="searchParam" @on-search="search"></pro-query>
    </a-col>
    <pro-table v-if="visible" ref="tableChilrenRef" rowKey="id" :fetch="fetch" :columns="columns" :toolbar="toolbar"
      :operate="operate" :param="state.param" :pagination="pagination" :model="formState" @cancel="cancel"
      :row-selection="{ selectedRowKeys: state.selectedRowKeys, onChange: onSelectChange }">
    </pro-table>
    <a-empty v-else />
    <!-- <save :visible="state.visibleSave" @close="closeSave" :record="state.recordSave"></save> -->
    <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 './inBillDetail/edit.vue';
  import {
    message,
    Modal as modal
  } from '@hwork/ant-design-vue';
  import {
    ExclamationCircleOutlined
  } from '@ant-design/icons-vue';
  import {
    page,
    remove,
    removeBatch,
    createInBillDetails
  } from "@/api/wms/inBillDetail";
  import {
    reactive,
    createVNode,
    watch,
    ref,
    toRaw
  } from 'vue';

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

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

    setup(props) {

      const tableChilrenRef = ref()
      const formState = reactive({
        invoiceCode: ''
      });

      const cancel = (e) => {};

      watch(props, (props) => {
        state.param.invoiceCode = props.record.invoiceCode;
        state.inBill = props.record; //主表信息
        console.log("子表watch" + JSON.stringify(props))
        console.log("state.inBills" + JSON.stringify(state.inBill))
      })

      const columns = [{
        dataIndex: "index",
        key: "index",
        title: "序号",
        width: 60,
        fixed: 'left',
        align: 'center'
      },
      {
          dataIndex: "invoiceCode",
          key: "invoiceCode",
          title: "单据号",
          width: 200,
          sorter: true,
          showSearch: true,
        },
        {
          dataIndex: "itemCode",
          key: "itemCode",
          title: "单据明细号",
          width: 200,
          sorter: true,
          showSearch: true,
        },
        {
          dataIndex: "skuCode",
          key: "skuCode",
          title: "SKU编号",
          width: 120,
          sorter: true,
          showSearch: true,
        },
        {
          dataIndex: "skuName",
          key: "skuName",
          title: "SKU名称",
          width: 200,
          sorter: true,
        },
        {
          dataIndex: "skuType",
          key: "skuType",
          title: "SKU类别",
          width: 150,
          sorter: true,
          showSearch: true,
        },
        {
          dataIndex: "status",
          key: "status",
          title: "明细状态",
          width: 100,
          showSearch: true,
          dictionary: {
            type: 'tag',
            code: 'inBillDetailStatus'
          },
        },
        {
          dataIndex: "planQty",
          key: "planQty",
          title: "计划数量",
          width: 130,
          sorter: true,
        },
        {
          dataIndex: "lockQty",
          key: "lockQty",
          title: "已组盘数量",
          width: 130,
          sorter: true,
        },
        {
          dataIndex: "actQty",
          key: "actQty",
          title: "实际入库数量",
          width: 150,
          sorter: true,
        },
        {
          dataIndex: "batch",
          key: "batch",
          title: "批次",
          width: 100,
          sorter: true,
          showSearch: true,
        },
        {
          dataIndex: "qualityStatus",
          key: "qualityStatus",
          title: "质量状态",
          width: 140,
          sorter: true,
          showSearch: true,
          dictionary: {
            type: 'tag',
            code: 'qualityStatus'
          },
        },
        {
          dataIndex: "master",
          key: "master",
          title: "货主",
          width: 100,
          sorter: true,
          showSearch: true,
        },
        {
          dataIndex: "producer",
          key: "producer",
          title: "厂家",
          width: 100,
          sorter: true,
          showSearch: true,
        },
        {
          dataIndex: "customer",
          key: "customer",
          title: "客户",
          width: 100,
          sorter: true,
          showSearch: true,
        },
        /* {
          dataIndex: "produceDate",
          key: "produceDate",
          title: "生产日期",
          width: 200,
          sorter: true,
          showSearch: true,
        },
        {
          dataIndex: "overdue",
          key: "overdue",
          title: "到期日期",
          width: 200,
          sorter: true,
          showSearch: true,
        },

        {
          dataIndex: "submitQty",
          key: "submitQty",
          title: "提交过账数量",
          width: 120,
          sorter: true,
          showSearch: true,
        },
        {
          dataIndex: "convertPlanQty",
          key: "convertPlanQty",
          title: "换算计划数量",
          width: 120,
          sorter: true,
          showSearch: true,
        },
        {
          dataIndex: "convertActQty",
          key: "convertActQty",
          title: "换算实际数量",
          width: 120,
        }, */
        /* {
          dataIndex: "baseUnit",
          key: "baseUnit",
          title: "物流单位",
          width: 100,
          sorter: true,
          showSearch: true,
        },
        {
          dataIndex: "primaryUnit",
          key: "primaryUnit",
          title: "换算单位",
          width: 100,
          sorter: true,
          showSearch: true,
        },
        {
          dataIndex: "spInv",
          key: "spInv",
          title: "特殊库存",
          width: 100,
          sorter: true,
          showSearch: true,
          dictionary: {
            type: 'tag',
            code: 'spInv'
          },
        },
        {
          dataIndex: "invType",
          key: "invType",
          title: "库存类型",
          width: 100,
          sorter: true,
          showSearch: true,
        }, */

        {
          dataIndex: "remark",
          key: "remark",
          title: "备注",
          width: 100,
        },
        {
          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,
        },
      ];

      /// 数据来源 [模拟]
      const fetch = async (param) => {
        console.log("子表查询" + JSON.stringify(param))
        var response = await page(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: "删除失败",
                  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: "删除失败",
                  key: removeBatchKey,
                  duration: 1
                })
              }
            })
          }
        });
      }

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

      /// 行操作
      const operate = [{
          label: "修改",
          code: "wms:inBillDetail:edit",
          event: function(record) {
            state.visibleEdit = true, state.recordEdit = record
          }
        },
        {
          label: "删除",
          code: "wms:inBillDetail:remove",
          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: {},
        visibleEdit: false,
        visibleSave: false,
        recordEdit: {},
        recordSave: {},
        currentBill: {}
      });
      const searchParam = [{
          key: "invoiceCode",
          type: "input",
          label: "单据号"
        }, {
          key: "itemCode",
          type: "input",
          label: "单据明细号"
        },
        {
          key: "status",
          type: "select",
          label: "明细状态",
          options: [{
              value: 'New',
              text: '新增',
            }, {
              value: 'Doing',
              text: '执行中',
            },
            {
              value: 'Pause',
              text: '暂停',
            },
            {
              value: 'Cancel',
              text: '取消',
            },
            {
              value: 'Over',
              text: '执行完成',
            },
            {
              value: 'Closed',
              text: '关闭',
            },
          ]
        },
      ]
      const search = function(value) {
        console.log("子表查询" + JSON.stringify(value))
        console.log(value)
        // value.itemCode = props.record.itemCode
        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()
      }

      return {
        state,
        fetch,
        search,
        formState,
        cancel,
        searchParam,
        toolbar,
        columns,
        operate,
        pagination,
        onSelectChange,
        closeSave,
        closeEdit,

        tableChilrenRef
      };
    },
  };
</script>
