import Api from '~/service/Api';
import commonFunctions from '~/mixin/commonFunctions.js';
import moment from 'moment'
import Utils from "~/utils"

export default {
	mixins: [commonFunctions],
	methods: {
    async getBranchList() {
      const loading = this.loading();
      let res = await Api.post('/public/quesel/getBranchList');
      loading.close();
      if (res.respCode == SUCCESS) {
        this.model.bchtypList = res.data.list;
      }
    },
        async currentChange(num){
			this.pagination.pageNum = num 
			this.query()
        },
        // pageSize改变
        handleSizeChange(val) {
            console.log(`每页 ${val} 条`);
            this.pagination.pageNum = 1;
            this.pagination.pageSize = val;
            this.query();
        },
        // 页码改变
        handleCurrentChange(val) {
            console.log(`当前页: ${val}`);
            this.pagination.pageNum = val;
            this.query();
        },

        async query() {
            		if (this.model.bopquep.frmdat == null) {
            			this.$notify.error({ title: '错误', message: '查询开始日期必输!' });
                      return;
            		}
            		if (this.model.bopquep.tildat == null) {
            			this.$notify.error({ title: '错误', message: '查询结束日期必输!' });
            			return;
            		}
            		if(new Date(this.model.bopquep.frmdat).getTime() > new Date(this.model.bopquep.tildat).getTime()){
            			this.$notify.error({ title: '错误', message: '开始日期应小于结束日期!' });
            			return
            		}
            		let day = 731 * 24 * 3600 * 1000
            		if (new Date(this.model.bopquep.tildat).getTime() - new Date(this.model.bopquep.frmdat).getTime() > day) {
            			this.$notify.error({title:'错误',message:'请将查询条件中时间范围控制在2年内(含)'})
            			return
            		}
            const loading = this.loading();
            const rtnmsg = await Api.post(`/${this.moduleRouter()}/${this.root.trnName}/query`,
            {
                ...this.model.bopquep,
                pageNum: this.pagination.pageNum,
                pageSize: this.pagination.pageSize
            });
            loading.close();
			if (rtnmsg.respCode === SUCCESS) {
        this.model.bopsellst = rtnmsg.data.list;
        this.model.bopquep.sum = rtnmsg.data.total
			  this.pagination.total =rtnmsg.data.total;
			}
			else{
                this.model.bopsellst = rtnmsg.data.list;
                this.pagination.total =rtnmsg.data.total;
			}
			
      },
      
      
    async onSearchBack() {
      if (this.model.bopquep.frmdat == null) {
        this.$notify.error({ title: '错误', message: '查询开始日期必输!' });
        return;
      }
      if (this.model.bopquep.tildat == null) {
        this.$notify.error({ title: '错误', message: '查询结束日期必输!' });
        return;
      }
      if (new Date(this.model.bopquep.frmdat).getTime() > new Date(this.model.bopquep.tildat).getTime()) {
        this.$notify.error({ title: '错误', message: '开始日期应小于结束日期!' });
        return
      }
      let day = 731 * 24 * 3600 * 1000
      if (new Date(this.model.bopquep.tildat).getTime() - new Date(this.model.bopquep.frmdat).getTime() > day) {
        this.$notify.error({ title: '错误', message: '请将查询条件中时间范围控制在2年内(含)' })
        return
      }
      const loading = this.loading();
      const rtnmsg = await Api.post(`/${this.moduleRouter()}/${this.root.trnName}/onSearchBack`,
        {
          ...this.model.bopquep,
          pageNum: this.pagination.pageNum,
          pageSize: this.pagination.pageSize
        });
      loading.close();
      if (rtnmsg.respCode === SUCCESS) {
        this.model.backlist = rtnmsg.data.list;
        this.pagination.total = rtnmsg.data.total;
      }
      else {
        this.model.backlist = rtnmsg.data.list;
        this.pagination.total = rtnmsg.data.total;
      }

    },

		  todel(row) {
            let objtype = row.objtyp
            switch (objtype) {
                case 'DBA':
                    this.routerPush({
                        path: "/business/dbadel",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBB':
                    this.routerPush({
                        path: "/business/dbbdel",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBC':
                    this.routerPush({
                        path: "/business/dbcdel",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBD':
                    this.routerPush({
                        path: "/business/dbddel",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBE':
                    this.routerPush({
                        path: "/business/dbedel",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBF':
                    this.routerPush({
                        path: "/business/dbfdel",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBJ':
                    this.routerPush({
                        path: "/business/dbjdel",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBW':
                    this.routerPush({
                        path: "/business/dbwdel",
                        query: {inr: row.objinr}
                    })
                    break
              
            }
        },
        toDetail(row) {
			let objtype = row.objtyp
            switch (objtype) {
                case 'DBA':
                    this.routerPush({
                        path: "/business/dbainf",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBB':
                    this.routerPush({
                        path: "/business/dbbinf",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBC':
                    this.routerPush({
                        path: "/business/dbcinf",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBD':
                    this.routerPush({
                        path: "/business/dbdinf",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBE':
                    this.routerPush({
                        path: "/business/dbeinf",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBF':
                    this.routerPush({
                        path: "/business/dbfinf",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBJ':
                    this.routerPush({
                        path: "/business/dbjinf",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBW':
                    this.routerPush({
                        path: "/business/dbwinf",
                        query: {inr: row.objinr}
                    })
                    break
              
            }
        },
        toadd() {
			let objtype = this.model.bopquep.boptyp
            switch (objtype) {
                case 'DBA':
                    this.routerPush({
                        path: "/business/dbaadd"
                    })
                    break
                case 'DBB':
                    this.routerPush({
                        path: "/business/dbbadd"
                       
                    })
                    break
                case 'DBC':
                    this.routerPush({
                        path: "/business/dbcadd"
                        
                    })
                    break
                case 'DBD':
                    this.routerPush({
                        path: "/business/dbdadd"
                        
                    })
                    break
                case 'DBE':
                    this.routerPush({
                        path: "/business/dbeadd",
                      
                    })
                    break
                case 'DBF':
                    this.routerPush({
                        path: "/business/dbfadd"
                       
                    })
                    break
                case 'DBJ':
                    this.routerPush({
                        path: "/business/dbjadd"
                      
                    })
                    break
                case 'DBW':
                    this.routerPush({
                        path: "/business/dbwadd"
                        
                    })
                    break
            }
		},
		
        tomod(row) {
			let objtype = row.objtyp
            switch (objtype) {
                case 'DBA':
                    this.routerPush({
                        path: "/business/dbaame",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBB':
                    this.routerPush({
                        path: "/business/dbbame",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBC':
                    this.routerPush({
                        path: "/business/dbcame",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBD':
                    this.routerPush({
                        path: "/business/dbdame",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBE':
                    this.routerPush({
                        path: "/business/dbeame",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBF':
                    this.routerPush({
                        path: "/business/dbfame",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBJ':
                    this.routerPush({
                        path: "/business/dbjame",
                        query: {inr: row.objinr}
                    })
                    break
                case 'DBW':
                    this.routerPush({
                        path: "/business/dbwame",
                        query: {inr: row.objinr}
                    })
                    break
              
            }
  },

  tohis(row) {
    let objtype = row.objtyp
    switch (objtype) {
        case 'DBA':
            this.routerPush({
                path: "/business/dbareb",
                query: {inr: row.objinr}
            })
            break
        case 'DBB':
            this.routerPush({
                path: "/business/dbbreb",
                query: {inr: row.objinr}
            })
            break
        case 'DBC':
            this.routerPush({
                path: "/business/dbcreb",
                query: {inr: row.objinr}
            })
            break
        case 'DBD':
            this.routerPush({
                path: "/business/dbdreb",
                query: {inr: row.objinr}
            })
            break
        case 'DBE':
            this.routerPush({
                path: "/business/dbereb",
                query: {inr: row.objinr}
            })
            break
        case 'DBF':
            this.routerPush({
                path: "/business/dbfreb",
                query: {inr: row.objinr}
            })
            break
      
    }
},


	upload(){


	},
	download(){

	},
	delall(){

	},
	check(){
		
  },

    //应急导出
    async exportToTxt() {
      const rtnmsg = await Api.post(`/${this.moduleRouter()}/${this.root.trnName}/exportToTxt`,
        {
          ...this.model.bopquep,
        });
        let bopexplsit = [];
      if (rtnmsg.respCode === SUCCESS) {
        bopexplsit = rtnmsg.data;
      }else{
        this.$notify({ title: '提示', type: 'error', message: '应急导出失败' })
      return;
      }
      //进行字符串拼接 
      let textcontent = bopexplsit.map(item =>{
        return Object.values(item).map(value => (value == null)?'':value.toString()).join('|+|');
      }).join('\n');

      //创建 bolb对象
      const blob = new Blob([textcontent], {type: 'text/plain; charset=utf-8'});
      const url = window.URL.createObjectURL(blob);
      const downloadElement = document.createElement('a');
      downloadElement.href = url;
      //创建文件名
      let datstr = moment(new Date()).format('yyyyMMDDHHmmss');
      downloadElement.download = "国际收支申报数据查询_"+datstr+".txt";
      document.body.appendChild(downloadElement);
      downloadElement.click();
      document.body.removeChild(downloadElement);
      window.URL.revokeObjectURL(url);
    },

    //获取码表
	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
		}
	  },
	  //获取码值
	  getCodelabel(value,codenam) {
		const codeobj = this.dbCodes[codenam].find(obj => obj.value === value)
		return codeobj ? codeobj.label : value;
    },

    	async exportExcel(){
        const res = await Api.post(`/${this.moduleRouter()}/${this.root.trnName}/exportExcel`,
        {
            ...this.model.bopquep,
        });

			if (res.respCode == SUCCESS) {
        let datstr = moment(new Date()).format('yyyy-MM-DD HH:mm:ss');
				Utils.exportToExcel(res.data,"国际收支申报数据查询" +  "_" + datstr + ".xlsx", "国际收支申报数据查询");
			}
    },
    
    //获取总行标志 Y-总行  N-非总行
    async bopIsHeadBch() {
      let rtnmsg = await Api.post(`/report/cfa/public/bopIsHeadBch`);
      if (rtnmsg.respCode == SUCCESS) {
        this.model.btnflg = rtnmsg.data;
      }
    },

     //获取试点行标志 N-非试点  Y-试点
     async initPilotBchBop() {
      let params = {
        ownextkey: "",
      };
      let rtnmsg = await Api.post(`/${this.moduleRouter()}/bopcalcom/pilotbopbch`, params);
      if (rtnmsg.respCode == SUCCESS) {
        this.model.enableflg = rtnmsg.data;
      }
    },
  
}
}