// case/pages/agreement/agreement.js
import {getDeal,getRubric,getSign} from "../../../api/api"
import { throttle } from "../../../utils/util"
const dayjs = require("../../../utils/dayjs");
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
    node:'',
    type:'',
    year:'',
    month:'',
    day:'',
    signImg:'',
    navName:'项目协议'
    },
    handleGetDeal(name,mobile,bankName,bankCardNo,idCardNo){
        getDeal().then(res=>{
             let deal=res.replace(/\$\{name\}/g,name).replace(/\$\{mobile\}/g,mobile).replace(/\$\{bankName\}/g,bankName).replace(/\$\{bankCardNo\}/g,bankCardNo).replace(/\$\{idCardNo\}/g,idCardNo);
          this.setData({
               node:deal
          })
        })
    },
    handleFetRubric(){
        getRubric().then(res=>{
           let str = res.replace(/\<img/gi, '<img class="imgstyle"');
            this.setData({
                node:str
           })
        })
    },
    goSign:throttle(function(){
        let tokenStr=''
        const { envVersion } = wx.getAccountInfoSync().miniProgram;
        if(envVersion=="develop" || envVersion=="trial"){
          tokenStr="DEV_CASE_TOKEN"
        }else{
          tokenStr="PROD_CASE_TOKEN"
        }
        let token = wx.getStorageSync(tokenStr);
        if(token){
            app.method.navigateTo({
                url: '/case/pages/bankCard/bankCard',
                complete:function(err){
                   console.log(err)
                }
              })
        }else{
            app.method.navigateTo({
                url: '/case/pages/mobileLogin/mobileLogin?redirectUrl='+encodeURIComponent('case/pages/agreement/agreement'),
                complete:function(err){
                   console.log(err)
                }
              })  
        }
       
    }),
    handleGetSign(){
        getSign().then(res=>{
            if(res && res.signImg){
                let {signImg,createTime,bankCardNo,bankName,idCardNo,mobile,name}=res;
                this.handleGetDeal(name,mobile,bankName,bankCardNo,idCardNo);
               this.setData({
                signImg:signImg,
                year:dayjs(createTime).format("YYYY"),
                month:dayjs(createTime).format("M"),
                day:dayjs(createTime).format("D"),
               })
            }else{
                this.handleGetDeal('','','','','');
            }
        }).catch(error=>{
          
             if(error.code==30007){
                app.method.navigateTo({
                    url: '/case/pages/mobileLogin/mobileLogin?redirectUrl='+encodeURIComponent('case/pages/agreement/agreement'),
                    complete:function(err){
                       console.log(err)
                    }
                  })  
               
             }
        })
      },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if(options.type=="description"){
            this.handleFetRubric();
            this.setData({
                type:'description',
                navName:'操作说明'
            })
        }else{
          
            this.handleGetSign();
        }
       
      
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    // onShareAppMessage() {

    // }
})