import { API } from './../../../utils/network/api'
const api = new API()
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    active: 0,
    active_color: "rgb(116,162,250)",
    navbarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
      title: '药师端', //导航栏 中间的标题
    },
    // 此页面 页面内容距最顶部的距离
    height: app.globalData.height,
    pharmacist: {},
    audit_number: 0,
    data_list_0: [],
    data_list_1: [],
    data_list_2: [],
    data_list_3: [],
    current_page: 0,
    total: 0,
    per_page: 0,
    last_page: 0,
  },

  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        active: 0,  //数字是当前页面在tabbar的索引,如我的查询页索引是2，因此这边为2，同理首页就为0，审批页面为1
      })
    }

    this.getPharmacistPrescription(this.data.active);
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onLoad: function () {
   
    api.getPharmacistIndex().then(response => {
      this.setData({
        pharmacist: response.data.pharmacist,
        audit_number: response.data.audit_number
      })
    }).catch(errors => {
      console.error(errors);
    })


  },
  getPharmacistPrescription(active){
    let params = {};
    let page = this.data.current_page + 1;
    let pharmacist_audit_status = 0;
    let platform_audit_status = 1;

    if(active == 0){
      pharmacist_audit_status = 0;
      platform_audit_status = 0;
    }
    if(active == 1){
      pharmacist_audit_status = 1;
      platform_audit_status = 1;
    }
    if(active == 2){
      pharmacist_audit_status = 2;
      platform_audit_status = 0;
    }
    if(active == 3){
      pharmacist_audit_status = 1;
      platform_audit_status = 2;
    }
    params.pharmacist_audit_status = pharmacist_audit_status;
    params.platform_audit_status = platform_audit_status;
    params.page = page;
    let data_name = "data_list_"+active;
    let list = this.data[data_name];
    api.getPharmacistPrescription(params).then(response => {
      this.setData({
        [data_name]: list.concat(response.data.data),
        current_page: response.data.current_page,
        total: response.data.total,
        per_page: response.data.per_page,
        last_page: response.data.last_page,
      })
    }).catch(errors => {
      console.error(errors);
    })
  },
  onHide(){
    this.setData({
      current_page: 0,
      total: 0,
      per_page: 0,
      last_page: 0,
      data_list_0: [],
      data_list_1: [],
      data_list_2: [],
      data_list_3: [],
    })
  },
  onChange(e){
    console.log(e)
    let data_name = "data_list_"+e.detail.index;
    this.setData({
      active: e.detail.index,
      current_page: 0,
      total: 0,
      per_page: 0,
      last_page: 0,
      [data_name]: []
    })
    this.getPharmacistPrescription(e.detail.index);
  },
  onReachBottom() {
    console.log('===触底了！！===');
    if(this.data.current_page < this.data.last_page){//最后一页时停止分页
      this.getPharmacistPrescription(this.data.active)
    }
  },
  go(e){
    let url = e.currentTarget.dataset.url;
    app.go(url)
  },
  switchStatus(){
    let is_online = this.data.pharmacist.is_online;
    if(is_online == 0){
      is_online = 1
    }else if(is_online == 1){
      is_online = 0
    }
    api.putPharmacistOnOff({is_online: is_online}).then(response => {
      this.setData({
        "pharmacist.is_online": is_online
      })
    }).catch(errors => {console.error(errors);})
  }


})