// 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 {getSign,getProjectStatus} 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,
        showAgree:false,
        //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: {
        handleGetProjectStatus(){
            getProjectStatus().then(res=>{
                    this.handleGetSign() 
            }).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
            })
        },
        handleGetSign(){
            getSign().then(res=>{
                if(res && res.signImg){
                    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
                    })
                }  
            }).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
            });
            app.method.navigateTo({
                url:'/case/pages/agreement/agreement'
            })
          },
          onCancelAgree(){
            this.setData({
                showAgree:false
            })
          },
        goCreate:throttle(function(){
            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);
             }
           })
          
         
           
          },
    }
})