<template>
  <div class="box-all">
    <PageHead :phName="packageName"></PageHead>
    <div class="main" v-loading="indexLoading">
      <template v-if="calc_type==1">
        <div class="yellow" style="text-align: right;">
          <span v-if="calc_dimension">总分=评分 X 维度权重，再累加</span>
          <span v-else>总分=评分直接累加</span>
        </div>
      </template>
      <template v-if="calc_type==2">
        <div class="yellow" style="text-align: right;">
          <span v-if="calc_dimension">总分=评分 X 维度权重 X 指标权重，再累加</span>
          <span v-else>总分=评分 X 指标权重，再累加</span>
        </div>
      </template>
      <div class="wds" v-for="(item, index) in dimensionalityList" :key="index">
        <div class="wd-header">
          <div class="flex-box-ce wd-title">
            <div class="flex-1 fontColorA" style="font-size: 18px;">{{ item.name }}</div>
            <div class="flex-box-ce titles">
              <div v-if="item.index_type == 1">量化指标</div>
              <div v-if="item.index_type == 2">非量化指标</div>
              <div v-if="item.index_type == 3">额外加分项</div>
              <div v-if="item.index_type == 4">额外扣分项</div>
              <div v-if="item.index_type != 3 && item.index_type != 4 && calc_dimension == 1">维度权重{{ item.dimension_weight }}%</div>
            </div>
          </div>
        </div>
        <el-table :data="item.index" style="width: 100%;margin-top: 14px;">
          <el-table-column prop="name" label="指标名称"></el-table-column>
          <el-table-column prop="per_remark" min-width="200" :label="item.index_type == 3 || item.index_type == 4 ? (item.index_type == 3 ? '加分标准' : '扣分标准') : '考核标准'">
            <template slot-scope="scope">
              <PreBox :value="scope.row.per_remark"></PreBox>
            </template>
          </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">
              <PreBox v-if="scope.row.target" :value="scope.row.target + '' + scope.row.unit"></PreBox>
            </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 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="wd-footer flex-box-ce">
          <div class="flex-box flex-1">
            <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>
            <el-button class="primaryBtn" @click="fzIndex(item, index)">
              <i class="el-icon-document-copy blue"></i>
              复制指标
            </el-button>
          </div>
          <div class="yellow" style="font-size: 16px;font-weight: 700;">
            共
            <span class="blue">{{ getIndexNum(index, 1) }}</span>
            项指标
            <span v-if="item.index_type == 1 || item.index_type == 2">
              ，所有指标权重合计：
              <span style="font-weight: 600;color: #222;">{{ getIndexNum(index, 2) }}</span>
              %
            </span>
          </div>
        </div>
      </div>
    </div>

    <footer class="footer flex-box-end">
      <el-button class="primaryBtn" @click="save(0)">暂存</el-button>
      <el-button type="primary" @click="save(1)">
        <span v-if="nodeId==1">提交</span>
        <span v-else>提交并同意</span>
      </el-button>
    </footer>

    <!-- 新增指标 -->
    <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 v-model="addIndexForm.name" placeholder="请输入指标名称" maxlength="30" show-word-limit></el-input>
          </el-form-item>
          <el-form-item :label="fromName" prop="per_remark">
            <el-input type="textarea" placeholder="请输入" 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" id="input3" @input="checkCountry2('input3')"></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" id="input4" @input="checkCountry2('input4')"></el-input>
          </el-form-item>
          <el-form-item label="目标值:" prop="target"v-if="addIndexForm.type == 1">
            <el-input v-model="addIndexForm.target" placeholder="请输入目标值"></el-input>
          </el-form-item>
          <el-form-item label="量化指标单位:" prop="unit" v-if="addIndexForm.type == 1">
            <el-input v-model="addIndexForm.unit" placeholder="请输入量化指标单位"></el-input>
          </el-form-item>
          <el-form-item 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 v-model="addIndexForm.result_type" placeholder="请选择指标类型" 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 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 type="textarea" placeholder="请输入" 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">
            <el-switch v-model="addIndexForm.isReviewer"></el-switch>
            <div style="position: relative;" v-if="addIndexForm.isReviewer">
              <el-input v-model="assignScoreUser.name" placeholder="请选择评分人" class="width-250"></el-input>
              <div @click="addUser(2)" class="inputDc"></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>
          <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="请选择">
              <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>
        </el-form>
      </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>

    <!-- 选择指标项 -->
    <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="indexLoading2" 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="indexLoading2" @click="selectCate()">确定</el-button>
      </template>
    </BrawerBox>

    <!-- 复制指标项 -->
    <BrawerBox drawerTitle="复制指标项" :showDrawer.sync="isShowFzindex">
      <template slot="main">
        <div class="flex-box-ce">
          <el-select v-model="cycle_type" filterable  class="flex-1">
            <el-option v-for="item in amhlList" :key="item.value" :label="item.label" :value="item.value"></el-option>
          </el-select>
          <el-select v-model="khId"  placeholder="请选择绩效考核" class="flex-2" style="margin-left: 10px;">
            <el-option v-for="item in jxList" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </div>
        <div style="margin-top: 10px;">
          <el-select v-model="wdIndex"  placeholder="请选择维度" style="width: 100%;">
            <el-option v-for="(item,index) in wdList" :key="(index+1)" :label="item.name" :value="(index+1)"></el-option>
          </el-select>
        </div>
        <template v-if="fzIndexList.length > 0" v-loading="loading">
          <div class="flex-box-ce li" style="margin-top: 20px;"><el-checkbox class="flex-box-ce" v-model="isAllSelectIndex" label="全选"></el-checkbox></div>
          <ul class="ul">
            <li v-for="(item, index) in fzIndexList" :key="index" class="flex-box-ce li">
              <el-checkbox class="flex-box-ce" v-model="item.check" :label="item.name"></el-checkbox>
            </li>
          </ul>
        </template>
        <NoData v-else></NoData>
      </template>
      <template slot="footer">
        <el-button plain @click="isShowFzindex = false">取消</el-button>
        <el-button plain @click="replaceIndex()">替换</el-button>
        <el-button type="primary" :loading="loading" @click="selectFzIndex()">确定</el-button>
      </template>
    </BrawerBox>

    <!-- 评分人 -->
    <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 PageHead from '@/components/public/PageHead'; //头部---返回
