<template>
  <div class="all boxMinHeight">
    <PageHead phName="调整目标"></PageHead>
    <div class="main">
      <div class="flex-box-ce" style="border-bottom: 1px solid #f1f1f1;padding-bottom: 10px;margin-bottom: 20px;">
        <div class="flex-box-ce flex-1">
          <userImage :user_name="relevance_employee.name" :img_url="relevance_employee.img_url" fontSize="14" width="50px" height="50px"></userImage>
          <div>
            <div class="name" style="margin: 0 10px;">{{ relevance_employee.name }}</div>
            <div class="name fontColorC" style="margin: 0 10px;" v-if="relevance_employee.dept_list.length > 0">
              {{ relevance_employee.dept_list[0].dept_name }}
            </div>
          </div>
        </div>
      </div>
      <div style="margin-top: 20px;">
        <el-tabs v-model="wdIndex">
          <el-tab-pane :label="getName(item)" :name="index.toString()" v-for="(item, index) in dimension" :key="index">
            <el-table :data="item.index" v-loading="loading" :header-cell-style="{ background: '#ECF5FF' }">
              <el-table-column prop="name" label="指标名称"></el-table-column>
              <el-table-column prop="type" label="指标类型">
                <template slot-scope="scope">
                  <span v-if="scope.row.type == 1">量化指标</span>
                  <span v-if="scope.row.type == 2">非量化指标</span>
                  <span v-if="scope.row.type == 3">额外加分项</span>
                  <span v-if="scope.row.type == 4">额外扣分项</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="per_remark"
                min-width="150"
                :label="item.index_type == 3 || item.index_type == 4 ? (item.index_type == 3 ? '加分标准' : '扣分标准') : '考核标准'"
              ></el-table-column>
              <el-table-column prop="point_limit" :label="item.index_type == 3 ? '加分上限' : '扣分上限'" v-if="item.index_type == 3 || item.index_type == 4"></el-table-column>
              <el-table-column prop="target" label="目标值" v-if="item.index_type == 1">
                <template slot-scope="scope">
                  <span v-if="scope.row.target">{{ scope.row.target }}{{ scope.row.unit }}</span>
                </template>
              </el-table-column>
              <el-table-column prop="unit" label="量化指标单位" v-if="item.index_type == 1"></el-table-column>
              <el-table-column prop="weight" label="权重(%)" v-if="item.index_type == 1 || item.index_type == 2"></el-table-column>
              <!-- <el-table-column prop="remark" label="备注"></el-table-column> -->
              <el-table-column label="结果值录入人" v-if="item.index_type == 1">
                <template slot-scope="scope">
                  <span v-if="scope.row.result_type == 'none'">不开启</span>
                  <span v-if="scope.row.result_type == 'self'">被考核人</span>
                  <span v-if="scope.row.result_type == 'special'">指定员工</span>
                  <span v-if="scope.row.result_type == 'supervisor'">指定管理员</span>
                </template>
              </el-table-column>
              <el-table-column prop="reviewer_name" label="指定评分人"></el-table-column>
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button @click="compileIndex(scope, index)" type="text">编辑</el-button>
                  <!-- <el-button @click="deleteIndex(scope, index)" type="text">删除</el-button> -->
                </template>
              </el-table-column>
            </el-table>
            <div class="fontColorB" style="margin: 20px 0;" v-if="item.index_type == 1 || item.index_type == 2">
              <div style="margin-bottom: 5px;">指标权重: {{ getWeight()[0] }}%</div>
              <div>
                所有指标总权重:
                <span class="yellow">{{ getWeight()[1] }}%</span>
              </div>
            </div>
