<template>
  <div>
    <basic-container>
      <div class="content-box">
        <leftMentInfo @select="select" v-if="!isCSM" :platformShow="true" />
        <!-- csm管理员 -->
        <leftMenu v-if="isCSM" @select="select" />
        <div class="right-content">
          <el-button
            type="primary"
            @click="
              dialogVisible = true;
              type = 0;
            "
            >新增</el-button
          >
          <div class="rule-list">
            <div
              v-for="(item, index) in ruleList"
              :key="index"
              class="rule-item"
            >
              <div class="item-l">
                <div class="item-info">
                  <span class="name">任务类型：</span>
                  <p>
                    {{ item.taskTypeNames && item.taskTypeNames.join("，") }}
                  </p>
                </div>
                <div class="item-info">
                  <span class="name">起始积分包值：</span>
                  <p>{{ item.startScore }}</p>
                </div>
                <div class="item-info">
                  <span class="name">完成上限：</span>
                  <p>{{ item.limitPercent }}</p>
                </div>
              </div>
              <div class="item-r">
                <div class="edit" @click="editClick(item)">编辑</div>
                <div class="delete" @click="deleteClick(item)">删除</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </basic-container>
    <el-dialog
      :visible.sync="dialogVisible"
      width="60%"
      :before-close="handleClose"
    >
      <div class="add-content">
        <p class="title">任务类型：</p>
        <div class="detail">
          <div
            v-for="(item, index) in typeList"
            :key="index"
            class="detail-uls"
          >
            <div class="item-title">
              {{ item.categoryName }}
            </div>
            <div class="item-lis">
              <span
                v-for="(iten, indey) in item.children"
                :key="indey"
                :class="{ active: getIsAct(iten.taskTypeId, item.category) }"
                @click="itemClick(iten.taskTypeId, item.category)"
              >
                {{ iten.taskTypeName }}
              </span>
            </div>
          </div>
        </div>
        <div class="txt">
          当积分包值达到
          <input
            type="text"
            v-model="startScore"
            class="dia-inp"
            oninput="value=value.replace(/[^\d]/g,'')"
          />
          时，所选任务类型完成总值不得大于该积分包值的
          <input
            type="text"
            v-model="limitPercent"
            class="dia-inp"
            oninput="value=value.replace(/[^\d]/g,'')"
          />
          %
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="putData">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import leftMentInfo from "@/views/components/leftMentInfo";
import leftMenu from "@/views/components/teamManagement/leftMenu";
import {
  getTaskType,
  putData,
  getPercentRule,
  delRule,
  getEditRule,
  editRule
} from "@/api/percentage.js";
export default {
  components: { leftMentInfo, leftMenu },
  data() {
    return {
      type: 0,
      dialogVisible: false,
      batchId: "",
      startScore: "",
      ruleList: [],
      limitPercent: "",
      batchId: "",
      typeList: [],
      selectId: [],
      deptId: "",
      isCSM: false
    };
  },
  created() {
    const userInfo = this.$store.getters.userInfo;
    if (userInfo.roles.includes(19)) {
      this.isCSM = true;
    }
  },
  methods: {
    select(info) {
      console.log("info", info);
      this.typeList = [];
      if (info) {
        this.deptId = info.deptId;
        this.getData();
        this.getPercentRule();
      }
    },

    handleClose() {
      this.selectId = [];
      this.startScore = "";
      this.limitPercent = "";
      this.type = 0;
      this.batchId = "";
      this.dialogVisible = false;
    },
    async getData() {
      const res = await getTaskType({
        deptId: this.deptId
      });
      if (res.data.code === 0) {
        this.typeList = res.data.data;
      }
    },
    async getPercentRule() {
      const res = await getPercentRule({
        deptId: this.deptId
      });
      if (res.data.code === 0) {
        this.ruleList = res.data.data;
      }
    },
    itemClick(taskTypeId, category) {
      let index = this.selectId.findIndex(
        item => item.subCategory === category
      );
      if (index === -1) {
        let obj = {
          subCategory: category,
          rule: {
            taskTypeIds: [taskTypeId]
          }
        };
        this.selectId.push(obj);
      } else {
        let curr = this.selectId[index].rule.taskTypeIds;
        if (!curr) return;
        if (curr.indexOf(taskTypeId) > -1) {
          this.selectId[index].rule.taskTypeIds.splice(
            curr.indexOf(taskTypeId),
            1
          );
          if (this.selectId[index].rule.taskTypeIds.length === 0) {
            this.selectId.splice(index, 1);
          }
        } else {
          this.selectId[index].rule.taskTypeIds.push(taskTypeId);
        }
      }
    },
    getIsAct(taskTypeId, category) {
      let index = this.selectId.findIndex(
        item => item.subCategory === category
      );
      if (index !== -1) {
        let curr = this.selectId[index].rule.taskTypeIds;
        if (curr && curr.includes(taskTypeId)) {
          return true;
        }
      }
      return false;
    },
    async putData() {
      if (this.selectId.length === 0) {
        this.$message.error("请选择任务类型！");
        return;
      }
      if (!this.startScore || !this.limitPercent) {
        this.$message.error("请完善信息！");
        return;
      }
      this.selectId.forEach(item => {
        item.deptId = this.deptId;
        item.rule.startScore = this.startScore;
        item.rule.limitPercent = this.limitPercent;
      });

      let res;
      let message;
      if (this.type === 0) {
        res = await putData(this.selectId);
        message = "添加成功！";
      } else {
        console.log("c");
        this.selectId.forEach(item => (item.batchId = this.batchId));
        res = await editRule(this.selectId);
        message = "编辑成功！";
      }

      if (res.data.code === 0) {
        this.$message.success(message);
        this.handleClose();
        this.getPercentRule();
      }
    },
    async deleteClick(item) {
      const res = await delRule(item.batchId);
      if (res.data.code === 0) {
        this.$message.success("删除成功！");
        this.getPercentRule();
      }
    },
    async editClick(item) {
      this.type = 1;
      const res = await getEditRule(item.batchId);
      this.batchId = item.batchId;
      if (res.data.code === 0) {
        this.selectId = res.data.data;
        console.log("res", res.data.data);
        this.limitPercent = this.selectId[0].rule.limitPercent;
        this.startScore = this.selectId[0].rule.startScore;
        this.dialogVisible = true;
      }
    }
  }
};
</script>

