var app = getApp();
var that;
import moment from 'moment' // 时间库
Page({
  data: {
    tabIndex: 1,//条件选择
    list: [],
    page: 1,
    id: '',
    packageName: '',
  },
  onLoad(e) {
    that = this;
    dd.setNavigationBar({ title: "结果值录入" });
    this.setData({
      id: e.id,
      packageName: e.tel,
    })
    this.getPackageDtail()
  },
  tabAction(e) {
    let index = e.target.dataset.index
    this.setData({
      tabIndex: index,
    })
    this.getPackageDtail()
  },
  bindTextAreaBlur(e) {
    this.setData({
      [`noStatusList[${e.target.dataset.index}].result`]: e.detail.value,
    });
  },
  // 提交
  save(e) {
    let num = e.target.dataset.index
    let isLr = false;
    let data = {
      id: this.data.id, // 个人考核包ID
      cache: num, // 是否暂存 1 是 0 否（提交）
      result_info: '' // 结果值信息
    };
    let result_info = [];
    this.data.noStatusList.some(item => {
      if (item.result) {
        result_info.push({
          result: item.result, // 单项目结果值
          dimension_key: item.message.dimension_key, // 维度索引
          index_key: item.message.index_key, // 指标索引
          index_id: item.id // 指标ID
        });
        isLr = true;
      }
    });
    if (!isLr) {
      app.globalData.showToast('至少输入一项结果值');
      return false;
    }
    data.result_info = JSON.stringify(result_info);
    if (this.data.bootBool) {
      return;
    }
    this.setData({
      bootBool: true
    })
    app.$post('api/per/package/record_result', data)
      .then(res => {
        this.getPackageDtail();
        setTimeout(() => {
          app.globalData.showToast(num == '1' ? '暂存成功' : '提交成功');
          this.setData({
            bootBool: false
          })
        }, 1000);
      })
  },
  getPackageDtail() {
    app.$get2('api/per/package/employee/info', { id: this.data.id }).then(res => {
      let data = res.data.data;
      let dimension = data.dimension;
      let relevance_employee = data.relevance_employee;
      dimension.forEach((item, index) => {
        if (item.index.length > 0) {
          item.index.forEach((item2, index2) => {
            item2.wdName = item.name;
            item2.userName = relevance_employee.name;
            item2.userId = relevance_employee.id;
            item2.dept_list = relevance_employee.dept_list;
            item2.dil = [
              {
                lab: '考核标准',
                prop: item2.per_remark ? item2.per_remark : null
              },
              { lab: '目标值', prop: item2.target ? item2.target : null },
              { lab: '权重%', prop: item2.weight ? item2.weight : null },
              { lab: '备注', prop: item2.remark ? item2.remark : null }
            ];
          });
        }
      });
      let flow = data.flow;
      let list = []; // 结果值录入集合
      flow.forEach(item => {
        if (item.code == 'result_value') {
          item.target.forEach(item2 => {
            if (item2.employee_id == app.globalData.userData.id) {
              list = item2.list;
              // status = item2.status;
            }
          });
        }
      });
      let statusList = []; // 已录入指标
      let noStatusList = []; // 未录入指标
      list.forEach(item => {
        if (item.status == 2) {
          dimension[item.dimension_key].index[item.index_key].message = item;
          statusList.push(dimension[item.dimension_key].index[item.index_key]);
        }
        if (item.status == 1) {
          dimension[item.dimension_key].index[item.index_key].message = item;
          if (item.result_cache) {
            // 是否有暂存的数据有就显示出来
            dimension[item.dimension_key].index[item.index_key].result = item.result_cache;
          }
          noStatusList.push(dimension[item.dimension_key].index[item.index_key]);
        }
      });
      this.setData({
        statusList: statusList,
        noStatusList: noStatusList,
      })
      if (this.data.tabIndex == 1) {
        this.setData({
          rvenotList: this.data.noStatusList
        })
      } else if (this.data.tabIndex == 2) {
        this.setData({
          rvenotList: this.data.statusList
        })
      }
      this.setData({
        age: { num1: noStatusList.length, num2: statusList.length }
      })
    })
  }
});
