import Api from "~/service/Api";
import moment from "moment";

export default {
    methods: {
        async handleSearch() {
            console.log("serach......");

            let opndatfrom = this.model.infcon.opndatfrom;
            if (!opndatfrom || opndatfrom == "") {
                this.$notify.error({
                    title: this.$t("financing.错误"),
                    message: this.$t("financing.查询开始日期必输!"),
                });
                return;
            }
            let opndatto = this.model.infcon.opndatto;
            if (!opndatto || opndatto == "") {
                this.$notify.error({
                    title: this.$t("financing.错误"),
                    message: this.$t("financing.查询结束日期必输!"),
                });
                return;
            }

            this.load = true;
            let rtnmsg = await Api.post("/funds/ftdsel/query", {
                ...this.model.infcon,
                dir: ">",
                pageNum: this.pagination.pageNum,
                pageSize: this.pagination.pageSize,
                opndatfrom: moment(opndatfrom).format("YYYY-MM-DD"),
                opndatto: moment(opndatto).format("YYYY-MM-DD"),
            });
            if (rtnmsg.respCode == SUCCESS) {
                this.load = false;
                this.stmData.data = [];
                this.stmData.data = rtnmsg.data.list;
                this.pagination = {
                    pageNum: rtnmsg.data.pageNum || 1,
                    pageSize: rtnmsg.data.pageSize || 10,
                    total: rtnmsg.data.total,
                };
            } else {
                this.$notify.error({
                    title: this.$t("financing.错误"),
                    message: this.$t("financing.服务请求失败!"),
                });
            }
            this.load = false;
        },
        //nothing
        async handleReset() {
            this.model.infcon.seaownref = "";
            this.model.infcon.opndatfrom = new Date();
            this.model.infcon.opndatto = new Date();
            this.model.infcon.nam = "";
            this.model.infcon.seacur = "";
            this.model.infcon.eaamtfr = "";
            this.model.infcon.eaamtto = "";

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


         // 详情
    async details(row) {
      const params = {
        ownref: row.ownref,
      };
      const res = await Api.post('/manager/trn/getTrnListByOwnref', params);
      if (res.respCode === SUCCESS) {
        if (res.data.length === 1) {
          if(this.oldRefId &&  this.$refs[this.oldRefId]){
            this.$refs[this.oldRefId].showPopper = false;
          }
          this.display(res.data[0])
        }else{
          this.trnData.data = res.data;
          this.oldRefId = 'popover_' + row.inr
          this.$refs[this.oldRefId].showPopper = true;
        }
      }
    },
      // 关闭详情弹框
      closeDetailsDialog(refId) {
        if(refId){
          this.$refs[refId].showPopper = false;
        }
      },

    // closeDisplayDialog(){
    //   if(this.oldRefId){
    //     this.$refs[this.oldRefId].showPopper = false;
    //   }
    // },
    // 处理
    async handler(row) {
        this.handleModel = row;
        this.trnUrl = 'funds';
        this.inifrm = 'ftdsel';
        this.initdialog = true;
      },
  
      //双击表格数据
      async TableDblRow(row) {
        this.handler(row)
      },
      closeDisplayDialog(){
        if(this.oldRefId){
          this.$refs[this.oldRefId].showPopper = false;
        }
      },


      //点击处理弹窗中按钮
      async handleClick (btn,row) {
        let obj = {
          objtyp:'FTD',
          objinr:row.inr,
          pntinr:row.pntinr,
          pnttyp:row.pnttyp,
          ledinr:row.ledinr,
          trnName:btn.code.toLowerCase(),
        }
       let isPush =  await this.$refs.lockAndPending.checkLockAndPending(obj)
        if(!isPush){
          return
        }
        let params = {}
        if(btn.code.toLowerCase() == 'fttfmt'){
          params = {
            path: "/business/" + btn.code.toLowerCase(),
          }
        }else{
          params = {
            path: "/business/" + btn.code.toLowerCase(),
            query: { inr: row.inr,pntinr: row.pntinr }
          }
        }
        this.routerPush(params);
        this.initdialog = false;
      },
      //修改弹窗状态
     changeBtn(isVisible){
      this.initdialog = isVisible;
    },
      // 关闭处理弹框
      closeHandlerDialog() {
        this.initdialog = false;
      },
  
      // /**
      //  * 打开快照页面头寸调拨
      //  */
      // display(row) {
      //   if(this.oldRefId){
      //     this.$refs[this.oldRefId].showPopper = false;
      //   }
      //   // 历史快照
      //   if (row.inr.length == 8) {
      //     this.routerPush({
      //       path: '/business/HistoryRecord',
      //       query: {
      //         businessInr: row.inr,
      //         businessType: 'TRN',
      //         type:'view'
      //       }
      //     })
      //   } else if (row.inr.length == 4) {//TODO:修改为16 本地测试:4
      //     this.routerPush({
      //       path: `/display/fttpcm`,//${row.inifrm.toLowerCase()}
      //       query: {
      //         businessInr: row.inr,
      //         businessType: 'TRN'
      //       }
      //     });
      //   }
      // },
            /**
       * 打开快照页面资金拆借
       */
            display(row) {
              if(this.oldRefId){
                this.$refs[this.oldRefId].showPopper = false;
              }
              // 历史快照
              if (row.inr.length == 8) {
                this.routerPush({
                  path: '/business/HistoryRecord',
                  query: {
                    businessInr: row.inr,
                    businessType: 'TRN',
                    type:'view'
                  }
                })
              } else if (row.inr.length == 16) {
                this.routerPush({
                  path: `/display/${row.inifrm.toLowerCase()}`,
                  query: {
                    businessInr: row.inr,
                    businessType: 'TRN'
                  }
                });
              }
            },
      
      changeOwnref(){
        if(this.model.infcon.seaownref){
          this.model.infcon.opndatfrom = null
        }
      },
       //Info
       toInfo(row,objtyp,subobjtyp,paytyp){
        this.routerPush({
          path: "/business/fttpcminf",
          query: { inr: row.inr,objtyp:objtyp,pntinr:row.pntinr,subobjtyp:subobjtyp,paytyp:paytyp}
        });
      },
      //InfoZJCJ
      toInfoZJCJ(row,objtyp,subobjtyp,paytyp){
        this.routerPush({
          path: "/business/fttfcminf",
          query: { inr: row.inr,objtyp:objtyp,pntinr:row.pntinr,subobjtyp:subobjtyp,paytyp:paytyp}
        });
      },
      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.model.dbCodes[codeNam] = curList
        }
      },
      getCodelabel(value,codenam) {
        const codeobj = this.model.dbCodes[codenam].find(obj => obj.value === value)
        return codeobj ? codeobj.label : value;
      }, 
      //进入头寸调拨交易
      toFttpcm() {
        this.routerPush({
        path: '/business/fttpcm',
        });
      },

      
      toFttfcm() {
        this.routerPush({
        path: '/business/fttfcm',
        });
      },
    },

  };