<!--            <div class="wd-footer">
              <div class="flex-box">
                <el-button class="primaryBtn" @click="selectIndex(item, index)">
                  <i class="el-icon-s-operation blue"></i>
                  选择指标项
                </el-button>
                <el-button class="primaryBtn" @click="addIndex(item, index)">
                  <i class="el-icon-plus blue"></i>
                  增加指标项
                </el-button>
              </div>
            </div> -->
          </el-tab-pane>
        </el-tabs>
      </div>
    </div>
    <footer class="footer flex-box-end">
      <el-button @click="$router.go(-1)">取消</el-button>
      <el-button type="primary" @click="getIsTemplate()">提交</el-button>
    </footer>

    <!-- 选择指标项 -->
    <BrawerBox drawerTitle="选择指标项" :showDrawer.sync="isSelectIndex">
      <template slot="main">
        <div class="flex-box-ce">
          <el-select v-model="cate_id" filterable placeholder="请选择或搜索分类" class="flex-2">
            <el-option v-for="item in indexTypeList" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-select v-model="tabIds" multiple placeholder="标签搜索" class="flex-1" style="margin-left: 10px;" v-if="tabs.length > 0">
            <el-option v-for="item in tabs" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </div>
        <template v-if="indexList.length > 0">
          <div class="flex-box-ce li" style="margin-top: 20px;"><el-checkbox class="flex-box-ce" v-model="isAllSelectIndex" label="全选"></el-checkbox></div>
          <div class="infinite-list-wrapper scroll-bar">
            <ul class="ul" v-loading="indexLoading" v-infinite-scroll="load" infinite-scroll-disabled="disabled">
              <li v-for="(item, index) in indexList" :key="index" class="flex-box-ce li">
                <el-checkbox class="flex-box-ce" v-model="item.check" :label="item.name">
                  <div>
                    <div>{{ item.name }}</div>
                    <div class="fontColorD" style="font-size: 12px; width: ;">{{ item.cate_name }}</div>
                  </div>
                </el-checkbox>
              </li>
              <div class="fontColorC" style="text-align: center;padding: 10px 0;font-size: 12px;">没有更多了</div>
            </ul>
          </div>
        </template>
        <NoData v-else></NoData>
      </template>
      <template slot="footer">
        <el-button plain @click="isSelectIndex = false">取消</el-button>
        <el-button type="primary" :loading="indexLoading" @click="selectCate()">确定</el-button>
      </template>
    </BrawerBox>

    <!-- 新增指标 -->
    <BrawerBox drawerTitle="指标设置" :showDrawer.sync="isAddIndex" :closeModal="false">
      <template slot="main">
        <el-form ref="saveAddIndex" :model="addIndexForm" :rules="addIndexFormRule" @submit.native.prevent label-width="120px">
          <el-form-item label="指标类型:" prop="type">
            <el-select v-model="addIndexForm.type" disabled placeholder="请选择指标类型" class="width-250">
              <el-option label="量化指标" :value="1"></el-option>
              <el-option label="非量化指标" :value="2"></el-option>
              <el-option label="额外加分项" :value="3"></el-option>
              <el-option label="额外扣分项" :value="4"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="指标名称" prop="name">
            <el-input placeholder="请输入指标名称" v-model="addIndexForm.name" clearable show-word-limit maxlength="30"></el-input>
          </el-form-item>
          <el-form-item :label="fromName" prop="per_remark">
            <el-input placeholder="请输入" type="textarea" v-model="addIndexForm.per_remark" clearable autosize show-word-limit maxlength="500"></el-input>
          </el-form-item>
          <el-form-item label="加分上限" prop="point_limit" v-if="addIndexForm.type == 3">
            <el-input placeholder="请输入加分上限" v-model="addIndexForm.point_limit" clearable oninput="value=value.replace(/[^\d]/g,'')"></el-input>
          </el-form-item>
          <el-form-item label="扣分上限" prop="point_limit" v-if="addIndexForm.type == 4">
            <el-input placeholder="请输入扣分上限" v-model="addIndexForm.point_limit" clearable oninput="value=value.replace(/[^\d]/g,'')"></el-input>
          </el-form-item>
          <el-form-item label="目标值" prop="target" v-if="addIndexForm.type == 1">
            <el-input placeholder="请输入目标值" v-model="addIndexForm.target" clearable></el-input>
          </el-form-item>
          <el-form-item label="量化指标单位" prop="unit" v-show="addIndexForm.type == 1">
            <el-input placeholder="请输入单位,比如%,元,万元" v-model="addIndexForm.unit" clearable></el-input>
          </el-form-item>
          <el-form-item label="结果值录入" prop="result_type" v-if="addIndexForm.type == 1">
            <template slot="label">
              <span>结果值录入:</span>
              <el-tooltip effect="dark" placement="top">
                <template slot="content">
                  开启后，当前指标评分前需要先录入结果值。结果值用来记录考核周期内指标的实际完成情况。
                </template>
                <i class="el-icon-warning"></i>
              </el-tooltip>
            </template>
            <el-select disabled v-model="addIndexForm.result_type" class="width-250">
              <el-option label="不开启" value="none"></el-option>
              <el-option label="被考核人" value="self"></el-option>
              <el-option label="指定员工" value="special"></el-option>
              <el-option label="直属部门主管" value="supervisor"></el-option>
            </el-select>
            <div style="position: relative;margin-top: 10px;" v-if="addIndexForm.result_type == 'special'" class="width-250">
              <el-input v-model="assignUser.name" placeholder="请选择指定员工"></el-input>
              <div @click="addUser(1)" class="inputDc"></div>
            </div>
          </el-form-item>
          <el-form-item label="权重" prop="weight" v-if="addIndexForm.type == 2 || addIndexForm.type == 1">
            <el-input disabled placeholder="请输入权重(1~100)" id="input2" @input="checkCountry('input2')" v-model.number="addIndexForm.weight">
              <template slot="append">
                %
              </template>
            </el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remark">
            <el-input placeholder="请输入" type="textarea" v-model="addIndexForm.remark" autosize  show-word-limit maxlength="500"></el-input>
          </el-form-item>
          <el-form-item label="评分方式" v-if="addIndexForm.type == 1"><el-input placeholder="手动输入评分" disabled></el-input></el-form-item>
          <el-form-item label="指定评分人" prop="isReviewer" v-if="special_scorer">
            <el-switch v-model="addIndexForm.isReviewer" disabled></el-switch>
            <div>
              <div style="position: relative;" v-if="addIndexForm.isReviewer">
                <el-input v-model="assignScoreUser.name" placeholder="请选择指定员工"></el-input>
                <div @click="addUser(2)" class="inputDc"></div>
              </div>
              <div class="fontColorC" style="line-height: 24px;">开启后，该指标如被制定目标引用，该指标将由指定的人进行评分</div>
            </div>
          </el-form-item>
          <el-form-item label="管理记录人" prop="employee_ids">
            <div class="border flex-box-ce">
              <div class="flex-1" v-if="record_ids.length == 0">请选择记录人</div>
              <div v-else style="width: 250px;" class="font-flex-word">
                <span v-for="(item, index) in record_ids" :key="index">
                  <i v-if="index != 0">,</i>
                  {{ item.name }}
                </span>
              </div>
              <span v-if="record_ids.length > 0" class="blue">{{ record_ids.length }}人</span>
              <i class="el-icon-arrow-down icon-right" v-else></i>
              <!-- <div @click="isShowRecordFun()" class="inputDc"></div> -->
            </div>
          </el-form-item>
        </el-form>
        <div>
          <span style="font-weight: 600;font-size: 14px;color: #606266;">按分数展示对应的评级：</span>
          <el-select v-model="addIndexForm.index_level_id" @change="levelReadOnly" filterable clearable placeholder="请选择" disabled>
            <el-option v-for="item in levelOptions" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </div>
        <ClassSet
          v-if="addIndexForm.index_level_id != 0 && getIs"
          ref="ClassSet"
          :readOnly="true"
          :inputs="inputs"
          style="padding-top: 20px;"
          :inputsStyle="{ paddingLeft: '0px', width: '450px' }"
        ></ClassSet>
      </template>
      <template slot="footer">
        <el-button plain @click="closeAddIndexForm('saveAddIndex')">取消</el-button>
        <el-button type="primary" @click="saveAddIndex('saveAddIndex', 1)" v-if="isSave">提交</el-button>
        <el-button type="primary" @click="saveAddIndex('saveAddIndex', 2)" v-else>保存</el-button>
      </template>
    </BrawerBox>
    <!-- 调整说明 -->
    <el-dialog title="调整说明" :visible.sync="isAdjustment" width="500px" :close-on-click-modal="false">
      <el-input type="textarea" v-model="adjustmentText" rows="4" placeholder="请输入内容(选填)"></el-input>
	  <div style="height: 20px;"></div>
	  <el-checkbox v-model="ding_msg" :true-label="1" :false-label="0">发送钉钉通知</el-checkbox>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isAdjustment = false">取 消</el-button>
        <el-button type="primary" @click="saveAdjustment()">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 评分人 -->
    <EmployeeSelector :multi="false" :is_filtration_creator="false" :selected="selected" :isChecKedAll="false" :visible.sync="setAdministrator" @confirm="confirmAdministrator" />
    <!-- 管理记录人 -->
    <EmployeeSelector :is_filtration_creator="false" :selected="selected2" :visible.sync="isShowRecord" @confirm="confirmAdministrator2" />
  </div>