import EmployeeSelector from '@/components/public/EmployeeSelector';
import BrawerBox from '@/components/public/BrawerBox';
import ClassSet from '@/components/public/ClassSet';
export default {
  components: { PageHead,BrawerBox,EmployeeSelector,ClassSet },
  name: 'formulate',
  data() {
    return {
      id: '', //员工考核记录id
      packageName: '' ,//考核包名称 结果值录入用到
      userId:0,//当前被考核人
      nodeId:'',//当前节点ID

      calc_type:1,//加权还是加和
      calc_dimension:0,//是否参与权重
      dimensionalityList: [],
      selectItemIndex: 0, //选择的维度下标，
      selectItemIndex_index: 0, //选择的指标下标，

      indexLoading: false,
      isSelectIndex: false, //选择指标弹窗控制
      isAllSelectIndex: false, //是否全选指标
      cate_id: 0,
      indexType: 0, //指标分类
      indexList: [],
      indexTypeList: [],

      indexLoading2: false,
      indexPage:1,
      isAfterLoading:false,//是否加载完

      // 添加指标
	selected2:{ employee: [], dept: [] },//已经选择人员
	record_ids:[],
	isShowRecord:false,
      isAddIndex: false,
      addIndexForm: {
        // cate_id: '', //指标分类id
        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-100之间
        reviewer_id: '', //指定审批人(评分人)id
        point_limit: '', //分数上限,只对额外加分项以及额外扣分项有用
        // tag_ids:'',//指标id 数组或逗号分割
        // 自定义
        isReviewer: false ,//
        index_level_id: '',//指标等级
		record_ids:[],//管理记录人
      },
      fromName: '考核标准:',
      assignUser: {}, //指定人员
      assignScoreUser: {}, //指定评分人
      selected: { employee: [], dept: [] }, //已经选择人员
      setAdministrator: false,
      isUserIndex: 1, //判断选择什么人，1是指定人员，2是评分人
      isSave: true, //判断指标是提交还是编辑
      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' }]
      },
      ids: [], //需要过滤的指标ID
      isTan: true,
      sumText: {
        num: 0,
        val: 0
      },
      // 标签相关
      tabs: [],
      tabIds: [],
      loading: false,
      // 复制指标相关
      isShowFzindex:false,
      amhlList: [
        { value: 2, label: '月度' },
        { value: 3, label: '季度' },
        { value: 4, label: '半年度' },
        { value: 5, label: '年度' },
        { value: 1, label: '日' },
        { value: 6, label: '自定义' }
      ],
      cycle_type:2,
      jxList:[],//绩效包集合
      wdList:[],//维度集合
      fzIndexList:[],
      khId:0,//考核Id
      wdIndex:'',//维度下标
      wdName:'',

      inputs: [],//分值区间(只读)
      levelOptions: [],//指标等级列表
    };
  },
  watch: {
    isAllSelectIndex(val) {
      this.indexList.forEach(item => {
        item.check = val;
      });
      this.fzIndexList.forEach(item => {
        item.check = val;
      });
    },
    'addIndexForm.type'(val) {
      if (val == 3) {
        this.fromName = '加分标准:';
      } else if (val == 4) {
        this.fromName = '扣分标准:';
      } else {
        this.fromName = '考核标准:';
      }
    },
    cate_id() {
      this.indexPage=1;
      this.isAfterLoading=false;
      this.getIndexList();
    },
    isSelectIndex(val) {
      //关闭选择指标时清空过滤的指标
      if (!val) {
        this.ids=[];
        this.tabIds=[];
        this.isAfterLoading=false;
        this.isAllSelectIndex=false
        this.cate_id=0
        this.indexPage=1;
      }
    },
    isShowFzindex(val) {
      //关闭选择指标时清空过滤的指标
      if (!val) {
        this.isAllSelectIndex = false;
        this.cycle_type=2;
        this.jxList=[];//绩效包集合
        this.wdList=[];//维度集合
        this.fzIndexList=[];
        this.khId=0;//考核Id
        this.wdIndex='';//维度下标
      }
    },
    tabIds(val) {
      this.indexPage=1;
      this.isAfterLoading=false;
      this.isAllSelectIndex=false
      this.getIndexList();
    },
    // 复制指标相关
    cycle_type(){
      this.sle()
    },
    khId(val){
      if(val){
         this.getWdList()

      }else{
        this.wdList=[];
        this.wdIndex='';
        this.fzIndexList=[];
      }
    },
    wdIndex(val){//根据维度下标，找出该维度下的指标
      this.$nextTick(()=>{
        if(val){
          let employeeMap = this.$getEmployeeList();
          var indexs=this.wdList[val-1].index;
          indexs.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.fzIndexList=indexs;
        }
      })
    },
	record_ids(val) {
	   let ids=val.map(item=>{
	     return item.id
	   })
	   this.addIndexForm.record_ids=ids||'';
	},
  },
  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
      }
      // console.log(this.levelOptions);
    },
    disabled () {
      return this.indexLoading2||this.isAfterLoading
    }
  },
  created() {
    this.getTagList();
    this.getIndexType();
    if (this.$route.query.id) {
      this.id = this.$route.query.id;
      this.packageName = this.$route.query.packageName;
      this.nodeId=this.$route.query.nodeId
      this.getPackageDtail();
    }
     this.gradeSetList();
  },
  methods: {
	  isShowRecordFun(){
	    this.selected2.employee=this.record_ids;
	    this.isShowRecord=true;
	  },
	  confirmAdministrator2(e){
	    this.record_ids=e.employee.length>0?e.employee:[]
	  },
    //指标等级列表
    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
        }
      })
    },
    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
      })
    },
    // 复制指标相关
    sle(fun = function() {}){
      this.indexLoading = true;
      let data={
        cycle_type:this.cycle_type,
        employee_id_code:this.$returnCode(this.userId),
        page: 0
      }
      this.$axios('get', 'api/per/package/list', data).then(res => {
           this.jxList=res.data.data.list
           if(this.jxList.length>0){
             this.khId=res.data.data.list[0].id
           }else{
             this.khId=''
           }
           fun();
      }).finally(()=>{
           this.indexLoading = false;
      });
    },
    getWdList(){
       this.wdList=[];
       this.wdIndex='';
       this.fzIndexList=[];
       this.$axios('get', '/api/per/package/employee/info', {package_id:this.khId,employee_id_code:this.$returnCode(this.userId)}).then(res => {
            let data = res.data.data;
            let indexs=[];
            let wdList=[];
            // if(data.doing_id==11){//已经完成的考核记录
              data.dimension.forEach(item => {
                if (item.index.length > 0&&item.index_type==this.indexType) {  //查找当前考核人的历史考核记录
                  wdList.push(item)
                }
              });
            // }
            this.wdList=wdList;
            this.wdIndex=wdList.length>0? 1:'';
       }).finally(()=>{
            this.indexLoading = false;
      });
    },
    selectFzIndex(){
      let fzIndexList = JSON.parse(JSON.stringify(this.fzIndexList));
      let arr = [];
      fzIndexList.forEach(item => {
        if (item.check) {
          arr.push(item);
        }
      });
      this.dimensionalityList[this.selectItemIndex].index.push(...arr);
      this.isShowFzindex = false;
    },
    replaceIndex(){
      this.$confirm('已选的历史指标将会替换【'+this.wdName+'】内的所有指标，确认替换吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          let fzIndexList = JSON.parse(JSON.stringify(this.fzIndexList));
          let arr = [];
          fzIndexList.forEach(item => {
            if (item.check) {
              arr.push(item);
            }
          });
          this.dimensionalityList[this.selectItemIndex].index=arr;
          this.isShowFzindex = false;
      }).catch(() => {});
    },
    // 保存||编辑指标
    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:[],//指定跟踪管理记录人 数组
          };
          if (val == 1) {
            this.dimensionalityList[this.selectItemIndex].index.push(index);
          } else {
            this.dimensionalityList[this.selectItemIndex].index.splice(this.selectItemIndex_index, 1, index); //替换元素
          }
          this.closeAddIndexForm(formName);
        }
      });
    },
    //获取指标分类
    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;
      });
    },
    //获取标签列表
    getTagList() {
      this.$axios('get', '/api/per/index/tag').then(res => {
        this.tabs = res.data.data.list || [];
      });
    },
    closeAddIndexForm(formName) {
      // 清空数据
      this.$nextTick(()=>{
        this.$refs[formName].resetFields();
      })
      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 ,//
         record_ids:[]
      };
      this.assignUser = {}; //指定人员
      this.assignScoreUser = {}; //指定评分人
      this.isSave = true;
      this.isAddIndex = false;
      this.record_ids=[];
    },
    checkCountry2(str) {
      //解决v-model与oninput同时使用失效问题
      var dom = document.getElementById(str);
      var value = dom.value;
      //写你自己的输入规则
      value = value.replace(/[^\d]/g, '');
      dom.value = value; //输入框赋值
      this.addIndexForm.point_limit = dom.value;
    },
    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 = '') : (this.addIndexForm.weight = '');
        this.$message.error('不能大于100');
        return false;
      }
      dom.value = value; //输入框赋值
      str == 'input' ? (this.dimensionalityForm.dimension_weight = dom.value) : (this.addIndexForm.weight = dom.value);
    },
    //复制指标
    fzIndex(e, index) {
      this.selectItemIndex = index;
      this.indexType = e.index_type;
      this.wdName=e.name;
      this.sle(() => {
        this.isShowFzindex = 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.indexLoading = true;
      this.getIndexList(() => {
        this.indexLoading = false;
        this.isSelectIndex = true;
      });
    },
    //删除指标
    deleteIndex(e, index) {
      this.$confirm('确定删除该指标', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          this.dimensionalityList[index].index.splice(e.$index, 1);
      }).catch(() => {});
    },
    //编辑指标
    compileIndex(e, index) {
      this.selectItemIndex = index;
      this.selectItemIndex_index = e.$index;
      let obj = e.row;
      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:[],
      };
      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;
    },
    //保存人员
    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;
    },
    //筛选指标分类
    selectCate() {
      this.loading=true;
      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.dimensionalityList[this.selectItemIndex].index.push(...arr);
      this.isSelectIndex = false;
      this.loading=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;
    },
    load () {
      this.indexLoading2 = true
      this.indexPage=this.indexPage+1;
      setTimeout(() => {
        this.getIndexList();
        this.indexLoading2 = false
      }, 500)
    },
    //获取指标分类
    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;
      });
    },
    //获取指标
    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();
       })
    },
    // 统计指标
    getIndexNum(index, type) {
      let num = this.dimensionalityList[index].index.length;
      let val = 0;
      if (type == 1) {
        return num;
      } else {
        this.dimensionalityList[index].index.forEach(item => {
          val += Number(item.weight);
        });
        return val;
      }
    },

    // 提交
    save(num) {
      let dimensionalityList = this.dimensionalityList
      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;
          let is = false; //是否有量化跟非量化
          dimensionalityList.forEach(item => {
            if (item.index_type == 1 || item.index_type == 2) {
              is = true;
              indexSum += Number(item.dimension_weight);
            }
          });
          if (indexSum != 100 && is) {
            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 data={
        id:this.id,
        node_id:this.nodeId,
        submit:num,
        dimension:JSON.stringify(dimensionalityList),
      }
      this.$axios('post', 'api/per/package/target_set', data).then(res => {
            this.$message.success(num==1?'制定成功':'暂存成功');
            this.$router.go(-1);
      })
    },
    showMessage(str) {
      this.$message.error(str);
    },
    getPackageDtail() {
      let employeeMap = this.$getEmployeeList();
      this.$axios('get', '/api/per/package/employee/info', { id: this.id }).then(res => {
        let data = res.data.data;
        data.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.userId=res.data.data.relevance_employee.id;
        this.calc_dimension=data.calc_dimension;
        this.calc_type=data.calc_type;
        this.dimensionalityList = data.dimension;
      });
    }
  }
};
</script>
<style scoped lang="scss">
  .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;
  }
  .border .font-flex-word{
    color: #606266;
  }
  .infinite-list-wrapper{
    height: calc(100vh - 270px);
    overflow:auto;
    overflow-x:hidden
  }
