<template>
  <div class="downLoadOrdersTbRetail">
    <FormLayout
      ref="formLayout"
      :default-column="formLayout.defaultColumn"
      :defaultconfig="formLayout.defaultConfig"
    />
    <businessForm :form-config="formConfig" />
    <businessButton :btn-config="btnConfig" />
  </div>
</template>

<script>
  import businessForm from 'professionalComponents/businessForm';
  import businessButton from 'professionalComponents/businessButton';

  export default {
    components: {
      businessForm,
      businessButton
    },
    props: {},
    data() {
      return {
        vmI18n: window.vmI18n,
        PLATFORM: {
          // 淘宝订单
          T_V_OMSTAOBAOORDER: '194859', // 2
          // 京东订单
          T_OMSJINGDONGORDER: '194865', // 4
          // 淘宝分销订单
          T_V_OMSFENXIAOORDER: '194824', // 3
          // 通用订单
          T_V_OMSSTANDPLATORDER: '194828',
        },
        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.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', `${this.PLATFORM[this.$route.params.tableName]}`);
                    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: 'radio', // 单选框
              label: '订单状态', // 订单状态 前面字段
              width: '24', // 宽度
              value: 'downloadType', // 绑定到formValue的值
              radioChange: () => {
                this.radioChangeHandle();
              }, // 切换时的方法
              // setRequired: "required", //必选标识,值不为required时无标识
              options: [
                {
                  label: '全部', // 全部
                  value: 0,
                },
                {
                  label: '待发货', // 待发货
                  value: 1,
                },
              ]
            },
            {
              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() {
      // this.getShop();
      // this.radioChangeHandle();
      if (this.$route.params.tableName == 'T_OMSJINGDONGORDER') { // 京东订单
        this.formConfig.formData.splice(0, 1);
      } else if (this.$route.params.tableName == 'T_V_OMSSTANDPLATORDER') { // 通用订单
        this.formConfig.formData.splice(0, 1);
      } 
    },
    methods: {
      // formLayout形式获取平台店铺
      async getShop(pageNum = 1, start = 0, range = 10) {
        const query = {
          isdroplistsearch: true,
          refcolid: `${this.PLATFORM[this.$route.params.tableName]}`,
          startindex: (pageNum - 1) * range,
          range,
        };

        const fd = new FormData();
        fd.append('searchdata', JSON.stringify(query));
        const res = await this.service.common.QueryList(fd);
        console.log('getShop::res', res);
        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;
        }
      },
      radioChangeHandle() {
        const formValue = this.formConfig.formValue;
        let formData = this.formConfig.formData;
      },
      // 确定按钮
      async determine() {
        const formValue = this.formConfig.formValue;
        const formLayoutFormValue = this.formLayout.formValue;
        if (!formLayoutFormValue.shops.length) {
          this.$Message.warning('请选择需要下载的店铺'); // 请选择需要下载的店铺
          return;
        }
        let param = {
          SHOPID: Number(formLayoutFormValue.shops[0].ID),
          SOURCECODES: formValue.numIIds,
        };
        if (this.$route.params.tableName == 'T_V_OMSTAOBAOORDER') { // 菜鸟订单下载
          if (formValue.downloadType == '0') {
            if (formValue.startEndTimes.length == 0 || formValue.startEndTimes[0] == '') {
              return this.$Message.warning('请选择平台修改时间');
            }
          } else if (formValue.downloadType == '1') {
            if ((formValue.startEndTimes.length == 0 || formValue.startEndTimes[0] == '') && formValue.numIIds == '') {
              return this.$Message.warning('请选择平台修改时间或者平台单号');
            }
          }
          param.STARTTIME = formValue.startEndTimes.length > 0 && formValue.startEndTimes[0] != '' ? this.$comUtils.dateFormat(formValue.startEndTimes[0], 'yyyy-MM-dd hh:mm:ss') : '';
          param.ENDTIME = formValue.startEndTimes.length > 0 && formValue.startEndTimes[0] != '' ? this.$comUtils.dateFormat(formValue.startEndTimes[1], 'yyyy-MM-dd hh:mm:ss') : '';
          param.ORDERSTATUS = formValue.downloadType;
        } else if (this.$route.params.tableName == 'T_OMSJINGDONGORDER') { // 京东订单下载
          if ((formValue.startEndTimes.length == 0 || formValue.startEndTimes[0] == '') && formValue.numIIds == '') {
            return this.$Message.warning('请选择平台修改时间或者平台单号');
          }
          param.STARTTIME = formValue.startEndTimes.length > 0 && formValue.startEndTimes[0] != '' ? this.$comUtils.dateFormat(formValue.startEndTimes[0], 'yyyy-MM-dd hh:mm:ss') : '';
          param.ENDTIME = formValue.startEndTimes.length > 0 && formValue.startEndTimes[0] != '' ? this.$comUtils.dateFormat(formValue.startEndTimes[1], 'yyyy-MM-dd hh:mm:ss') : '';
        } else if (this.$route.params.tableName == 'T_V_OMSSTANDPLATORDER') { // 通用订单下载
          if ((formValue.startEndTimes.length == 0 || formValue.startEndTimes[0] == '') && formValue.numIIds == '') {
            return this.$Message.warning('请选择平台修改时间或者平台单号');
          }
          param.STARTTIME = formValue.startEndTimes.length > 0 && formValue.startEndTimes[0] != '' ? this.$comUtils.dateFormat(formValue.startEndTimes[0], 'yyyy-MM-dd hh:mm:ss') : '';
          param.ENDTIME = 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 = '';
          if (this.$route.params.tableName == 'T_V_OMSTAOBAOORDER') {
            res = await this.service.commodityCenter.TBOrderManualDownload(param);
          } else if (this.$route.params.tableName == 'T_OMSJINGDONGORDER') {
            res = await this.service.commodityCenter.JDOrderManualDownload(param);
          } else if (this.$route.params.tableName == 'T_V_OMSSTANDPLATORDER') {
            res = await this.service.commodityCenter.TYOrderManualDownload(param);
          }
          console.log('请求下载订单接口::res', res);
          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;
  }

  .downLoadOrdersTbRetail /deep/ .formlayout {
    padding-right: 28px;
  }
  .downLoadOrdersTbRetail .formlayout .formlayout-label {
    padding-right: 2px;
  }
  .downLoadOrdersTbRetail #dropDownSelectFilter {
    width: 340px;
  }
  .downLoadOrdersTbRetail .orderManageEdit .businessForm_a .ark-form-item .ark-input-wrapper {
    line-height: unset;
  }
  .downLoadOrdersTbRetail .buttonBox.right {
    padding-right: unset;
  }
</style>
