import request from '~/api/request';
import useToastBehavior from '~/behaviors/useToast';

Page({
  behaviors: [useToastBehavior],

  data: {
    isLoad: true,
    service: [],
    personalInfo: {
      name: '邹方成'
    },
    gridList: [
      {
        name: '全部发布',
        icon: 'root-list',
        type: 'all',
        url: '',
      },
      {
        name: '审核中',
        icon: 'search',
        type: 'progress',
        url: '',
      },
      {
        name: '已发布',
        icon: 'upload',
        type: 'published',
        url: '',
      },
      {
        name: '草稿箱',
        icon: 'file-copy',
        type: 'draft',
        url: '',
      },
    ],

    settingList: [
      { label: '用药方案', path: '/pages/therapeuticRegimen/index', icon: 'service', type: 'text' },
      { label: '健康教育', path: '/pages/articleList/index', icon: 'setting', type: 'text' },

    ],
    risk_type: ''
  },

  onLoad() {
    // this.getServiceList();
  },

  handelClick(e) {
    console.log(e)
  },

  async onShow() {
    const Token = wx.getStorageSync('access_token');
    if (Token) {
      const personalInfo = await this.getPersonalInfo();
      let style = ''
      if(personalInfo.risk_type == '1' || !personalInfo.risk_type){
        style = 'success'
      }
      if(personalInfo.risk_type == '2'){
        style = 'waring'
      }
      if(personalInfo.risk_type == '3'){
        style = 'danger'
      }
      this.setData({
        isLoad: false,
        personalInfo: personalInfo,
        settingList: [
          { label: '用药方案', path: '/pages/therapeuticRegimen/index', icon: 'service', type: 'text' },
          { label: '健康教育', path: '/pages/articleList/index', icon: 'setting', type: 'text' },
    
        ],
        risk_type: style
      });
    } else {
      this.setData({
        isLoad: true,
        settingList: [
          { label: '用药方案', path: '', icon: 'service', type: 'text' },
          { label: '健康教育', path: '', icon: 'setting', type: 'text' },
    
        ],
      });
    }
  },

  getServiceList() {
    request('/api/getServiceList').then((res) => {
      const { service } = res.data.data;
      this.setData({ service });
    });
  },

  async getPersonalInfo() {
    const info = await request('patient/basic/0').then((res) => res);
    return info;
  },

  onLogin(e) {
    wx.navigateTo({
      url: '/pages/login/login',
    });
  },

  onNavigateTo() {
    wx.navigateTo({ url: `/pages/my/info-edit/index` });
  },

  onEleClick(e) {
    const { name, url } = e.currentTarget.dataset.data;
    if (url) return;
    this.onShowToast('#t-toast', name);
  },
  toPath(e){
    wx.navigateTo({
      url: e.currentTarget.dataset.url,
    })
  },
  logOut(){
    wx.removeStorageSync('access_token');
    this.setData({
      isLoad: true,
      
    }) 
  },
  toEmergency(){ 
    wx.navigateTo({
      url: '/pages/emergency/index',
    })
  }

});