</template>

<script>
import EmployeeSelector from '@/components/public/EmployeeSelector';
import BrawerBox from '@/components/public/BrawerBox';
import PageHead from '@/components/public/PageHead';
import ClassSet from '@/components/public/ClassSet';
export default {
  name: 'adjustment',
  components: { EmployeeSelector, BrawerBox, PageHead, ClassSet },
  data() {
    return {
      inputs: [], //分值区间(只读)
      levelOptions: [], //指标等级列表
      wdIndex: '0',
      total: 0,
      page: 1,
      page_size: 10,
      loading: false,
      indexTypeList: [],

      isSelectIndex: false, //选择指标弹窗控制
      isAllSelectIndex: false, //是否全选指标
      cate_id: 0,
      indexType: 0, //指标分类
      indexList: [],
      indexLoading: false,
      indexPage: 1,
      isAfterLoading: false, //是否加载完

      // 添加指标
      isAddIndex: false,
      selected2: { employee: [], dept: [] }, //已经选择人员
      record_ids: [],
      isShowRecord: false,
      addIndexForm: {
        id: 0,
        custom: 1,
        name: '', //指标名称
        per_remark: '', //考核标准
        remark: '', //备注
        type: 1, //默认1 指标种类 1-量化指标 2-非量化指标 3-额外加分项 4-额外扣分项
        unit: '', //量化指标单位
        target: '', //目标值
        result_type: 'none', //(结果值录入者设置,none-不开启 self-被考核人 special-指定员工 supervisor-指定主管’
        result_employee_id: '', //结果值录入者id, 只有result_type为special时才需要指定
        weight: 0, //权重 0-100之间
        reviewer_id: '', //指定审批人(评分人)id
        point_limit: '', //分数上限,只对额外加分项以及额外扣分项有用
        // 自定义
        isReviewer: false, //是否有指定评分人
        index_level_id: '', //指标等级
        record_ids: []
      },
      addIndexFormRule: {
        name: [{ required: true, message: '请输入指标名称', trigger: 'blur' }, { min: 2, message: '最少2个字', trigger: 'blur' }],
        per_remark: [{ required: true, message: '请输入考核标准', trigger: 'blur' }, { min: 2, message: '最少2个字', trigger: 'blur' }],
        weight: [{ required: true, message: '请输入权重', trigger: 'blur' }]
      },
      assignUser: {}, //指定人员
      assignScoreUser: {}, //指定评分人
      selected: { employee: [], dept: [] }, //已经选择人员
      setAdministrator: false,
      isUserIndex: 1, //判断选择什么人，1是指定人员，2是评分人
      isSave: true, //判断指标是提交还是编辑
      ids: [], //需要过滤的指标ID
      fromName: '考核标准',

      dimension: [], //列表
      dimensionalityList: [], //提交的数据
      calc_dimension: '', //是否维度权重参与计算 1-是 0-否
      calc_type: '', //指标评分计算方式 1-加和计算 2-加权计算
      relevance_employee: {
        name: '',
        img_url: '',
        dept_list: []
      },
      id: '', //
      isAdjustment: false, //调整说明
      adjustmentText: '',
      special_scorer: false, //判断是否能设置指定评分人
      process: {},

      // 标签相关
      tabs: [],
      tabIds: [],
	  ding_msg:0,
    };
  },
  watch: {
    isAllSelectIndex(val) {
      this.indexList.forEach(item => {
        item.check = val;
      });
    },
    isAdjustment(val) {
      if (!val) {
        this.adjustmentText = '';
      }
    },
    tabIds(val) {
      this.indexPage = 1;
      this.isAfterLoading = false;
      this.isAllSelectIndex = false;
      this.getIndexList();
    },
    cate_id() {
      this.indexPage = 1;
      this.isAfterLoading = false;
      this.getIndexList();
    },
    isSelectIndex(val) {
      //关闭选择指标时清空过滤的指标
      if (!val) {
        this.ids = [];
        this.isAfterLoading = false;
        this.isAllSelectIndex = false;
        this.cate_id = 0;
        this.indexPage = 1;
      }
    },
    'addIndexForm.type'(val) {
      if (val == 3) {
        this.fromName = '加分标准';
      } else if (val == 4) {
        this.fromName = '扣分标准';
      } else {
        this.fromName = '考核标准';
      }
    },
    record_ids(val) {
      let ids = val.map(item => {
        return item.id;
      });
      this.addIndexForm.record_ids = ids || '';
    }
  },
  created() {
    this.getTagList();
    this.getIndexType();
    if (this.$route.query.id) {
      this.id = this.$route.query.id;
      this.getPackageDtail();
    }
    this.gradeSetList();
  },
  computed: {
    getIs() {
      if (this.levelOptions.length > 0) {
        let is = false;
        this.levelOptions.forEach(item => {
          if (item.id == this.addIndexForm.index_level_id) {
            is = true;
          }
        });
        return is;
      } else {
        return false;
      }
    },
    disabled() {
      return this.indexLoading || this.isAfterLoading;
    }
  },
  methods: {
    isShowRecordFun() {
      this.selected2.employee = this.record_ids;
      this.isShowRecord = true;
    },
    confirmAdministrator2(e) {
      this.record_ids = e.employee.length > 0 ? e.employee : [];
    },
    //获取标签列表
    getTagList() {
      this.$axios('get', '/api/per/index/tag').then(res => {
        this.tabs = res.data.data.list || [];
      });
    },
    load() {
      this.indexLoading = true;
      this.indexPage = this.indexPage + 1;
      setTimeout(() => {
        this.getIndexList();
        this.indexLoading = false;
      }, 500);
    },
    levelReadOnly(item) {
      var newArr = this.levelOptions.filter(function(obj) {
        return obj.id == item;
      });
      if (newArr.length > 0) {
        this.islevel(newArr[0]);
      }
    },
    islevel(levels) {
      var inputs = [];
      var max = 0; //最大值
      levels.scope.forEach((item, index) => {
        var obj;
        if (index == 0) {
          obj = { name: item.name, max: Number(item.value), min: 0 };
        } else {
          obj = { name: item.name, max: Number(item.value), min: max }; //当不是第一个等级时，最小值为上一个的最大值
        }
        max = item.value;
        inputs.push(obj);
      });
      this.$nextTick(() => {
        this.inputs = inputs;
      });
    },
    //指标等级列表
    gradeSetList() {
      this.$axios('get', '/api/per/index/level/list').then(res => {
        if (res.data.code == 1) {
          let list = res.data.data.list;
          this.levelOptions = list;
        }
      });
    },
    // 调整说明确定
    saveAdjustment() {
      this.$axios('post', '/api/per/package/adjust_package', {
        id: this.id,
        content: this.adjustmentText,
        dimension: this.dimensionalityList,
        process: JSON.stringify(this.process),
        reset_flow:0,
		ding_msg:this.ding_msg
      }).then(res => {
        this.$message.success('调整成功');
        this.$router.go(-1);
      });
    },
    checkCountry(str) {
      //解决v-model与oninput同时使用失效问题
      var dom = document.getElementById(str);
      var value = dom.value;
      //写你自己的输入规则
      value = value.replace(/[^\d]/g, '');
      if (value > 100) {
        str == 'input' ? (this.dimensionalityForm.dimension_weight = 0) : (this.addIndexForm.weight = 0);
        this.$message.error('不能大于100');
        return false;
      }
      dom.value = value; //输入框赋值
      str == 'input' ? (this.dimensionalityForm.dimension_weight = dom.value) : (this.addIndexForm.weight = dom.value);
    },
    //筛选指标分类
    selectCate() {
      let indexList = JSON.parse(JSON.stringify(this.indexList));
      let arr = [];
      indexList.forEach(item => {
        if (item.check) {
			if(item.type==3||item.type==4){
			  if(item.point_limit==0){
				item.point_limit="";
			  }
			}
          arr.push(item);
        }
      });
      this.dimension[this.selectItemIndex].index.push(...arr);
      this.isSelectIndex = false;
    },
    //保存人员
    confirmAdministrator(e) {
      if (this.isUserIndex == 1) {
        this.assignUser = e.employee.length > 0 ? e.employee[0] : [];
      } else {
        this.assignScoreUser = e.employee.length > 0 ? e.employee[0] : [];
      }
      this.setAdministrator = false;
    },
    //删除指标
    deleteIndex(e, index) {
      this.$confirm('确定删除该指标', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.dimension[index].index.splice(e.$index, 1);
        })
        .catch(() => {});
    },

    //编辑指标
    compileIndex(e, index) {
      this.selectItemIndex = index;
      this.selectItemIndex_index = e.$index;
      let obj = e.row;
      // console.log(obj)
      this.addIndexForm = {
        id: obj.id,
        custom: obj.custom,
        name: obj.name, //指标名称
        per_remark: obj.per_remark, //考核标准
        remark: obj.remark, //备注
        type: obj.type, //默认1 指标种类 1-量化指标 2-非量化指标 3-额外加分项 4-额外扣分项
        unit: obj.unit, //量化指标单位
        target: obj.target, //目标值
        result_type: obj.result_type, //(结果值录入者设置,none-不开启 self-被考核人 special-指定员工 supervisor-指定主管’
        result_employee_id: obj.result_employee_id, //结果值录入者id, 只有result_type为special时才需要指定
        weight: obj.weight, //权重 0-100之间
        reviewer_id: obj.reviewer_id, //指定审批人(评分人)id
        point_limit: obj.point_limit, //分数上限,只对额外加分项以及额外扣分项有用
        index_level_id: obj.index_level_id ? obj.index_level_id : '',
        // 自定义
        isReviewer: obj.reviewer_id ? true : false, //是否显示指定审批人

        record_ids: obj.record_ids ? obj.record_ids : [],
        mamage_record:obj.mamage_record,//管理记录
        schedule:obj.schedule,//执行计划
      };
      if (obj.index_level_id && obj.index_level_id > 0 && obj.index_level_id != '') {
        this.levelReadOnly(obj.index_level_id);
      }
      let employeeMap = this.$getEmployeeList();
      // 管理记录
      if (obj.record_ids) {
        this.record_ids = obj.record_ids.map(e => {
          let user = employeeMap[e];
          return user;
        });
      }
      if (obj.result_employee_id) {
        this.assignUser = {
          name: employeeMap[obj.result_employee_id].name,
          id: employeeMap[obj.result_employee_id].id
        };
      }
      if (obj.reviewer_id) {
        this.assignScoreUser = {
          name: employeeMap[obj.reviewer_id].name,
          id: employeeMap[obj.reviewer_id].id
        };
      }
      this.isSave = false;
      this.isAddIndex = true;
    },
    // 考核详情
    getPackageDtail() {
      let employeeMap = this.$getEmployeeList();
      this.$axios('get', '/api/per/package/employee/info', { id: this.id }).then(res => {
        let data = res.data.data;
        let dimension = data.dimension;
        this.process = res.data.data.process;
        this.calc_type = data.calc_type;
        this.calc_dimension = data.calc_dimension;
        this.special_scorer = data.process.special_scorer.enable == 1 ? true : false; //判断流程中是否开启指定评分人
        dimension.forEach(item => {
          if (item.index.length > 0) {
            item.index.forEach(item2 => {
              item2.result_employee_name = employeeMap[item2.result_employee_id] ? employeeMap[item2.result_employee_id].name : '';
              item2.reviewer_name = employeeMap[item2.reviewer_id] ? employeeMap[item2.reviewer_id].name : '';
            });
          }
        });
        this.dimension = dimension;
        this.getWeight();
        this.relevance_employee = data.relevance_employee;
      });
    },
    // 保存||编辑指标
    saveAddIndex(formName, val) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.addIndexForm.result_type == 'special') {
            if (!this.assignUser.id) {
              this.$message.error('请选择指定人员');
              return;
            }
          }
          if (this.addIndexForm.type == 3 || this.addIndexForm.type == 4) {
            if (this.addIndexForm.point_limit === 0) {
              this.$message.error('分数上限不能为0');
              return;
            }
          }
          if (this.addIndexForm.type == 1 || this.addIndexForm.type == 2) {
            if (this.addIndexForm.weight == 0) {
              this.$message.error('权重不能为0');
              return;
            }
            if (this.addIndexForm.weight > 100) {
              this.$message.error('权重不能大于100');
              return;
            }
          }
          if (this.addIndexForm.isReviewer) {
            if (!this.assignScoreUser.id) {
              this.$message.error('请选择指定评分人');
              return;
            }
          }
          var index = {
            id: this.addIndexForm.id ? this.addIndexForm.id : 0, //对应的指标库id，0-标识为自定义添加的指标
            custom: this.addIndexForm.custom,
            type: this.addIndexForm.type, //指标种类 1-量化指标 2-非量化指标 3-额外加分项 4-额外扣分项
            name: this.addIndexForm.name, //指标名
            per_remark: this.addIndexForm.per_remark, //考核标准,加分标准,扣分标准等说明
            remark: this.addIndexForm.remark, //指标备注
            target: this.addIndexForm.target, //目标值
            unit: this.addIndexForm.unit, //量化指标单位
            point_limit: this.addIndexForm.point_limit, //分数上限,只对额外加分项以及额外扣分项有用
            weight: this.addIndexForm.weight, //权重 0-100之间
            result_type: this.addIndexForm.result_type, //结果值录入者设置,none-不开启 self-被考核人 special-指定员工 supervisor-指定主管)
            index_level_id: this.addIndexForm.index_level_id, //指标等级
            // 当录入者是指定人员时才录入
            result_employee_id: this.addIndexForm.result_type == 'special' ? this.assignUser.id : '', //结果值录入者id,只有result_type为special时才需要指定
            result_employee_name: this.addIndexForm.result_type == 'special' ? this.assignUser.name : '',

            // 当开启了指定审批人时才录入值
            reviewer_id: this.addIndexForm.isReviewer ? this.assignScoreUser.id : '', //指定审批人(评分人)id
            reviewer_name: this.addIndexForm.isReviewer ? this.assignScoreUser.name : '', //指定审批人名称
            record_ids: this.addIndexForm.record_ids.length > 0 ? this.addIndexForm.record_ids : [] ,//指定跟踪管理记录人 数组

            mamage_record:this.addIndexForm.mamage_record,//管理记录
            schedule:this.addIndexForm.schedule//执行计划
          };
          if (val == 1) {
            this.dimension[this.selectItemIndex].index.push(index);
          } else {
            this.dimension[this.selectItemIndex].index.splice(this.selectItemIndex_index, 1, index); //替换元素
          }
          this.closeAddIndexForm(formName);
        }
      });
    },
    closeAddIndexForm(formName) {
      // 清空数据
      this.addIndexForm = {
        id: 0,
        custom: 1,
        name: '', //指标名称
        per_remark: '', //考核标准
        remark: '', //备注
        type: '', //默认1 指标种类 1-量化指标 2-非量化指标 3-额外加分项 4-额外扣分项
        unit: '', //量化指标单位
        target: '', //目标值
        result_type: 'none', //(结果值录入者设置,none-不开启 self-被考核人 special-指定员工 supervisor-指定主管’
        result_employee_id: '', //结果值录入者id, 只有result_type为special时才需要指定
        weight: 0, //权重 0-100之间
        reviewer_id: '', //指定审批人(评分人)id
        point_limit: '', //分数上限,只对额外加分项以及额外扣分项有用
        // tag_ids:'',//指标id 数组或逗号分割
        // 自定义
        isReviewer: false, //
        index_level_id: '',
        record_ids: []
      };
      this.$refs[formName].resetFields();
      this.assignUser = {}; //指定人员
      this.assignScoreUser = {}; //指定评分人
      this.record_ids = [];
      this.isSave = true;
      this.isAddIndex = false;
    },
    //选择人员
    addUser(index) {
      if (index == 1) {
        this.selected.employee = this.assignUser.name ? [this.assignUser] : [];
      } else {
        this.selected.employee = this.assignScoreUser.name ? [this.assignScoreUser] : [];
      }
      this.isUserIndex = index;
      this.setAdministrator = true;
    },
    //获取指标分类
    getIndexType() {
      this.$axios('get', 'api/per/index/cate/list', { page: 0 }).then(res => {
        let data = res.data.data.list;
        let indexTypeList = [{ label: '全部', value: 0 }];
        data.forEach(item => {
          indexTypeList.push({ label: item.name, value: item.id });
        });
        this.indexTypeList = indexTypeList;
      });
    },
    showMessage(str) {
      this.$message.error(str);
    },
    // 提交
    getIsTemplate() {
      let dimensionalityList = this.dimension;
      let dimensionalityIndex = false; //判断维度是否有指标
      dimensionalityList.some(item => {
        if (item.index.length != 0) {
          dimensionalityIndex = true;
          return true;
        }
      });
      if (!dimensionalityIndex) {
        this.showMessage('维度中指标至少有一条');
        return false;
      }

      if (this.calc_dimension == 1) {
        //当维度参与时
        let indexSum = 0;
        dimensionalityList.forEach(item => {
          indexSum += Number(item.dimension_weight);
        });
        if (indexSum != 100) {
          this.showMessage('所有维度的权重相加要等于100%');
          return false;
        }
      }
      if (this.calc_type == 2 && this.calc_dimension == 0) {
        //当为加权计算，维度权重不参与时
        let indexSum = 0;
        let is = false; //是否有量化跟非量化
        dimensionalityList.forEach(item => {
          if (item.index.length > 0) {
            item.index.forEach(item2 => {
              if (item.index_type == 1 || item.index_type == 2) {
                is = true;
                indexSum += Number(item2.weight);
              }
            });
          }
        });
        if (indexSum != 100 && is) {
          this.showMessage('各维度中的所有指标权重相加要等于100%');
          return false;
        }
      }

      if (this.calc_type == 2 && this.calc_dimension == 1) {
        //当为加权计算，维度权重参与时
        let isSum = true;
        let str = '';
        dimensionalityList.forEach(item => {
          if (item.index_type == 1 || item.index_type == 2) {
            let indexSum = 0;
            if (item.index.length > 0) {
              item.index.forEach(item2 => {
                indexSum += Number(item2.weight);
              });
            }
            if (indexSum != 100) {
              str = item.name;
              isSum = false;
            }
          }
        });
        if (!isSum) {
          this.showMessage('“' + str + '” 的指标权重相加要等于100%');
          return false;
        }
      }
      dimensionalityList.forEach(item => {
        //重置数据
        item.target_index = item.target_index ? 1 : 0;
        if (item.index.length > 0) {
          item.index.forEach(item2 => {
            item2.type = item2.type;
            item2.per_remark = item2.per_remark;
            item2.remark = item2.remark;
            item2.target = item2.target;
            item2.result_type = item2.result_type;
            item2.result_employee_id = item2.result_employee_id;
            item2.unit = item2.unit;
            item2.weight = item2.weight;
            item2.reviewer_id = item2.reviewer_id; //指标的指定评分人id
            item2.need = 0;
          });
        }
      });
      // 判断指标里是否有指定评分人
      let isShowScore = false;
      dimensionalityList.some(item => {
        if (item.index.length > 0) {
          item.index.some(item2 => {
            if (item2.reviewer_id) {
              isShowScore = true;
              return true;
            }
          });
        }
      });
      this.process.special_scorer.enable = isShowScore ? 1 : 0; //取消流程中的指定评分人模块
      // console.log(dimensionalityList)
      this.dimensionalityList = JSON.stringify(dimensionalityList);
      // return
      this.isAdjustment = true;
    },
    //添加指标
    addIndex(e, index) {
      this.selectItemIndex = index;
      this.addIndexForm.type = e.index_type;
      this.isAddIndex = true;
    },
    //选择指标
    selectIndex(e, index) {
      this.selectItemIndex = index;
      this.indexType = e.index_type;
      this.ids = e.index.map(item => {
        //过滤已经选择的指标
        return item.id;
      });
      this.getIndexList(() => {
        this.isSelectIndex = true;
      });
    },
    //获取指标
    getIndexList(fun = function() {}) {
      let employeeMap = this.$getEmployeeList();
      this.$axios('get', '/api/per/index/list', {
        type: this.indexType,
        cate_id: this.cate_id,
        page: this.indexPage,
        page_size: 30,
        tag_ids: JSON.stringify(this.tabIds)
      }).then(res => {
        let list = res.data.data.list || [];
        let indexList = [];
        list.forEach(item => {
          if (this.ids.indexOf(item.id) < 0) {
            //过滤已经选择的指标
            item.check = false;
            item.result_employee_name = employeeMap[item.result_employee_id] ? employeeMap[item.result_employee_id].name : '';
            item.reviewer_name = employeeMap[item.reviewer_id] ? employeeMap[item.reviewer_id].name : '';
            item.custom = 0;
            indexList.push(item);
          }
        });
        if (list.length != 30) {
          this.isAfterLoading = true;
        }
        if (this.indexPage === 1) {
          this.indexList = indexList;
        } else {
          this.indexList = this.indexList.concat(indexList);
        }
        fun();
      });
    },
    // 计算维度
    getWeight() {
      let dimension = this.dimension;
      let sum1 = 0,
        sum2 = 0;
      dimension.forEach((item, index) => {
        if (item.index.length > 0) {
          item.index.forEach((item2, index2) => {
            if (index == this.wdIndex) {
              sum1 += Number(item2.weight);
            }
            sum2 += Number(item2.weight);
          });
        }
      });
      return [sum1, sum2];
    },
    getName(item) {
      return item.name + '(' + item.index.length + ')';
    }
  }
};
</script>

