<!--
 * @Author: 纪泽龙 jizelong@qq.com
 * @Date: 2022-09-16 17:07:30
 * @LastEditors: 纪泽龙 jizelong@qq.com
 * @LastEditTime: 2022-09-28 17:46:09
 * @FilePath: /danger-manage-web/src/views/trainingProgram/index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <div class="training-wrapper">
    <div class="top-title flex">
      <div class="item">
        <div class="text">培训管理</div>
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="addClick"
        >
          新增
        </el-button>
      </div>
    </div>

    <div>
      <Item
        v-for="item in list"
        :key="item.planId"
        :infoData="item"
        :personnelOptions="personnelOptions"
        @edit="edit"
        @save="itemSave"
        @deletePlan="deletePlan"
        @cancel="itemCancel"
        :isActiveId="isActiveId"
      />
    </div>

    <!-- <div v-for="item in list" :key="item.title">{{item.title}}</div> -->
    <el-dialog
      :title="dilogTitle"
      :visible.sync="addOpen"
      width="800px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :rules="rules" :model="form" label-width="120px">
        <el-form-item label="新增培训名称" prop="planName">
          <el-input v-model="form.planName"></el-input>
        </el-form-item>
        <el-form-item label="参与培训人员" prop="postIds">
          <el-checkbox-group v-model="form.postIds">
            <el-checkbox
              v-for="personnel in personnelOptions"
              :label="personnel.postId"
              :key="personnel.postId"
              >{{ personnel.postName }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="dialogSubmitForm">确 定</el-button>
        <el-button @click="dialogCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Item from "./components/Item";
import {
  getPersonnel,
  addPlan,
  getPlanList,
} from "@/api/educationPlanExam/trainingProgram";
// 培训计划管理
const personnelOptions = [
  "管理人员",
  "主要负责人",
  "入场新员工",
  "值组组长",
  "车间主任",
  "承包商",
  "实习学生",
  "参观人员",
  "专职应急救援人员",
];
export default {
  name: "trainingProgram",
  components: {
    Item,
  },
  data() {
    return {
      personnelOptions,
      isActiveId: 999,
      list: [],
      // 当点击编辑的时候，会记录最初始的
      oldInfoData: null,
      addOpen: false,
      dilogTitle: "新增培训",
      form: {
        planName: "",
        postIds: [],
      },
      rules: {
        planName: [
          {
            required: true,
            message: "新增培训名称不能为空",
            trigger: "change",
          },
        ],
        postIds: [
          {
            required: true,
            message: "最少选择一种参与培训人员",
            trigger: "change",
          },
        ],
      },
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    async init() {
      await this.getPersonnel();
      await this.getPlanList();
    },
    getPersonnel() {
      getPersonnel().then((res) => {
        this.personnelOptions = res.data.map((item) => {
          return {
            postId: item.postId,
            postName: item.postName,
          };
        });
      });
    },
    getPlanList() {
      return getPlanList().then((res) => {
        console.log(res.data);
        this.list = res.data.map((item) => {
          return {
            planId: item.planId,
            planName: item.planName,
            postIds: item.postList
              .filter((item) => item.ischeck)
              .map((item) => item.postId),
          };
        });
      });
    },
    addPlan(plan) {
      return addPlan(plan).then((res) => {
        if (res.code == 200) {
          console.log(res.data);
          const planId = +res.data;
          const obj = { planId, ...this.form };
          this.list.push(obj);
          this.addOpen = false;
          this.formReset();
        }
      });
    },
    edit(e) {
      // console.log(e);
      // 如果已经记录过oldInfoData
      if (this.oldInfoData) {
        // 就让上一个的那个数据还原
        this.itemCancel(this.oldInfoData, true);
      }
      // 然后记录一下
      const data = JSON.parse(e);
      this.oldInfoData = e;
      // 编辑状态改变
      console.log(e);
      this.isActiveId = data.planId;
    },
    itemSave(newValue) {
      // const data = JSON.parse(newValue);
      // editPlan(data).

      this.itemCancel(newValue);
    },
    // 删除
    deletePlan(value) {
      const data = JSON.parse(value);
      const index = this.list.findIndex((item) => item.planId == data.planId);
      this.list.splice(index, 1);
      // 只删除，别的什么也不干
      if(this.oldInfoData){
        // 如果有正在编辑还未保存，就让他还原
        // this.itemCancel(this.oldInfoData)
      }

    },
    // 取消,第二个值用来判断是否让isActiveId=999；
    itemCancel(value, isActiveIdChange = false) {
      // console.log(e);
      // 在这里转换，防止污染
      const data = JSON.parse(value);
      // 找到下标，然后改成初始值
      const index = this.list.findIndex((item) => item.planId == data.planId);
      this.list.splice(index, 1, data);
      // 取消的时候清空这里存储的初始值
      this.oldInfoData = null;
      if (isActiveIdChange) return;
      this.isActiveId = 999;
    },
    changeList() {},
    addClick() {
      this.addOpen = true;
    },
    dialogSubmitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          console.log(this.form);
          this.addPlan(this.form);
          // alert("submit!");
          // this.form.id = 10;
          // this.list.push(this.form);
          // this.addOpen = false;
          // this.formReset();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    dialogCancel() {
      this.addOpen = false;
      // this.formReset();
    },
    formReset() {
      this.form = {
        planName: "",
        postIds: [],
      };
    },
  },
};
</script>
<style lang="scss" scoped>
.training-wrapper {
  // width: 91.3%;
  width: 100%;
  height: calc(100vh - 50px);
  // background-color: red;
  overflow: auto;
  .top-title {
    padding-top: 14px;
    padding-bottom: 14px;
    width: 100%;
    justify-content: center;
    .item {
      width: 93.2%;
      max-width: 1600px;
      .text {
        margin-bottom: 10px;
      }
    }
  }
}
</style>
