<template>
  <div>
    <basic-container class="page-crad">
      <avue-crud ref="crud" :option="option" :data="dataList" :page.sync="page" :search.sync="search"
                 :cell-style="cellStyle" @search-change="searchChange" @current-change="currentChange"
                 @size-change="sizeChange"
                 @row-save="handleRowSave"
                 @row-update="handleRowUpdate"
                 @refresh-change="refreshChange" @on-load="onLoad" :table-loading="loading" @saveColumn="saveColumn"
                 @resetColumn="resetColumn" @search-criteria-switch="searchCriteriaSwitch">
                <template slot="menuLeft">
                  <el-button icon="el-icon-plus" size="small" type="primary" v-if="dataList.length === 0" @click.stop="$refs.crud.rowAdd">新 增
                  </el-button>
        <!--          <el-button type="info" size="small" @click="outExport">导出-->
        <!--          </el-button>-->
                </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import {getToken} from "@/util/auth";
import {getList,submit} from "@/api/paymentManagement/parameterConfiguration"

export default {
  name: "index",
  data() {
    return {
      UConfiguration: {
        multipleChoices: false,
        multiple: false,
        disabled: false,
        searchShow: true,
        collapseTags: false,
        placeholder: '请点击右边按钮选择',
        dicData: []
      },
      form: {},
      search: {},
      dataList: [],
      loading: false,
      detailData: {},
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0,
        pageSizes: [20, 30, 40, 50, 100, 200, 300, 400, 500]
      },
      option: {},
      optionBack: {
        align: 'center',
        searchShow: true,
        searchMenuPosition: "right",
        searchSpan: 8,
        searchMenuSpan: 8,
        border: true,
        index: true,
        addBtn: false,
        viewBtn: false,
        editBtn: true,
        delBtn: false,
        searchIcon: true,
        menu: true,
        menuWidth: 80,
        dialogWidth: "90%",
        summaryText: "合计",
        showSummary: true,
        span: 8,
        sumColumnList: [{
          name: "amount",
          type: "sum",
          decimals: 2
        }],
        column: [{
          label: "集团/代理商商户号",
          prop: "orgid",
          width: 140,
          labelWidth: 140,
          overHidden: true
        }, {
          label: "商户号",
          prop: "cusId",
          width: 80,
          overHidden: true
        }, {
          label: "平台分配的APPID",
          prop: "appId",
          width: 140,
          labelWidth: 140,
          overHidden: true
        }, {
          label: "商户号",
          prop: "mchId",
          width: 80,
          overHidden: true
        }, {
          label: "通联支付appid",
          prop: "sybAppId",
          labelWidth: 120,
          width: 120,
          overHidden: true
        }, {
          label: "交易类型",
          prop: "payType",
          width: 100,
          overHidden: true
        }, {
          label: "支付签名key",
          prop: "paySignKey",
          width: 100,
          overHidden: true
        }, {
          label: "有效时间",
          prop: "validtime",
          width: 100,
          overHidden: true
        }, {
          label: "支付回调地址",
          prop: "notifyUrl",
          width: 100,
          overHidden: true
        }, {
          label: "微信子appid",
          prop: "subAppid",
          width: 100,
          labelWidth: 110,
          overHidden: true,
          rules: [
            {
              required: true,
              message: " ",
              trigger: "blur"
            }
          ]
        }, {
          label: "小程序appSecret",
          prop: "appSecret",
          width: 120,
          labelWidth: 120,
          overHidden: true
        }, {
          label: "渠道门店编号",
          prop: "chnlstoreId",
          width: 120,
          labelWidth: 120,
          overHidden: true
        }, {
          label: "门店号",
          prop: "subbranch",
          width: 80,
          overHidden: true
        }, {
          label: "支付完成跳转",
          prop: "frontUrl",
          width: 100,
          overHidden: true
        }, {
          label: "终端ip",
          prop: "cusIp",
          width: 90,
          overHidden: true
        }, {
          label: "MD5交易密钥",
          prop: "sybMdAppkey",
          width: 100,
          labelWidth: 110,
          overHidden: true
        }, {
          label: "RSA公钥",
          prop: "sybRsacusprikey",
          width: 100,
          overHidden: true
        }, {
          label: "RSA2公钥",
          prop: "sybRsatlpubkey",
          width: 100,
          overHidden: true
        }, {
          label: "商户sm2私钥",
          prop: "sybSmpprivatekey",
          width: 100,
          overHidden: true
        }, {
          label: "通联平台sm2公钥",
          prop: "sybSmtlpubkey",
          width: 150,
          labelWidth: 120,
          overHidden: true
        }, {
          label: "备注",
          prop: "remarks",
          width: 200,
          span: 24,
          type: 'textarea',
          overHidden: true,
          minRows: 3
        }]
      }
    };
  },
  async created() {
    this.option = await this.getColumnData(this.getColumnName(260), this.optionBack);
  },
  methods: {
    handleRowSave(row, done, loading) {
      //新增
      done(row)
      this.addModify(row)
    },
    handleRowUpdate(row, index, done, loading) {
      //编辑
      done(row)
      this.addModify(row)
    },
    addModify(row){
      submit(row).then(res=>{
        if (row.id){
          this.$message.success("修改成功")
        }else {
          this.$message.success("新增成功")
        }
        this.onLoad(this.page);
      })
    },
    cellStyle() {
      return "padding:0;height:40px;";
    },
    searchCriteriaSwitch(type) {
      if (type) {
        this.option.height = this.option.height - 46;
      } else {
        this.option.height = this.option.height + 46;
      }
      this.$refs.crud.getTableHeight();
    },
    //点击搜索按钮触发
    searchChange(params, done) {
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    refreshChange() {
      this.onLoad(this.page, this.search);
    },
    currentChange(val) {
      this.page.currentPage = val;
    },
    sizeChange(val) {
      this.page.currentPage = 1;
      this.page.pageSize = val;
    },
    onLoad(page, params = {}) {
      let data = this.deepClone(Object.assign({}, params, this.search));
      this.loading = true;
      getList(
          page.currentPage,
          page.pageSize,
          Object.assign(data)
      ).then(res => {
        this.dataList = res.data.data.records ? res.data.data.records : [];
        this.page.total = res.data.data.total;
        if (this.page.total) {
          this.option.height = window.innerHeight - 210;
        }
      }).finally(() => {
        this.loading = false;
      });
    },
    //列保存触发
    async saveColumn() {
      const inSave = await this.saveColumnData(this.getColumnName(260), this.option);
      if (inSave) {
        this.$message.success("保存成功");
        //关闭窗口
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    async resetColumn() {
      this.option = this.optionBack;
      const inSave = await this.delColumnData(this.getColumnName(260), this.optionBack);
      if (inSave) {
        this.$message.success("重置成功");
        this.$refs.crud.$refs.dialogColumn.columnBox = false;
      }
    },
    editOpen(row) {
    },
    outExport() {
      let config = {params: {...this.search}}
      if (config.params) {
        for (const propName of Object.keys(config.params)) {
          const value = config.params[propName];
          if (value !== null && typeof (value) !== "undefined") {
            if (value instanceof Array) {
              for (const key of Object.keys(value)) {
                let params = propName + '[' + key + ']';
                config.params[params] = value[key]
              }
              delete config.params[propName]
            }
          }
        }
      }
      const routeData = this.$router.resolve({
        path: '/api/blade-box-tube/annualItem/annualBudgetProfitExcel',      //跳转目标窗口的地址
        query: {
          ...config.params    //括号内是要传递给新窗口的参数
        }
      })
      window.open(routeData.href.slice(1, routeData.href.length) + '&' + `${this.website.tokenHeader}=${getToken()}`);
    }
  }
}
;
</script>

<style scoped>
.page-crad ::v-deep .basic-container__card {
  height: 94.2vh;
}

::v-deep .el-table__expanded-cell[class*="cell"] {
  padding: 0px;
}

.itemTable ::v-deep .el-table {
  width: 100%;
}
</style>
