var app = getApp();
var that;
import { _debounce, getCache,setCache } from '../../../utils/util'
import 'dingtalk-jsapi/entry/union';
import { contactAdminToUseApp } from 'dingtalk-design-libs';
Page({
  data: {
    tabIndex: 1,
    menuList: [
      { name: '我的绩效', icon: 'icon-fenzu', url: '../myJx/myJx', code: 'creator admin', index: 1, color: 'blue' },
      { name: '我参与的', icon: 'icon-canyu', url: '../participation/participation', code: 'admin employee', index: 2, color: 'orange' },
      { name: '报表统计', icon: 'icon-tongji', url: '../statistics/statistics', code: 'creator admin', index: 3, color: 'green' },
      { name: '全部考核', icon: 'icon-quanbu', url: '../allJx/allJx', code: 'creator admin', index: 4, color: 'blue' },
    ],
    dbList: [],
    dbTolal: 0,
    xxList: [],
    xxTolal: 0,
    userInfo: app.globalData.userData,

    isSubject: false,//判断是否是个人版进入
    mainCorpId: '',//如果是个人版，这个企业ID是登录者所在的企业ID，不是钉钉那边提供的隐藏企业ID
    errorInfo:'',
    openDataAvailable: dd.canIUse('open-data'),
    isClose:true,
    isPoint:true,
  },
  onLoad() {
    that = this;
    dd.setNavigationBar({ title: "首页" });
    that.setData({
      userInfo: app.globalData.userData,
      role: getCache('role'),
      isPoint: getCache('isPoint')? false:true,
      isClose:getCache('isClose')? false:true
    })
    this.setR();
    this.returnS();
    this.getUserList();
  },
  onShow() {
    this.getDb()
  },
  openWebView(e) {
    dd.navigateTo({
      url: '../../serve/webView/webView?index='+5
    })
  },
  closes(){
      that.setData({
        isClose:false,
      })
      // setCache('isClose',true);
  },
  closes2(){
      that.setData({
        isPoint:false,
      })
      setCache('isPoint',true);
  },
  openDataError(err) {
    this.setData({
      errorInfo: JSON.stringify(err)
    });
  },
  openGly() {
    contactAdminToUseApp({ id: '107002', corpId: that.data.mainCorpId })
      .catch((err) => {
        // 入参不正确，或者遇到技术异常时才会进入这个链路
        console.error(err);
      })
  },
  returnS() {
    let str = "绩效主管理员"
    let role = this.data.role
    if (role == 'masterAdministrator') {
      str = "绩效主管理员"
    } else if (role == 'childAdministrator') {
      str = "绩效子管理员"
    } else if (role == 'deptManager') {
      str = "部门管理员"
    } else {
      str = "员工"
    }
    that.setData({
      str: str
    })
  },
  setR() {
    if (this.data.role == 'employee') {
      this.setData({
        menuList: [
          { name: '我的绩效', icon: 'icon-fenzu', url: '../myJx/myJx', code: 'admin', index: 1, color: 'blue' },
          { name: '我参与的', icon: 'icon-canyu', url: '../participation/participation', code: 'admin employee', index: 2, color: 'orange' },
        ],
      })
    }
  },
  getUserList() {
    app.$get("api/per/user/employee_list", { page: 0, page_size: 0 }).then((res) => {
      let list = res.data.data.list
      let data = {}
      for (let i in list) {
        if (list[i].is_scope === 0||list[i].is_official ===0) {
					list[i].name = list[i].name + '(未启用)';
				}
        data[list[i].id] = list[i];
      }
      app.globalData.usersList = data;
    })
  },
  openUrl(e) {
    // app.$get('api/drive/info').then(res => {
    //   console.log(res.data.data.space_id.toString())
    //   dd.uploadAttachmentToDingTalk({
    //     space: { spaceId: res.data.data.space_id.toString(), isCopy: 1, max: 9 },
    //     // space: { spaceId: '6200066562', isCopy: 1, max: 9 },
    //     types: ["space"],//PC端仅支持["photo","file","space"]
    //     success: (res) => {
    //         console.log(JSON.stringify(res));
    //     },
    //     fail: (err) => {
    //       dd.alert({
    //         content: JSON.stringify(err)
    //       })
    //     }
    //   })
    // })
    // return false
    var url = e.target.dataset.url;
    if (url == 'all') {
      dd.switchTab({
        url: '../../statistics/index/index'
      })
      return
    }
    dd.navigateTo({
      url: url
    })
  },
  openDetail(e) {
    let item = e.target.dataset.item
    if (this.data.tabIndex == 2) {
      let data = {
        id: item.id,
        type: item.type,
        employee_id: item.employee_id
      }
      dd.navigateTo({
        url: `../performanceDetails/performanceDetails?id=${item.remark.packageEmployee_id}&tel=${item.remark.package_name}&paths='messageInform'&data=${JSON.stringify(data)}`
      })
    } else {
      dd.navigateTo({
        url: `../performanceDetails/performanceDetails?id=${item.remark.packageEmployee_id}&tel=${item.remark.package_name}`
      })
    }
  },
  //代办||未读消息
  getDb() {
    var http1 = app.$get("api/per/package/msg/agency", { page: 1, page_size: 5, status: 0, node_type: 0 })
    var http2 = app.$get('api/per/package/msg/cc', { page: 1, page_size: 5, status: 0, type: '1,2' })// 获取排行榜
    Promise.all([http1, http2]).then(function (res) {
      let data1 = res[0].data.data;
      let data2 = res[1].data.data;
      data1.list.forEach(item => {
        if (item.remark.employee_id) {
          //被考核人
          item.userInfo = app.globalData.usersList[item.remark.employee_id];
        }
      });
      data2.list.forEach(item => {
        if (item.remark.employee_id) {
          //被考核人
          item.userInfo = app.globalData.usersList[item.remark.employee_id];
        }
      });
      that.setData({
        dbList: data1.list,
        dbTolal: data1.total,
        xxList: data2.list,
        xxTolal: data2.total,
      })
    })
  },
  tabAction(e) {
    let index = e.target.dataset.index
    this.setData({
      tabIndex: index
    })
  },
  openAll(e) {
    let index = e.target.dataset.index
    if (index == 1) {
      dd.navigateTo({
        url: `../backlog/backlog`
      })
    } else {
      dd.navigateTo({
        url: `../messageInform/messageInform`
      })
    }
  }
});
