<template>
  <div class="grid-container">
    <div class="grid-item-1">
      <Navbar
        :title="title"
        :is-go-back="false"
      />
    </div>
    <div
      class="grid-item-2"
      style="margin-bottom: 5px;"
    >
      <div class="btn-group">
        <button
          class="btn btn-primary"
          @click="generate()"
        >
          确定
        </button>
        <button
          class="btn btn-warning"
          @click="back()"
        >
          撤销
        </button>
      </div>
    </div>
    <div class="grid-item-3">
      <DocGenerator
        ref="docGenerator"
        :info-window-no="infoWindowNo"
        :generate-ids="generateIds"
        @show-title="title = $event"
      />
    </div>
  </div>
</template>

<script>
import Common from '../common/Common.js';


import { IFrameUtil } from 'pc-component-v3';

export default {

  components: { },
  data: function () {
    return {
      uuid: '',
      infoWindowNo: '',
      type: '',
      title: '',
      generateIds: [],
    };
  },

  mounted: function () {
    this.uuid = this.$route.params.uuid;
    this.infoWindowNo = this.$route.params.infoWindowNo;
    this.type = this.$route.params.type;
  },

  methods: {

    generate: function () {
      var _self = this;

      var selectedDatas = _self.$refs.docGenerator.getSelectedData();
      if (selectedDatas === undefined || selectedDatas === null || selectedDatas.length === 0) {
        Notify.error('提示', '未选择任何数据。', false);
        return;
      }
      // window.open调用
      let modelData = window.opener.getModelData();
      // 模态框 iframe调用
      //let modelData =  IFrameUtil.getCurdModelData();
      let url = null;

      if (_self.type == 'purchaseRequest') {
        url = 'PurchaseOrderResource/generatePurchaseOrderModelData';
      } else if (_self.type == 'purchaseOrder') {
        url = 'purchaseOrderArrivalResource/generatePurchaseOrderArrivalModelData';
      } else if (_self.type == 'purchaseOrderArrival') {
        url = 'incomingInspectionRequestResource/generateInspectRequestModelData';
      } else if (_self.type == 'inspectionRequest') {
        if (selectedDatas.length > 1) {
          Notify.error('数据选择错误', '只能选择一条报检单明细数据', false);
          return;
        } else {
          url = 'incomingInspectionResource/generateInspectionModelData';
        }
      } else if (_self.type == 'inspectionReject') {
        if (selectedDatas.length > 1) {
          Notify.error('数据选择错误', '只能选择一条检验单数据', false);
          return;
        } else {
          url = 'incomingInspectionRejectResource/generateInspectionRejectModelData';
        }
      } else if (_self.type == 'inspectionStockIn') {
        url = 'stockInResource/generateStockInModelDataByInspection';
      } else if (_self.type == 'purchaseOrderArrivalStockIn') {
        url = 'stockInResource/generateStockInModelDataByArrival';
      } else if (_self.type == 'purchaseStockIn') {
        url = 'StockOutResource/generateMaterialStockOutModelData';
      } else if (_self.type == 'purchaseOrderReturn') {
        url = 'purchaseOrderReturnResource/generatePurchaseOrderReturnModelData';
      } else if (_self.type == 'materialStockOut') {
        url = 'StockOutResource/generateRedStockOutModelData';
      } else if (_self.type == 'redPurchaseStockIn') {
        url = 'stockInResource/generateRedStockInModelData';
      } else if (_self.type == 'stockInPrepareLine') {
        url = 'stockInResource/generateRedStockInPrepareModelData';
      } else if (_self.type == 'generateStockInByOrder') {
        url = 'stockInResource/generateStockInByOrder';
      } else if (_self.type == 'newPurchaseRequest') {
        //仓库模块-采购订单
        url = 'PurchaseOrderResource/generateModelData';
      } else if (_self.type == 'newPurchaseOrder') {
        //仓库模块-采购到货单
        url = 'purchaseOrderArrivalResource/generateModelData';
      } else if (_self.type == 'newArrivalStockIn') {
        //仓库模块-（到货单生成）入库单
        url = 'stockInResource/generateArrivalStockInModelData';
      } else if (_self.type == 'newArrivalInspectionRequest') {
        //仓库模块-来料报检单
        url = 'incomingInspectionRequestResource/generateModelData';
      } else if (_self.type == 'newInspectionRequest') {
        //仓库模块-来料检验单
        if (selectedDatas.length > 1) {
          Notify.error('数据选择错误', '只能选择一条报检单数据', false);
          return;
        } else {
          url = 'incomingInspectionResource/generateModelData';
        }
      } else if (_self.type == 'newInspectionReject') {
        //仓库模块-不良品处理单
        if (selectedDatas.length > 1) {
          Notify.error('数据选择错误', '只能选择一条检验单数据', false);
          return;
        } else {
          url = 'incomingInspectionRejectResource/generateModelData';
        }
      } else if (_self.type == 'newCurrentStock') {
        //仓库模块-（仓库库存生成）出库单
        url = 'StockOutResource/generateModelData';
      } else if (_self.type == 'newRedStockOut') {
        //仓库模块-红字出库单
        url = 'StockOutResource/generateRedModelData';
      } else if (_self.type == 'newPurchaseOrderReturn') {
        //仓库模块-采购退货单
        url = 'purchaseOrderReturnResource/generateModelData';
      } else if (_self.type == 'newStockOut') {
        //仓库模块-红字出库单
        url = 'StockOutResource/generateRedModelData';
      } else if (_self.type == 'newRedStockIn') {
        //仓库模块-红字入库单
        url = 'stockInResource/generateRedModelData';
      } else if (_self.type == 'invStockIn') {
        //仓库模块-（物料批量）入库单
        url = 'stockInResource/generateModelDataByInventory';
      } else if (_self.type == 'orderToIn') {
        //仓库模块-（订单生成）入库单
        url = 'stockInResource/generateOrderToInModelData';
      } else if (_self.type == 'saleOrderToIn') {
        //仓库模块-销售订单生成入库单
        url = 'stockInResource/generateSaleOrderToInModelData';
      } else if (_self.type == 'saleOrderToOut') {
        //仓库模块-销售订单生成出库单
        url = 'StockOutResource/generateSaleOrderToOutModelData';
      } else if (_self.type == 'saleOrderToPrepare') {
        //仓库模块-销售订单生成领料单
        url = 'stockOutPrepareResource/generateSaleOrderToPrepareModelData';
      } else if (_self.type == 'prepareToOut') {
        //仓库模块-领料单生成出库单
        url = 'StockOutResource/generatePrepareToOutModelData';
      } else if (_self.type == 'saleOrderToStockUp') {
        //销售订单生成备货单
        url = 'StockUpResource/generateSaleOrderToStockUpModelData';
      } else if (_self.type == 'applyToStockUp') {
        //布草管理-出库申请单生成仓库备货单
        url = 'StockUpResource/generateApplyToUpModelData';
      } else if (_self.type == 'purchaseOrderToStockOut') {
        //布草管理-采购订单详单生成工厂出库单
        url = 'StockOutResource/generateStockOutByPurchaseOrder';
      } else if (_self.type == 'newAssetCheck') {
        //资产采购单生成验收单
        url = 'assetCheckResource/generateModelData';
      } else if (_self.type == 'newPurchaseBill') {
        //采购单生成采购丁单发票
        url = 'purchaseOrderBillResource/generateModelData';
      } else if (_self.type == 'newPurchaseOrderPayment') {
        //采购订单生成付款单
        url = 'purchaseOrderPaymentResource/generateModelData';
      } else if (_self.type == 'newSaleOrderReceivables') {
        //销售订单生成收款单
        url = 'SaleOrderReceiptResource/generateModelData';
      } else if (_self.type == 'newSaleOrderBill') {
        //销售订单生成销售订单发票
        url = 'saleOrderBillResource/generateModelData';
      } else if (_self.type == 'newSaleOrderReceipt') {
        //销售订单生成收款单
        url = 'SaleOrderReceiptResource/generateModelData';
      } else if (_self.type == 'generatePurchaseRequest') {
        //申购单生成采购订单
        url = 'PurchaseOrderResource/generateModelData';
      } else if (_self.type == 'generatePurchaseOrder') {
        //采购订单生成采购到货
        url = 'PurchaseOrderArrivalResource/generateModelData';
      } else if (_self.type == 'generatePurchaseOrderReturn') {
        //退货单生成换货单
        url = 'PurchaseOrderArrivalResource/generateModelDataReturn';
      }else{
        Notify.error('提示', '不识别的生单地址。' + _self.type, false);
        return;
      }
      let generateDocumentModelData = {
        windowNo: modelData.windowNo,
        tabIndex: modelData.tabIndex,
        modelData: modelData,
        inputDatas: selectedDatas,
      };

      $.ajax({
        url: Common.getApiURL(url),
        type: 'post',
        contentType: 'application/json',
        data: JSON.stringify(generateDocumentModelData),
        beforeSend: function (request) {
          Common.addTokenToRequest(request);
        },
        success: function (modelData) {
          if (modelData != undefined) {
            if (modelData.saveDatas != undefined) {
              modelData.saveDatas.forEach(function (item) {
                if(item.editMode == null){
                  item.editMode = true;
                }
                item.tabIndex = 1;
              });
            }
            IFrameUtil.setCurdModelData(modelData);

            _self.back();
          }
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
          Common.processException(XMLHttpRequest, textStatus, errorThrown);
        },
      });
    },


    isShowVendorDialogA: function (data) {
      var _self = this;
      var messageStr = '';
      data.forEach(function (item, index) {
        if (index == data.length - 1) {
          messageStr += item.name;
        } else {
          messageStr += item.name + '，';
        }
      });
      Notify.show({
        title: '项目确认',
        message: '您选择的请购订单明细中存在不同项目【' + messageStr + '】',
        buttons: [{
          label: '取消',
          action: function (dialogItself) {
            dialogItself.close();
          },
        }],
      });
    },

    isShowVendorDialog: function (data) {
      var _self = this;
      var messageStr = '';
      data.forEach(function (item, index) {
        if (index == data.length - 1) {
          messageStr += item.vendorName;
        } else {
          messageStr += item.vendorName + '，';
        }
      });
      Notify.show({
        title: '供应商确认',
        message: '您选择的请购订单明细中存在不同供应商【' + messageStr + '】，如果点击\\"确定\\"按钮，将从供应商【' + data[data.length - 1].vendorName + '】下订单，否则点击\\"取消\\"按钮',
        buttons: [
          {
            label: '确定',
            cssClass: 'btn-primary',
            action: function (dialogItself) {
              dialogItself.close();
              var url = 'PurchaseOrderResource/generatePurchaseOrderModelData';
              _self.responseReceiveData(_self.obj);
            },
          }, {
            label: '取消',
            action: function (dialogItself) {
              dialogItself.close();
            },
          },
        ],
      });
    },
    /**
         * 返回,关闭模态框
         */
    back: function () {
      IFrameUtil.close();
    },
  },
};


</script>

<style scoped>
    .grid-container {
        display: grid;
        grid-template-columns: 100%;
        grid-template-rows: min-content min-content auto;
        height: calc(100vh - 35px);
        width: 100%;
        padding: 10px;
    }
    
    .grid-item-1 {
        grid-row: 1 / 2;
        grid-column: 1 / 2;
    }
    
    .grid-item-2 {
        grid-row: 2 / 3;
        grid-column: 1 / 2;
    }
    
    .grid-item-3 {
        overflow: auto;
        grid-row: 3/4;
        grid-column: 1 / 2;
        height: 100%;
    }
</style>