import Api from '~/service/Api';
import { getTrnNameByInr } from "~/service/business/common";
import moment from 'moment';
import Utils from "~/utils"
import commonFunctions from '~/mixin/commonFunctions.js';

export default {
  mixins: [commonFunctions],
  methods: {
    async handleSearch() {
      const loading = this.loading();
      let param = {
        act: this.model.signinfo.act,
        channel: this.model.signinfo.channel,
        paytyp: this.model.signinfo.paytyp,
        extkey: this.model.signinfo.extkey,
        scenecode: this.model.signinfo.scenecode,
        inidatfro: this.model.signinfo.inidatfro ? moment(this.model.signinfo.inidatfro).format('YYYY-MM-DD') + ' 00:00:00' : "",
        inidattil: this.model.signinfo.inidattil ? moment(this.model.signinfo.inidattil).format('YYYY-MM-DD') + ' 23:59:59' : "", 
        pageNumber: this.pagination.pageIndex,
        pageSize: this.pagination.pageSize,
      };
      let rtnmsg = await Api.post('/Remittance/autosign/list',param);
      if (rtnmsg.respCode == SUCCESS) {
        this.stmData.data = [];
        this.stmData.data = rtnmsg.data.list;
        this.pagination.total = rtnmsg.data.total;
      } else {
        this.$notify.error({ title: '错误', message: '服务请求失败!' });
      }
      loading.close();
    },
    // pageSize改变
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pagination.pageIndex = 1;
      this.pagination.pageSize = val;
      this.handleSearch();
    },
    // 页码改变
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pagination.pageIndex = val;
      this.handleSearch();
    },

    clearDialogData() {
      Object.keys(this.model.signinfo).forEach(key => (this.model.signinfo[key] = ''));
      this.model.signinfo.pye = {
          ptytyp:'',
				  pts: new Pts().data,
      };
    },
    selectableFn(row) {
      return true;
    },
    sigAdd() {
			this.routerPush({
        path: "/business/auasig",
      });
    },
    handleEdit(row) {
			this.routerPush({
				path: "/business/auesig",
				query: { inr : row.inr}
      });
    },
    handleDeleteOne(row) {
      this.$confirm("确定删除选中的数据?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const loading = this.loading();
        let params = {
          inrs:[]
        }
        params.inrs.push(row.inr);
        let rtnmsg = await Api.post('/Remittance/autosign/delete',params);
        if (rtnmsg.respCode == SUCCESS) {
          this.$notify({title: '成功',message: '删除成功',type: 'success',duration: 1000})
          this.handleSearch();
        } else {
          this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
        loading.close();
      });
    },
    handleDelete() {
      this.$confirm("确定删除选中的数据?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      }).then(async () => {
        const loading = this.loading();
        let params = {
          inrs:[]
        }
        params.inrs = this.multipleSelection;
        let rtnmsg = await Api.post('/Remittance/autosign/delete',params);
        if (rtnmsg.respCode == SUCCESS) {
          this.$notify({title: '成功',message: '删除成功',type: 'success',duration: 1000})
          this.handleSearch();
        } else {
          this.$notify.error({ title: '错误', message: '服务请求失败!' });
        }
        loading.close();
      });
    },
    // 表格多选
    handleSelectionChange(val) {
      this.multipleSelection = val.map((item) => {
        return item.inr
      })
      this.multipleSelectionObj = val.map((item) => {
        return item
      })
      this.multipleSelectionRow = val.map((item) => {
        return item
      })[0]
    },
    async handleReset() {
      this.model.signinfo.act = '';
      this.model.signinfo.paytyp = '';
      this.model.signinfo.extkey = '';
      this.model.signinfo.scenecode = '';
      this.model.signinfo.inidatfro = moment(new Date()).subtract(7, "days").format("YYYY-MM-DD");
      this.model.signinfo.inidattil = moment(new Date()).format("yyyy-MM-DD");
    },
    formatPaytyp(row, column) {
      var result = "";
        if (row.paytyp == "I") {
          result = "汇入汇款";
        }else if(row.paytyp == "O"){
          result = "汇出汇款";
        }
      return result;
    },
    formatChannel(row, column) {
      var result = "";
        if (row.channel == "YPT") {
          result = "云平台";
        }else if(row.channel == "EJS"){
          result = "E结算";
        }
      return result;
    },
    formatBustyp(row, column) {
      var result = "";
        if (row.scenecode == "CPTHYI") {
          result = "海运费汇入汇款";
        }else if(row.scenecode == "CPTECI"){
          result = "跨境电商汇入汇款";
        }else if(row.scenecode == "CPTECO"){
          result = "跨境电商汇出汇款";
        }else if(row.scenecode == "CPTHYF"){
          result = "海运费汇出汇款";
        }
      return result;
    },
    formatMantyp(row, column) {
      var result = "";
      Object.keys(this.mantypCodeMap).forEach(key => {
        if (row.mantyp == key) {
          result = this.mantypCodeMap[key];
        }
      });
      return result;
    },
    formatTrntyp(row, column) {
      var result = "";
      Object.keys(this.trntypCodeMap).forEach(key => {
        if (row.trntyp == key) {
          result = this.trntypCodeMap[key];
        }
      });
      return result;
    },
    formatHascountpty(row, column) {
      var result = "";
        if (row.hascountpty == "1") {
          result = "是";
        }else if(row.hascountpty == "0"){
          result = "否";
        }
      return result;
    },
    handleSta(){
      this.stadialog = true;
    },
    formatExtkey(row, column){
      var result = row.extkey;
      if(result.includes('-')){
        result = result.split("-")[0];
      }
      return result;
    },
    getCodelabel(value, codenam) {
      const codeobj = this.dbCodes[codenam].find(obj => obj.value === value)
      return codeobj ? codeobj.label : value;
    },
    async getdbCode(codeType, uil, codeNam) {
      let params = {
        codeType: codeType,
        uil: uil ? uil : 'EN'
      }
      let rtnmsg = await Api.post("/manager/dic/listDicInfo", params)
      if (rtnmsg.respCode === SUCCESS) {
        let curList = rtnmsg.data.map(item => ({
          value: item.codeValue,
          label: item.codeName
        }));
        this.dbCodes[codeNam] = curList
      }
    },
  },
};