<style scoped lang="scss">
.content-box {
  display: flex;
  height: 78vh;
  overflow: hidden;
  .right-content {
    flex: 1;
    margin: 30px;
    height: 100%;
    width: 100%;
    overflow: auto;
    .rule-list {
      .rule-item {
        border-bottom: 1px solid #e7e7e7;
        padding: 10px 20px;
        display: flex;
        .item-l {
          flex: 1;
          .item-info {
            margin-top: 10px;
            display: flex;
            span {
              color: #333;
              font-weight: 600;
            }
            p {
              color: #d7d7d7;
            }
          }
        }
        .item-r {
          width: 50px;
          margin-left: 15px;
          .edit {
            margin-top: 15px;
            cursor: pointer;
            color: #02a7f0;
          }
          .delete {
            margin-top: 15px;
            cursor: pointer;
            color: #d9001b;
          }
        }
      }
    }
  }
}
.add-content {
  .detail {
    margin-top: 20px;
    .detail-uls {
      margin-bottom: 15px;
      display: flex;
      .item-title {
        width: 150px;
        text-align: right;
        margin-right: 30px;
      }
      .item-lis {
        flex: 1;
        span {
          cursor: pointer;
          display: inline-block;
          margin-bottom: 3px;
          margin-right: 3px;
          background-color: #f2f2f2;
          height: 30px;
          line-height: 30px;
          font-size: 12px;
          color: #333;
          padding: 0 5px;
          border-radius: 5px;
          &.active {
            background-color: #f59a23;
          }
        }
      }
    }
  }
  .dia-inp {
    width: 50px;
    height: 25px;
  }
}
</style>
