<template>
  <div>
    <Spin fix v-if="loading">
      <div class="loader">
        <svg class="circular" viewBox="25 25 50 50">
          <circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="5" stroke-miterlimit="10"></circle>
        </svg>
      </div>
      <div>正在生成补货单，请稍后...</div>
    </Spin>
    <div v-else v-html="message"></div>
    <businessButton :btn-config="btnConfig" />
  </div>
</template>

<script lang="js">
  import businessButton from 'professionalComponents/businessButton';
  import { common, jitDelivery } from '@yunque/service/index';

  export default {
    name: 'JitDelivery_CreatePickList',
    components: {
      businessButton
    },
    props: {
      idArray: { // 获取ID用于多选
        type: Array,
        default: () => []
      },
      itemId: {// 获取当前子表表名
        type: String,
        default: () => ''
      },
      selectRowData: { // 获取选择行的数据
        type: Array,
        default: () => []
      }
    },
    async created() {
      try {
        // 当前只允许选择一行
        const rowData = this.selectRowData[0];
        const jitDeliveryId = rowData.ID.val;
        const deliveryHourseCode = rowData.DELIVERYHOURSECODE.val;
        const shopId = rowData.ORDERSHOPID.refobjid;
        if (shopId < 1) {
          this.message = '该出仓单没有维护“店铺信息”';
          return;
        }
        if (!deliveryHourseCode) {
          this.message = '该出仓单没有维护“JIT仓库”';
          return;
        }

        // 先获取"卖家昵称"
        let shopInfo = await common.objectQuery({
          objid: shopId,
          table: rowData.ORDERSHOPID.reftablename
        });
        let sellerNick = shopInfo.SELLERNICK;

        // 获取子表数据
        const itemQueryParam = {
          table: 'T_OMSJITDELIVERYITEM',
          range: 5000,
          isdroplistsearch: false,
          fixedcolumns: {
            OMSJITDELIVERYID: `=${jitDeliveryId}`,
            SHIPDIFFNUM: '>0'
          }
        };
        let itemRsp = await common.listQuery(itemQueryParam);
        let poCodes = [...new Set(itemRsp.rows.map(p => p.PO.val))].join(',');

        let data = await jitDelivery.createPickList(sellerNick, null, poCodes, deliveryHourseCode, '2');

        if (data.code != 0) {
          this.message = `生成补货单失败，${data.message}`;
        } else {
          this.message = '生成补货单成功，请至PO单或拣货单页面下载';
          this.processedSuccessfully = true;
        }
      } catch (e) {
        console.log(e);
        this.message = '系统出现异常，请联系管理员！';
      } finally {
        this.loading = false;
      }
    },
    data() {
      return {
        loading: true,
        message: '',
        processedSuccessfully: false, // 是否处理成功
        btnConfig: {
          typeAll: 'error', // 按钮统一风格样式
          btnsite: 'right', // 按钮位置 (right , center , left)
          buttons: [
            {
              type: '', // 按钮类型
              text: '确定', // 按钮文本
              icon: '', // 按钮图标
              size: '', // 按钮大小
              disabled: false, // 按钮禁用控制
              btnclick: () => {
                this.closeDialog();
              }, // 按钮点击事件
            }
          ]
        }
      };
    },
    methods: {
      /**
       * 关闭弹窗
       * @param refreshGrid 关闭后是否刷新列表
       */
      closeDialog() {
        this.$emit('closeActionDialog', this.processedSuccessfully);
      }
    }
  };
</script>
