<template>
  <div class="orderTable">
    <Table
      ref="currentRowTable"
      :height="tableName=='T_V_OMSONLINEORDER'||tableName=='T_V_IESNOGROUPSKU'?tableHeight:'400'"
      :columns="tableHead"
      :data="offerInfo"
      :highlight-row="highlightRow"
      size="small"
      @on-current-change="selectData"
      @on-select="onSelect"
      @on-select-cancel="onSelectCancel"
      @on-select-all="onSelectAll"
      @on-select-all-cancel="onSelectAllCancel"
    />
    <div class="pageContent">
      <Page
        size="small"
        :current="currentPage"
        show-elevator
        :total="total"
        show-total
        show-sizer
        @on-change="changePageOfferInfo"
        @on-page-size-change="changeSizeOfferInfo"
      />
    </div>
  </div>
</template>
<script>
  import cloneDeep from 'lodash/cloneDeep'
  const R3 = window.R3;

  const { network, urlSearchParams, store } = R3;
  // import smart from "address-smart-parse";
  export default {
    props: {
      highlightRow: {
        type: Boolean,
        default: ()=> false,
      },
      fixedcolumns: {
        type: Object,
        default: ()=> {},
      },
      tableHeight: {
        type: String,
        default: () => '',
      },
      tableHead: {
        type: Array,
        default: () => [],
      },
      tableName: {
        type: String,
        default: () => '',
      },
      searchcols: {
        type: [String, Array],
        default: () => '',
      },
      pageMsgId: {
        type: String,
        default: () => '',
      },
      isdroplistsearch: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        offerInfo: [], // 优惠信息关联表
        // 优惠信息page
        totalOfferInfo: 0, // 总条数
        currentPageOfferInfo: 0, // 页码
        pageSizeOfferInfo: 10, // 每页的条数
        currentPage: 1,
        total: 0,
      };
    },
    created() {},
    mounted() {
      setTimeout(() => {
        // 弹窗内的不能直接请求
        if(!document.querySelector('.ark-modal-body') || this.tableName !== "T_V_OMSONLINEORDER"){
          this.getInfoData();
        }
      }, 0);
    },
    methods: {
      handleClearCurrentRow() { // 清除当前单选项
        this.$refs.currentRowTable.clearCurrentRow();
      },

      async getTid() {
        return network.post('/p/cs/order/item/queryTid', {
          OMSONLINEORDERID: this.pageMsgId
        }).then(res => {
          if(res.data.code === 0) {
            return res.data.data.map(item => `=${item}`)
          }
        })
      },

      async getInfoData() {
        let virtual;
        let data;
        if (this.tableName == 'T_V_OMSONLINEORDER' || this.tableName == 'T_V_IESNOGROUPSKU') {
          virtual = {
            fixedcolumns: this.fixedcolumns ? this.fixedcolumns : {},
            isdroplistsearch: this.isdroplistsearch,
            range: this.pageSizeOfferInfo,
            startindex: this.currentPageOfferInfo,
            table: this.tableName,
          };
          if(this.searchcols) {
            virtual.fkdrplist = {
              searchcols: this.searchcols,
            }
          }
          // eslint-disable-next-line array-callback-return
          const checked = Object.keys(virtual.fixedcolumns).some((key)=>{
            if(key !== 'OrderStatus'){
                return virtual.fixedcolumns[key].length !== 0
              }
          })
          
          if (
            virtual.fixedcolumns && !checked) {
            this.$Message.warning('请输入查询条件');
            return;
          }

          network.post('/p/cs/list/query', virtual).then((res) => {
            this.total = res.data.total;
            this._originRowsData = cloneDeep(res.data.rows)

            data = [];
            for (let i = 0; i < res.data.rows.length; i++) {
              const obj = {}
              for (let j = 0; j < Object.keys(res.data.rows[i]).length; j++) {
                let keyName = Object.keys(res.data.rows[i])[j];
                const cellInfo = res.data.rows[i][keyName];
                // 如果是平台
                if (keyName === 'PLATFORM') {
                  obj[`${keyName}_ID`] = cellInfo.refobjid;
                }
                obj[keyName] = cellInfo.val;
              }
              data.push(obj);
            }
            this.offerInfo = data;
          });
        } else {
          virtual = {
            table: this.tableName,
            objId: this.pageMsgId,
            isdroplistsearch: this.isdroplistsearch,
            range: this.pageSizeOfferInfo,
            startindex: this.currentPageOfferInfo,
            orderby: this.tableName == 'T_OMSORDERLOGS' ? [{"asc":false,"column":"T_OMSORDERLOGS.CREATIONDATE"},{ asc: false, column: 'T_OMSORDERLOGS.ID' }] : undefined, // 日志排序
            fkdrplist: {
              searchcols: this.searchcols,
            },
            fixedcolumns: { OMSONLINEORDERID: [`=${this.pageMsgId}`] }
          };

          if (this.tableName == 'T_WMSLOGISTICSTRACK') {
            virtual.fixedcolumns = { OMSORDERID: [`=${this.pageMsgId}`] };
          }
          if (this.tableName == 'T_OMSONLINEPAYMENT') {
            const tids = await this.getTid()
            console.log("🚀 ~ file: tableFaker.vue:149 ~ getInfoData ~ tids:", tids)
            virtual.fixedcolumns = {
              OMSTID: tids
            }
            // virtual.fixedcolumns = this.fixedcolumns;
          }

          network.post('/p/cs/list/query', virtual).then((res) => {
            this.total = res.data.total;
            data = [];
            for (let i = 0; i < res.data.rows.length; i++) {
              const obj = {};
              for (let j = 0; j < Object.keys(res.data.rows[i]).length; j++) {
                let keyName = Object.keys(res.data.rows[i])[j];
                const cellInfo = res.data.rows[i][keyName];
                // 如果是平台
                if (keyName === 'PLATFORM') {
                  obj[`${keyName}_ID`] = cellInfo.refobjid;
                }
                obj[keyName] = cellInfo.val;
              }
              data.push(obj);
            }
            this.offerInfo = data;
          });
        }
      },
      // 优惠信息页码改变时触发
      changePageOfferInfo(val) {
        // 1=>0 2=>10 3=>20
        this.currentPage = val;
        this.currentPageOfferInfo = (val - 1) * this.pageSizeOfferInfo;
        this.getInfoData();
      },
      // 优惠信息页数改变时触发
      changeSizeOfferInfo(val) {
        this.pageSizeOfferInfo = val;
        this.currentPageOfferInfo = 0;
        this.getInfoData();
      },
      // 选择单项时触发
      selectData(currentRow, oldCurrentRow) {
        this.$emit('on-selectData', currentRow, this._originRowsData);
      },
      // 在多选模式下有效，选中某一项时触发
      onSelect(selection, row) {
        this.$emit('on-select', selection);
      },
      // 在多选模式下有效，取消选中某一项时触发
      onSelectCancel(selection, row) {
        this.$emit('on-select-cancel', selection);
      },
      // 在多选模式下有效，全选时触发
      onSelectAll(selection, row) {
        this.$emit('on-select-all', selection);
      },
      // 在多选模式下有效，取消全选时触发
      onSelectAllCancel(selection, row) {
        this.$emit('on-select-all-cancel', selection);
      },
    },
  };
</script>
<style lang="less" scoped>

  .pageContent {
    margin-top: 12px;
    display: flex;
    justify-content: flex-end;
  }
</style>
