// components/tabBar/tabBar.js
const app=getApp();
import {throttle} from "../utils/util"
import {storeBindingsBehavior} from "mobx-miniprogram-bindings"
import {store} from '../store/store.js'
import {getExpertSign,getProjectStatus,getBankInfo,getfangxinSite,limitCount,getDeal} from "../api/api"
Component({
    behaviors:[storeBindingsBehavior],
    storeBindings:{
        store,
        fields:{
            active:"active"
        },
        actions:{
            updateActive:"updateActive"
        }
    },
    /**
     * 组件的属性列表
     */
    properties: {

    },
    lifetimes: {
        // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
        attached: function () { 
            var info=wx.getSystemInfoSync()
            if(info.platform == 'ios'){
                this.setData({
                    isIos:true
                })
            }
        },
        moved: function () { },
        detached: function () { },
      },
    /**
     * 组件的初始数据
     */
    data: {
        isIos:false,
        showAuth:false,
        authMessage:'',
        showAgree:false,
        showCancel:true,
        //active:'list',
        img_host:app.hostConfig().imghost,
      tabList:[{
          id:'list',
          name:'病例列表',
          normal:app.hostConfig().imghost+'/list.png',
          active:app.hostConfig().imghost+'/list_on.png'
      },
      {
        id:'center',
        name:'个人中心',
        normal:app.hostConfig().imghost+'/center.png',
        active:app.hostConfig().imghost+'/center_on.png'
    }
    ],
     showDraft:false
    },

    /**
     * 组件的方法列表
     */
    methods: {
        handleGetDeal(){
            getDeal().then(res=>{   
                this.setData({
                    authMessage:res,
                    showAuth:true
                })
            })
        },
        handleGetProjectStatus(){
            getProjectStatus().then(res=>{
                limitCount({
                    type:4
                }).then(result=>{
                   
                    if(result<20){
                        this.handleGetSign() 
                    }else{
                        wx.showToast({
                          title: '每人参与病例征集不得超过20例',                         icon:'none'

                        })
                    }
                    
                })
                   
            }).catch((error)=>{
                if(error.code==30007){
                    wx.showToast({
                        title: '您未登录',
                        icon:'none',
                        duration:4000
                      })
                    app.method.navigateTo({
                      url: '/case/pages/mobileLogin/mobileLogin',
                    })
                 }
            })
        },
        onConfirmDraft(){
          this.setData({
            showDraft:false
          })
          app.method.navigateTo({
            url: '/case/pages/createCase/createCase',
          })
        },
        onCancelDraft(){
            this.setData({
                showDraft:false
            })
        },
        getSite(){
            getfangxinSite().then(res=>{
                app.method.navigateTo({
                    url:'/case/pages/webSign/webSign?src='+encodeURIComponent(res.linkUrl)
                })
            })
        },
        onConfirmAuth(){
            this.setData({
                showAuth:false
            });
            app.method.navigateTo({
                url: '/case/pages/bankCard/bankCard',
              })    
          },
          onCancleAuth(){
            this.setData({
                showAuth:false
            });
          },
        getBank(){
          getBankInfo().then(res=>{
              console.log(res)
           if(!res){
             this.handleGetDeal();
            //    wx.showToast({
            //      title: '请先绑定信息',
            //      icon:"none",
            //      duration:3000
                 
            //    })
            //    app.method.navigateTo({
            //     url: '/case/pages/bankCard/bankCard',
            //   })    
           }else{
                 this.setData({
                        showAgree:true
                })
           }
          
       })
     },
        handleGetSign(){
            getExpertSign().then(res=>{
                if(res && res.taskStatus==1){
                    let caseDraft=wx.getStorageSync('caseDraft');
                    if(caseDraft){
                        this.setData({
                            showDraft:true
                        })
                    }else{
                      app.method.navigateTo({
                        url: '/case/pages/createCase/createCase',
                      })
                    }
                   
                }else{
                    // this.setData({
                    //     showAgree:true
                    // })
                    this.getBank();
                   
                }  
            }).catch(error=>{
                 if(error.code==30007){
                    wx.showToast({
                        title: '您未登录',
                        icon:'none',
                        duration:4000
                      })
                    app.method.navigateTo({
                      url: '/case/pages/mobileLogin/mobileLogin',
                    })
                 }
            })
          },
        onConfirmAgree(){
            this.setData({
                showAgree:false
            });
            //this.getSite();
            app.method.navigateTo({
                url:'/case/pages/bankCard/bankCard'
            })
          },
          onCancelAgree(){
            this.setData({
                showAgree:false
            })
          },
        goCreate:throttle(function(){
            // app.method.navigateTo({
            //     url: '/case/pages/createCase/createCase',
            //   })
            this.handleGetProjectStatus()
          }),
        onChange(event) {
            this.updateActive(event.detail)
            // this.setData({ 
            //     active:event.detail 
            // });
     
            //console.log(event.detail )
           let url=event.detail=='list'?'/pages/index/index':'/pages/personCenter/personCenter'
           console.log(url);
           let THIS=this;
           wx.switchTab({
             url: url,
             success:function(){
                //  THIS.setData({
                //   active:app.globalData.tabBar_active
                //  })
                // console.log(app.globalData.tabBar_active);
             }
           })
          
         
           
          },
    }
})