import Api from '~/service/Api';
import commonFunctions from '~/mixin/commonFunctions.js';
import Utils from "~/utils"
import moment from 'moment'
export default {
	mixins: [commonFunctions],
	methods: {
		//重置
		handleReset() {
			this.model.bchcon = '';
			this.model.trncorco.ownref = '';
			this.model.usrcon = '';
			// this.model.trncorco.inidatfro = '';
			// this.model.trncorco.inidattil = '';
			this.model.atp.cod = '';
			this.model.trncorco.dflg = [];
			this.model.atptxt = '';
            this.model.minamt='';
            this.model.maxamt='';
      this.model.searchAllUsers = false;
      this.model.codeLev = "01";
      this.sortObj={};
		},
		// 机构列表
		async getBranchList() {
			const loading = this.loading();
			let res = await Api.post('/public/quesel/getActBranchList');
			if (res.respCode == SUCCESS) {
				this.bchtypList = res.data.list;
			}
			loading.close();
		},

		//经办柜员列表
		async getUserList() {
		  const loading = this.loading();
      let param={};
      if(this.model.bchcon){
        param.bchcon=this.model.bchcon
      }
      let res = await Api.post("/public/quesel/getActUserList",param);
      if (res.respCode == SUCCESS) {
        this.userList = res.data.list;
      }
      loading.close();
    },
    
    sortChange(obj) {
      let order = "";
      let prop = "";
      let column = "";
      if(obj && obj.order){
        order=obj.order === 'descending' ? 'desc' : 'asc';
        prop=obj.prop;
      }
      switch (prop) {
      case 'inifrm':
        column = "t.inifrm";
        break;
      case 'inifrmname':
        column = "stb.txt";
        break;
      case 'ownref':
        column = "t.ownref";
        break;
      case 'relflgName':
        column = "t.relflg";
        break;
      case 'reloricur':
        column = "t.reloricur";
        break;
      case 'reloriamt':
        column = "t.reloriamt";
        break;
      case 'cpldattim':
        column = "t.cpldattim";
        break;
      case 'iniusr':
        column = "t.iniusr";
        break;
      case 'relusr3':
        column = "t.relusr3";
        break;
      case 'objnam':
        column = "t.objnam";
        break;
      case 'bchname':
        column = "t.bchkeyinr";
        break;
      case 'relreqCom':
        column = "relreqCom";
        break;
      case 'relresCom':
        column = "relresCom";
        break;
    }
			let orderObj = {
				order: order,
				column: column
      }
      this.sortObj=orderObj;
			this.handleSearch(orderObj);
    },
    
    handleSearch(orderObj){
      if(this.searchModel==="OWN"){
          this.handleSearchOwn(orderObj);
      }else{
          this.handleSearchAll(orderObj);
      }
    },

		//查询列表
		async handleSearchOwn(orderObj) {
            this.searchModel="OWN";
			let inidatfro = this.model.trncorco.inidatfro;
			if (!inidatfro) {
				this.$notify.error({
					title: '错误',
					message: '查询开始日期必输!'
        });
        this.searchLoadingAll=false;
        this.searchLoading=false;
				return;
			}
			let inidattil = this.model.trncorco.inidattil;
			if (!inidattil) {
				this.$notify.error({
					title: '错误',
					message: '查询结束日期必输!'
        });
        this.searchLoadingAll=false;
        this.searchLoading=false;
				return;
			}
            if (Number(this.model.minamt)>Number(this.model.maxamt)) {
                this.$notify.error({title: '错误', message: '业务金额区间错误!'});
                return;
            }
			let params = {
				ownref: this.model.trncorco.ownref?this.model.trncorco.ownref.trim():this.model.trncorco.ownref,
				inidatfro: moment(inidatfro).format('YYYY-MM-DD') + ' 00:00:00',
				inidattil: moment(inidattil).format('YYYY-MM-DD') + ' 23:59:59',
				usrcon: this.model.usrcon,
				bchcon: this.model.bchcon,
				inifrm: this.model.atp.cod,
                minamt: this.model.minamt,
                maxamt: this.model.maxamt,
                bustypList: this.model.trncorco.dflg,
				pageNum: this.pagination.pageIndex,
        pageSize: this.pagination.pageSize,
        codeLev: this.model.codeLev,
        searchModel:this.searchModel,
      };
      if (orderObj) {
				params.orderStr = orderObj.order;
				params.columnStr = orderObj.column
      }
      if(this.roleTypeName==="单证中心" && this.model.tranTypeStr){
          params.tranTypeStr=this.model.tranTypeStr;
      }
			const loading = this.loading();
			const res = await Api.post('/public/trnrel/getDataList', params);
			if (res.respCode === SUCCESS) {
				this.stmData.data = res.data.list;
				this.pagination.total = Number(res.data.total);
			}
			loading.close();
    },

    handleToggleSearch: function () {
      this.searchToggle = !this.searchToggle
      this.$emit('search-toggle', this.searchToggle)
    },

    async handleSearchAll(orderObj) {
      this.searchModel="ALL";
			let inidatfro = this.model.trncorco.inidatfro;
			if (!inidatfro) {
				this.$notify.error({title: '错误', message: '查询开始日期必输!'});
                this.searchLoadingAll=false;
                this.searchLoading=false;
				return;
			}
			let inidattil = this.model.trncorco.inidattil;
			if (!inidattil) {
				this.$notify.error({title: '错误', message: '查询结束日期必输!'});
                this.searchLoadingAll=false;
                this.searchLoading=false;
				return;
			}
            if (Number(this.model.minamt)>Number(this.model.maxamt)) {
                this.$notify.error({title: '错误', message: '业务金额区间错误!'});
                return;
            }
			let params = {
                ownref: this.model.trncorco.ownref?this.model.trncorco.ownref.trim():this.model.trncorco.ownref,
				inidatfro: moment(inidatfro).format('YYYY-MM-DD') + ' 00:00:00',
				inidattil: moment(inidattil).format('YYYY-MM-DD') + ' 23:59:59',
				usrcon: this.model.usrcon,
				bchcon: this.model.bchcon,
				inifrm: this.model.atp.cod,
                minamt: this.model.minamt,
                maxamt: this.model.maxamt,
                bustypList: this.model.trncorco.dflg,
				pageNum: this.pagination.pageIndex,
        pageSize: this.pagination.pageSize,
        codeLev: this.model.codeLev,
        searchModel:this.searchModel,
      };
      if (orderObj) {
				params.orderStr = orderObj.order;
				params.columnStr = orderObj.column
      }
      if(this.roleTypeName==="单证中心" && this.model.tranTypeStr){
        params.tranTypeStr=this.model.tranTypeStr;
     }
			const loading = this.loading();
			const res = await Api.post('/public/trnrel/getDataList', params);
			if (res.respCode === SUCCESS) {
				this.stmData.data = res.data.list;
				this.pagination.total = Number(res.data.total);
			}
			loading.close();
    },
    
    async exportExcel(){
      const loading = this.loading();
      let params = {
                ownref: this.model.trncorco.ownref?this.model.trncorco.ownref.trim():this.model.trncorco.ownref,
				inidatfro: moment(this.model.trncorco.inidatfro).format('YYYY-MM-DD') + ' 00:00:00',
				inidattil: moment(this.model.trncorco.inidattil).format('YYYY-MM-DD') + ' 23:59:59',
				usrcon: this.model.usrcon,
				bchcon: this.model.bchcon,
				inifrm: this.model.atp.cod,
                minamt: this.model.minamt,
                maxamt: this.model.maxamt,
                bustypList: this.model.trncorco.dflg,
				pageNum: this.pagination.pageIndex,
        pageSize: this.pagination.pageSize,
        codeLev: this.model.codeLev,
        searchModel:this.searchModel,
			};
      let res = await Api.post('/public/trnrel/exportExcel',params);
      if (res.respCode == SUCCESS) {
        let name=moment(new Date()).format('yyyy-MM-DD HH:mm:ss') ;
        res.data.forEach((arr,index)=>{
          if(index == 0) return
          let num = parseFloat(arr[4])
          if(isNaN(num)) num = 0
          arr[4] = {
            v: num,
            t:'n',
            z:'#,##0.00'
          }
        })
        Utils.exportToExcel (res.data, "已完成列表_"+name+".xlsx", "已完成列表") ;
      }
      loading.close();
    },

		//交易代码
		async onSeainf() {
			this.dialogTitle = `交易列表`;
			this.dialogTableVisible = true;
			// 分层取值
			this.loadingFlag = true;
			this.tableList = [];
			this.currentPage = 1;
			this.pageSize = 10000;
			this.total = 0;
			// 获取table的表格数据
			let params = {
				cod: this.model.atp.cod,
				pageNo: this.currentPage,
				pageSize: this.pageSize
			};
			const res = await Api.post('/public/quesel/getFrameList', params);
			if (res.respCode === SUCCESS) {
				if (res.data && res.data.list) {
					this.total = res.data.total;
					res.data.list.forEach((item, i) => {
						item.index = i + 1;
					});
					this.tableList = res.data.list;
				}
			}
			this.loadingFlag = false; //接口调完变成false
		},

		currentChange(num) {
			this.currentPage = num;
			this.getTableData();
		},

		async dbClickRow(row, column, event) {
			// 此处为了回填数据时回调
			this.$emit('handleChange')
			// 回填数据
			this.model.atp.cod = row.cod;
			this.dialogTableVisible = false;
		},
		beforeClose(done) {
			this.dialogTableVisible = false;
			this.currentPage = 1;
			done();
		},

		//详情
		async onWaitDetail(idx, row) {
      let res = await Api.post("/public/trnrel/getFinTrnInfo", {trninr:row.inr});
			if (res.respCode == SUCCESS) {
        let trninr =row.inr;
        let inifrm=row.inifrm;
        if(res.data.finTrninr){
          trninr = res.data.finTrninr;
          inifrm = res.data.finInifrm;
        }
        this.routerPush({
          path: '/business/trnrelDetail',
          query: {
            inr: trninr,
            inifrm: inifrm.toLocaleLowerCase(),
            businessType: row['objtyp'],
            inidattim: row.inidattim
          }
        });
      }
    },
    async onPrint(idx, row) {
        this.routerPush({
          path: '/business/trnrelPrint',
          query: {
            inr: row.inr,
            inifrm: row.inifrm.toLocaleLowerCase(),
            businessType: row['objtyp'],
            inidattim: row.inidattim
          }
        });
    },
    
    onNodeStatusFinsh(){

    },
		//快照
		async onHandle(idx, row) {
      let res = await Api.post("/public/trnrel/getFinTrnInfo", {trninr:row.inr});
			if (res.respCode == SUCCESS) {
        let trninr ="";
        let inifrm="";
        if(res.data.finTrninr){
          trninr = res.data.finTrninr;
          inifrm = res.data.finInifrm.toLocaleLowerCase();
        }else{
          trninr=row.inr
          inifrm=row.inifrm.toLocaleLowerCase();
        }
        if(row.inr.length == 8 && moment(row.inidattim).format('YYYY') <= new Date().getFullYear()) {
          this.routerPush({
            path: '/business/HistoryRecord',
            query: {
              businessInr: trninr,
              businessType: "TRN",
              type: 'view'
            }
          })
        }else if(row.inr.length == 16){
          this.routerPush({
            path: `/review/review-${inifrm}`,
            query: {
              businessInr: row['inr'],
              businessType: "TRN",
              type: 'view'
            }
          })
        }
      }
		},

		// pageSize改变
		handleSizeChange(val) {
			this.pagination.pageIndex = 1;
			this.pagination.pageSize = val;
			this.handleSearch(this.sortObj);
		},
		// 页码改变
		handleCurrentChange(val) {
			this.pagination.pageIndex = val;
			this.handleSearch(this.sortObj);
		},
	},
};