<template>
  <a-modal :open="visible" okText="提交" @ok="submit" @cancel="cancel" :width='1100' wrapClassName="full-modal">
    <div>
      <page-layout>
        <a-row :gutter="[0, 0]">
          <a-col :span="24">
            <a-card>
              <pro-query :searchParam="searchParam" @on-search="search"></pro-query>
              <pro-table  visible="visible" ref="tableRef" rowKey="id" :fetch="fetch"
                :columns="columns" :toolbar="toolbar" :param="state.param" :pagination="pagination"
                :row-selection="{ selectedRowKeys: state.selectedRowKeys, onChange: onSelectChange }">
              </pro-table>
            </a-card>
          </a-col>
        </a-row>
      </page-layout>
    </div>
  </a-modal>
</template>
<script>
  import {
    message,
    Modal as modal,
  } from '@hwork/ant-design-vue';

  import {
    defineComponent,
    reactive,
    ref,
    toRaw,
    watch,
    createVNode
  } from "vue";

  import {
    ExclamationCircleOutlined
  } from '@ant-design/icons-vue';

  import {
    remove,
    removeBatch
  } from "@/api/wms/pick";

  import {
    page,
  } from "@/api/wms/sku";

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

  export default defineComponent({
    props: {
      visible: {
        type: Boolean,

      },
      record: {

        type: Object,
      }
    },
    components: {},

    emit: ["close", "addMaterial"],

    setup(props, context) {
      const tableRef = ref();

      const formRef = ref();

      const formState = reactive({});

      watch(props, (props) => {});

      const columns = [{
        dataIndex: "index",
        key: "index",
        title: "序号",
        width: 60,
        fixed: 'left',
        align: 'center'
      },
      {
          dataIndex: "skuCode",
          key: "skuCode",
          title: "物料编号",
          width: 120,
          defaultSortOrder: 'ascend',
          sorter: (a, b) => a.skuCode - b.skuCode,
           showSearch : true
        },
        {
          dataIndex: "skuName",
          key: "skuName",
          title: "物料名称",
          width: 100,
        },
        {
          dataIndex: "ext1",
          key: "ext1",
          title: "颜色",
          width: 100,
        },
        {
          dataIndex: "ext2",
          key: "ext2",
          title: "转速",
          width: 120,
          defaultSortOrder: 'ascend',
          sorter: (a, b) => a.ext2 - b.ext2,
        },
        {
          dataIndex: "ext3",
          key: "ext3",
          title: "配置",
          width: 120,
          defaultSortOrder: 'ascend',
          sorter: (a, b) => a.ext3 - b.ext3,
        },
       /* {
          dataIndex: "barCode",
          key: "barCode",
          title: "条码",
          width: 250,
        },
        {
          dataIndex: "abc",
          key: "abc",
          title: "ABC分类",
          width: 100,
          dictionary: {
            type: 'tag',
            code: 'abc'
          }, */
       // },
        {
          dataIndex: "categoryCode",
          key: "categoryCode",
          title: "种类编号",
          width: 100,
        },
        {
          dataIndex: "categoryName",
          key: "categoryName",
          title: "种类名称",
          width: 100,
        },
        {
          dataIndex: "baseUnit",
          key: "baseUnit",
          title: "库存单位",
          width: 100,
        },
        {
          dataIndex: "primaryUnit",
          key: "primaryUnit",
          title: "换算单位",
          width: 100,
        },
        {
          dataIndex: "rate",
          key: "rate",
          title: "换算率",
          type: "number",
          width: 100,
          defaultSortOrder: 'ascend',
          sorter: (a, b) => a.rate - b.rate,
        },

        {
          dataIndex: "fullQty",
          key: "fullQty",
          title: "满盘数量",
          type: "number",
          width: 120,
          defaultSortOrder: 'ascend',
          sorter: (a, b) => a.fullQty - b.fullQty,
        },
        {
          dataIndex: "expire",
          key: "expire",
          title: "有效期",
          width: 100,
          defaultSortOrder: 'ascend',
          sorter: (a, b) => a.expire - b.expire,
        },
      ];

      /// 数据来源 [模拟]
      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: response.msg,
                  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 = [

      ];


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

      const state = reactive({
        selectedRowKeys: [],
        selectedRows: [],
        param: {},
        // recordEdit: {},

      });

      const searchParam = [{
        key: "skuCode",
        type: "input",
        label: "物料编号"
      }, ]

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

      const formRules = {};

      const editKey = "add";




      const submit = (e) => {
        console.log("submit", e)
        console.log(state.selectedRows)
        if (state.selectedRows.length == 0) {
          message.error({
            content: "请先选择需要添加的物料信息",
            duration: 1
          });
          return;
        }
        console.log("state.selectedRows", state.selectedRows)
        context.emit("addMaterial", state.selectedRows);
        context.emit("close", false);
      }

      const cancel = (e) => {
        // formRef.value.resetFields();
        context.emit("close", false);
      };

      const onSelectChange = (selectedRowKeys, selectedRows) => {
        console.log('selectedRowKeys changed: ', selectedRowKeys);
        console.log('selectedRows changed: ', selectedRows);
        console.log('selectedRows changed: ', ...selectedRows);
        state.selectedRows = selectedRows;
        state.selectedRowKeys = selectedRowKeys;
      };


      const closeInfo = function() {
        state.visibleInfo = false
        tableRef.value.reload()
      }

      return {


        submit,
        cancel,
        formRef,
        formState,
        formRules,
        state,
        fetch,
        toolbar,
        columns,
        pagination,
        onSelectChange,
        search,
        // closeEdit,
        tableRef,
        searchParam,

        labelCol: {
          span: 8,

        },
        wrapperCol: {
          span: 12,
        },
      };
    },

  });
</script>
/* <style>
  .full-modal {
    .ant-modal {
      max-width: 80%;
      max-height: 70%;
      box-align: center;
      /* top: 0; */
      /* padding-bottom: 0; */
      /* margin: 0; */
    }

    .ant-modal-content {
      display: flex;
      flex-direction: column;
      height: calc(89vh);
    }

    .ant-modal-body {
      flex: 1;
    }
  }
</style> */
