<template>
    <div class="platformInvoiceDownloadDownload">
      <FormLayout
        ref="formLayout"
        :default-column="formLayout.defaultColumn"
        :defaultconfig="formLayout.defaultConfig"
      />
      <businessForm :form-config="formConfig" />
      <businessButton :btn-config="btnConfig" />
    </div>
  </template>
  
  <script>
    const R3 = window.R3;
    const { network, urlSearchParams, store } = R3;
    import businessForm from 'professionalComponents/businessForm';
    import businessButton from 'professionalComponents/businessButton';
  
    export default {
      components: {
        businessForm,
        businessButton
      },
      props: {},
      data() {
        return {
          vmI18n: window.vmI18n,
          selectPlatform: '',
          formLayout: {
            defaultColumn: 1,
            defaultConfig: [
              {
                item: {
                  type: 'DropDownSelectFilter',
                  required: true,
                  label: '店铺',
                  props: {
                    disabled: false,
                    totalRowCount: 100,
                    pageSize: 10,
                    hidecolumns: ['ID'],
                    showColnameKey: 'isak', // 选中展示字段：对应表头需要增加改字段为true（isak：true）
                    AutoData: [],
                    defaultSelected: [],
                    data: {
                      start: 0,
                      totalRowCount: 70,
                      pageSize: 10,
                      tabth: [],
                      row: []
                    },
                  },
                  style: {
                    width: 60,
                    paddingRight: 2
                  },
                  event: {
                    'on-fkrp-selected': (e) => {
                      console.log('on-fkrp-selected::e', e);
                      this.formLayout.defaultConfig[0].item.props.data.row.forEach(item => {
                        if(item.ID.val == e[0].ID){
                          this.selectPlatform = item.PLATFORM.refobjid
                        }
                      })
                      this.formLayout.formValue.shops = e;
                    },
                    'on-clear': () => {
                      console.log('on-clear');
                      this.formLayout.formValue.shops = [];
                    },
                    'on-popper-show': async () => {
                      console.log('on-popper-show');
                      this.getShop();
                    },
                    'on-page-change': (value) => {
                      console.log('on-page-change', value);
                      this.getShop(value);
                    },
                    'on-input-value-change': async (value) => {
                      console.log('on-input-value-change::val', value);
                      let fd = new FormData();
                      fd.append('ak', value);
                      fd.append('colid', `196253`);
                      fd.append('fixedcolumns', JSON.stringify({"PLATFORM":"2,4,86,116,27,57,117,118,119,223"}));
                      const res = await this.service.common.fuzzyquerybyak(fd);
                      console.log('on-input-value-change::fuzzyquerybyak::res', res);
                      if (res.data.code === 0) {
                        this.formLayout.defaultConfig[0].item.props.AutoData = res.data.data;
                      }
                    }
                  }
                }
              },
            ],
            formValue: {
              shops: [],
            }
          },
          formConfig: {
            formValue: {
              shopId: '',
              downloadType: 1, // 订单状态
              numIIds: '', // number
              startEndTimes: [],
            },
            formData: [
              {
                style: 'date',
                type: 'datetimerange', // 日期组件类型,默认为data  (daterange)为双日期区间选择
                value: 'startEndTimes',
                label: '申请时间', // 平台修改时间
                width: '24',
                format: 'yyyy-MM-dd HH:mm:ss', // 格式参照burgeonui
                placeholder: ''
              },
              {
                style: 'input',
                label: '平台单号',
                value: 'numIIds', // 输入框的值
                width: '24',
                icon: '',
                placeholder: '多个平台单号使用英文逗号/空格分隔!',
                ghost: false, // 是否关闭幽灵按钮，默认开启
                inputenter: () => {
                }, // 表单回车事件
                iconclick: () => {
                } // 点击icon图标事件
                // setRequired: "required" //必选标识,值不为required时无标识
              },
            ],
            // 表单非空提示
            ruleValidate: {
              shopId: [{
                required: true,
                message: ' ',
                trigger: 'blur'
              }],
            }
          },
          btnConfig: {
            typeAll: 'error', // 按钮统一风格样式
            btnsite: 'right', // 按钮位置 (right , center , left)
            buttons: [
              {
                type: '', // 按钮类型
                text: window.vmI18n.t('common.cancel'), // 取消 按钮文本
                icon: '', // 按钮图标
                size: '', // 按钮大小
                disabled: false, // 按钮禁用控制
                btnclick: () => {
                  this.$emit('closeActionDialog', false);
                } // 按钮点击事件
              },
              {
                type: '', // 按钮类型
                text: window.vmI18n.t('btn.download'), // 下载 按钮文本
                icon: '', // 按钮图标
                size: '', // 按钮大小
                disabled: false, // 按钮禁用控制
                btnclick: () => {
                  this.determine();
                }, // 按钮点击事件
              }
            ]
          },
        };
      },
      mounted() {
      },
      methods: {
        // formLayout形式获取平台店铺
        async getShop(pageNum = 1, start = 0, range = 10) {
          const query = {
            "table":"T_V_IESSHOP",
            "startindex":(pageNum - 1) * range,
            "fixedcolumns":{"PLATFORM":"2,4,86,116,27,57,117,118,119,223"},
            "column_include_uicontroller":true,
            "isolr":false
          };
  
          const fd = new FormData();
          fd.append('searchdata', JSON.stringify(query));
          const res = await this.service.common.QueryList(fd);
          if (res.data.code == 0) {
            let data = {
              start,
              totalRowCount: res.data.data.totalRowCount,
              pageSize: range,
              tabth: res.data.data.tabth,
              row: res.data.data.row
            };
            this.formLayout.defaultConfig[0].item.props.data = data;
            this.formLayout.defaultConfig[0].item.props.totalRowCount = res.data.data.totalRowCount;
            console.log(this.formLayout.defaultConfig[0].item.props.data,row[1].PLATFORM.refobjid)
          }
        },
        // 确定按钮
        async determine() {
          const formValue = this.formConfig.formValue;
          const formLayoutFormValue = this.formLayout.formValue;
          if (!formLayoutFormValue.shops.length) {
            this.$Message.warning('请选择需要下载的店铺'); // 请选择需要下载的店铺
            return;
          }
          if(this.selectPlatform == 2 && !formValue.numIIds){
            this.$Message.warning('请填写平台单号'); // 请选择需要下载的店铺
            return;
          }
          let param = {
            iesShopIds: [Number(formLayoutFormValue.shops[0].ID)],
            tid: formValue.numIIds,
            applyStartTime:formValue.startEndTimes.length > 0 && formValue.startEndTimes[0] != '' ? this.$comUtils.dateFormat(formValue.startEndTimes[0], 'yyyy-MM-dd hh:mm:ss') : '',
            applyEndTime:formValue.startEndTimes.length > 0 && formValue.startEndTimes[0] != '' ? this.$comUtils.dateFormat(formValue.startEndTimes[1], 'yyyy-MM-dd hh:mm:ss') : '',
          };
          console.log(param)
  
          // 请求下载订单接口
          try {
            let res = '';
            res = await this.service.commodityCenter.InvoiceManualDownLoad(param);
            if (res.code === 0) {
              this.$Message.success(res.message || '下载成功!');
              // 成功关闭弹框
              this.$emit('closeActionDialog', false);
            } else {
              this.$Message.error(res.message || '下载失败!');
            }
          } catch (e) {
            console.log(e);
            this.$Message.error('系统异常');
          }
        }
      }
    };
  </script>
  
  <style>
    @import "~@yunque/css/modal/interfacePlatform/downLoadAll.less";
  
    .ark-col.ark-col-span-24:last-child {
      margin-top: 10px;
    }
  
    .platformInvoiceDownloadDownload /deep/ .formlayout {
      padding-right: 28px;
    }
    .platformInvoiceDownloadDownload .formlayout .formlayout-label {
      padding-right: 2px;
    }
    .platformInvoiceDownloadDownload #dropDownSelectFilter {
      width: 340px;
    }
    .platformInvoiceDownloadDownload .orderManageEdit .businessForm_a .ark-form-item .ark-input-wrapper {
      line-height: unset;
    }
    .platformInvoiceDownloadDownload .buttonBox.right {
      padding-right: unset;
    }
    .platformInvoiceDownloadDownload .orderManageEdit .businessForm_a .ark-input-innerWrap {
      top: 0px;
    }
  </style>
  