.box-all {
  position: relative;
  font-size: 14px;
  background-color: #fff;
  padding: 20px;
}
.main {
  margin-bottom: 60px;
}
.footer {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #fff;
  border-top: 1px solid #e8e8e8;
  padding: 12px 30px;
}
.pop-up{
  position: sticky;
  top: -30px;
  z-index: 999;
  background-color: #fff;
  padding: 10px 0;
}
.diy-tip {
  margin-bottom: 15px;
  border: 1px solid #e6a23c;
  padding: 20px 16px;
  background-color: #fdf6ec;
  p {
    color: #e6a23c !important;
    font-size: 15px;
    margin: 0 !important;
    padding-bottom: 4px;
  }
}
::v-deep .el-dialog__header {
  padding: 20px;
  border-bottom: 1px solid #ebebeb;
}
::v-deep .el-dialog__body {
  padding: 20px;
  padding-top: 0px;
}
.all {
  font-size: 14px;
  padding: 20px;
}
.title {
  font-size: 18px;
  font-weight: 500;
  margin-bottom: 20px;
}
.popover-img {
  width: 100%;
  height: 200px;
}
.el-icon-warning {
  color: #ccc;
}
.inputDc {
  position: absolute;
  top: 0;
  right: 0;
  left: 0;
  bottom: 0;
  z-index: 9;
  cursor: pointer;
}
.inputDc1 {
  position: absolute;
  top: 0;
  left: 85px;
  bottom: 0;
  z-index: 9;
}
.inputDc2 {
  position: absolute;
  top: 0;
  left: 195px;
  bottom: 0;
  z-index: 9;
}
.content {
  /* background-color: #f9fdff; */
  padding: 20px;
  margin-bottom: 20px;
  border: 1px solid #ecf5ff;
  border-radius: 4px;
}
.message {
  background-color: #ecf5ff;
  height: 100px;
}
.message-text {
  font-size: 16px;
  color: #222;
  margin-bottom: 8px;
}
.blue {
  cursor: pointer;
  padding: 0 5px;
}
.width-250 {
  width: 250px;
}
.wd-header {
  background-color: #ECF5FF;
}
.wd-header i {
  font-size: 24px;
  margin-left: 15px;
  cursor: pointer;
}
.wds {
  border: 1px solid #ebebeb;
  margin-top: 20px;
}
.wd-title {
  padding: 12px 20px;
  // border-bottom: 1px solid #ebebeb;
  color: #777777;
}
.wd-footer {
  padding: 12px 20px;
}
.titles div {
  padding-right: 12px;
  margin-right: 12px;
  border-right: 1px solid #ccc;
}
.titles div:last-child {
  border-right: none;
}
.el-icon-delete-solid:hover {
  color: #f56c6c;
}
.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;
}
::v-deep .el-textarea__inner::-webkit-scrollbar {
  width: 6px;
  height: 4px;
}
/*外层轨道。可以用display:none让其不显示，也可以添加背景图片，颜色改变显示效果*/
::v-deep .el-textarea__inner::-webkit-scrollbar-track {
  width: 6px;
  background-color: #fff0;
  -webkit-border-radius: 2em;
  -moz-border-radius: 2em;
  border-radius: 2em;
}
/*滚动条的设置*/
::v-deep .el-textarea__inner::-webkit-scrollbar-thumb {
  background-color: #fff0;
  background-clip: padding-box;
  -webkit-border-radius: 2em;
  -moz-border-radius: 2em;
  border-radius: 2em;
}
/*滚动条移上去的背景*/
::v-deep .el-textarea__inner:hover::-webkit-scrollbar-thumb {
  background-color: rgba(144, 147, 153, 0.3);
  height: 20px;
}

</style>