<style scoped="scoped">
.border {
  -webkit-appearance: none;
  background-color: #fff;
  background-image: none;
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  color: #c0c4cf;
  font-size: inherit;
  height: auto;
  outline: 0;
  padding: 0 15px;
  width: 330px;
  position: relative;
  cursor: pointer;
  background-color: #F5F7FA;
}
.border .font-flex-word {
  color: #606266;
}
.infinite-list-wrapper {
  height: calc(100vh - 270px);
  overflow: auto;
  overflow-x: hidden;
}
.all {
  position: relative;
  font-size: 14px;
  background-color: #fff;
  padding: 20px;
}
.main {
  margin-top: 20px;
}
.footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #fff;
  border-top: 1px solid #e8e8e8;
  padding: 12px 30px;
}
.wd-footer {
  padding: 12px 20px;
}
.btn {
  color: #26a2ff;
  border: 1px dashed #26a2ff;
}
.width-250 {
  width: 250px;
}
.inputDc {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  z-index: 9;
  cursor: pointer;
}
.ul {
  margin: 20px 0;
  margin-top: 0;
}
.li {
  border-bottom: 1px solid #ebebeb;
}
.li .flex-box-ce {
  padding: 15px 0;
}
.li:hover {
  background-color: #f5f7fa;
}
</style>
