<template>
  <div id="baseAddPaper">
    <div class="boxWidth">
      <el-form
        label-position="right"
        label-width="120px"
        :model="listDataIns"
        :rules="rules"
        ref="listDataIns"
      >
        <el-form-item label="适用业务层级" :required="true">
          <el-select
            disabled
            v-model="eduType"
            placeholder="请选择教育类型"
            @change="changeEduType"
          >
            <el-option
              v-for="(item, index) in eduTypeOptions"
              :key="index"
              :label="item.educationName"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <el-select
            disabled
            v-model="courType"
            placeholder="请选择业务层次"
            @change="changecourseType"
          >
            <el-option
              v-for="(item, index) in newCourTypeOptions"
              :key="index"
              :label="item.projectName + '-' + item.businessName"
              :value="item.id"
            >
            </el-option>
          </el-select>
          <el-popover
            ref="popovers"
            placement="bottom"
            width="200"
            trigger="click"
            @show="showHandle"
            @hide="hideHandle"
            :disabled="courType ? false : true"
          >
            <el-radio-group v-model="sujectApis">
              <el-radio
              disabled
                v-for="(item, index) in newSujectOption"
                :label="item.newId"
                :key="index"
                >{{ item.subjectName }}</el-radio
              >
            </el-radio-group>
            <div style="display: block; text-align: center; margin-top: 10px">
              <el-button size="mini" type="primary" @click="submitSujectArray" v-if="false"
                >确定</el-button
              >
            </div>
            <el-button slot="reference" style="margin-left: 12px" @click="getMessage"
              >请选择科目</el-button
            >
          </el-popover>
        </el-form-item>
        <el-form-item label="">
          <!-- <span v-if="Object.keys(newSujectApis).length == 0"
            >未选项目类型</span
          > -->
          <div
            v-if="Object.keys(newSujectApis).length !== 0"
            style="display: flex"
          >
            <div class="listBoxStys">
              {{
                newSujectApis.educationName +
                " - " +
                newSujectApis.projectName +
                " - " +
                newSujectApis.businessName +
                " - " +
                newSujectApis.subjectName
              }}
              <i class="el-icon-error closeIcons" @click="closeType" v-if="false"></i>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="试卷类型" prop="examPaperId">
          <el-select
            v-model="listDataIns.examPaperId"
            placeholder="请选择试卷类型"
          >
            <el-option
              v-for="item in paperexam"
              :key="item.paperId"
              :label="item.paperName"
              :value="item.paperId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地域（省）" prop="provinceId">
          <el-select v-model="listDataIns.provinceId" placeholder="请选择省">
            <el-option
              v-for="item in areas"
              :key="item.areaId"
              :label="item.areaName"
              :value="item.areaId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item
          label="地域（市）"
          prop="cityId"
          v-if="listDataIns.provinceId"
        >
          <el-select v-model="listDataIns.cityId" placeholder="请选择市">
            <el-option
              v-for="item in optionsCity"
              :key="item.areaId"
              :label="item.areaName"
              :value="item.areaId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="年份" prop="year">
          <el-date-picker
            v-model="listDataIns.year"
            type="year"
            value-format="yyyy"
            placeholder="请选择年份"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="标题前缀" prop="prefixName">
          <el-input v-model="listDataIns.prefixName"></el-input>
        </el-form-item>
        <el-form-item label="试卷名称" prop="examName">
          <el-input v-model="listDataIns.examName"></el-input>
        </el-form-item>
        <el-form-item label="是否发布" prop="publishStatus">
          <el-radio-group v-model="listDataIns.publishStatus">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="0">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button @click="backPage">取消</el-button>
          <el-button type="primary" @click="submitIns('listDataIns')"
            >确定</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import tableList from "@/components/tableList";
import { mapGetters } from "vuex";
export default {
  components: { tableList },
  data() {
    return {
      listDataIns: {
        publishStatus: 1,
        year: "",
      }, //   弹窗数据
      eduTypeOptions: [], //教育类型数据
      projectTypeOptions: [], //项目类型数据
      courTypeOptions: [], //业务层次数据
      newCourTypeOptions: [], //当前业务层次数据
      sujectOption: [], //科目数据
      newSujectOption: [], //当前科目数据数据
      eduType: "", //当前选中教育类型
      courType: "", //当前选中业务层次

      newSujectApis: {},
      sujectApis: "", //当前存在的科目 例如'19-2'
      optionsCity: [],
      //表单验证
      rules: {
        examPaperId: [
          {
            required: true,
            message: "请选择试卷类型",
            trigger: ["change", "blur"],
          },
        ],
        examName: [
          { required: true, message: "请输入试卷名称", trigger: "blur" },
        ],
        publishStatus: [
          { required: true, message: "请选择是否发布", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["paperexam", "areas", "cityList"]),
  },
  mounted() {
    this.getDict();
    // this.search();
  },
  // watch: {
  //   "listDataIns.provinceId": {
  //     handler(newVal, oldVal) {
  //       this.changeCity(newVal);
  //     },
  //     // 立即处理 进入页面就触发
  //     immediate: true,
  //   },
  // },
  methods: {
    getMessage() {
      if (!this.courType) {
        this.$message.warning("请先选择业务层级");
      }
    },
    search() {
      this.$api.obtainbankexam(this.$route.query.id).then((res) => {
        res.data.year = res.data.year + "";
        this.listDataIns = res.data;
      });
      this.$api
        .inquirebankexamListbusiness({ type: 2, majorId: this.$route.query.id })
        .then((res) => {
          this.eduType = res.rows[0].educationTypeId;
          this.changeEduType();
          this.courType = res.rows[0].businessId;
          this.changecourseType();
          this.newSujectApis = res.rows[0];
          this.sujectApis =
            res.rows[0].businessId + "-" + res.rows[0].subjectId;
        });
    },
    // changeCity(newVal) {
    //   console.log(newVal)
    //   this.optionsCity = this.cityList.filter((item) => {
    //     return item.parentId === newVal;
    //   });
    // },
    changeTypes() {
      var self = this;
      if (!this.sujectApis) {
        self.newSujectApis = {};
        return;
      }
      this.courTypeOptions.map((items) => {
        if (items.id === this.sujectApis.split("-").map(Number)[0]) {
          var obj = {
            type: 2,
            educationTypeId: items.educationId,
            educationName: items.educationName,
            projectId: items.projectId,
            projectName: items.projectName,
            businessId: items.id,
            businessName: items.businessName,
          };
          self.sujectOption.map((i) => {
            if (
              i.id === self.sujectApis.split("-").map(Number)[1] &&
              i.courseArrays.indexOf(items.projectId) !== -1
            ) {
              obj.subjectName = i.subjectName;
              obj.subjectId = i.id;
            }
          });
          self.newSujectApis = obj;
        }
      });
    },
    submitSujectArray() {
      this.changeTypes();
      this.$refs.popovers.doClose();
    },
    showHandle() {
      this.sujectApis = "";
      this.newSujectOption.map((item) => {
        item.newId = this.courType + "-" + item.id;
        if (item.newId === this.sujectApis) {
          console.log("成功");
        }
      });
      this.sujectApis =
        this.newSujectApis.businessId + "-" + this.newSujectApis.subjectId;
    },
    hideHandle() {},
    getDict() {
      new Promise((resolve, reject) => {
        this.$api.inquireCourseEducationType({ status: 1 }).then((res) => {
          this.eduTypeOptions = res.rows;
        });
        this.$api.inquireCourseProjectType({ status: 1 }).then((res) => {
          this.projectTypeOptions = res.rows;
        });
        this.$api.inquirebusinessList({ status: 1 }).then((res) => {
          this.courTypeOptions = res.rows;
          this.newCourTypeOptions = res.rows;
        });
        this.$api.inquireCourseSubject({ status: 1 }).then((res) => {
          res.rows.map((item, index) => {
            var array = [];
            item.courseProjectTypes.map((items, indexs) => {
              array.push(items.id);
            });
            item.courseArrays = array;
          });
          this.sujectOption = res.rows;
          resolve()
        });
      }).then((res) => {
        this.search()
      });
    },
    changeEduType() {
      if (!(this.courType === undefined || this.courType === "")) {
        this.courType = "";
      }
      var arrays = [];
      this.courTypeOptions.map((item) => {
        if (item.educationId === this.eduType) {
          arrays.push(item);
        }
      });
      this.newCourTypeOptions = arrays;
    },
    changecourseType() {
      this.newCourTypeOptions.map((item, index) => {
        if (item.id === this.courType) {
          this.eduType = item.educationId;
          var array = [];
          this.sujectOption.map((items, indexs) => {
            if (items.courseArrays.indexOf(item.projectId) !== -1) {
              array.push(items);
            }
          });
          this.newSujectOption = array;
        }
      });
      var arrays = [];
      this.courTypeOptions.map((item) => {
        if (item.educationId === this.eduType) {
          arrays.push(item);
        }
      });
      this.newCourTypeOptions = arrays;
      this.$refs.popovers.doClose();
    },
    submitIns(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (JSON.stringify(this.newSujectApis) === "{}") {
            this.$message.error("请选择适用业务层级");
            return;
          }
          this.rulesTableSumbit();
        } else {
          return false;
        }
      });
    },
    async rulesTableSumbit() {
      var data1 = {
        examId: this.listDataIns.examId,
        examPaperId: this.listDataIns.examPaperId,
        examName: this.listDataIns.examName,
        publishStatus: this.listDataIns.publishStatus,
        prefixName: this.listDataIns.prefixName,
        businessList: [this.newSujectApis],
        provinceId: this.listDataIns.provinceId,
        cityId: this.listDataIns.cityId,
        year: this.listDataIns.year,
      };
      this.$api.editbankexam(data1).then((res) => {
        this.$message.success("修改成功");
        setTimeout(() => {
          this.$router.go(-1);
        }, 500);
      });
    },
    backPage() {
      // this.$store.dispatch('tagsView/delView', this.$router.currentRoute)
      this.$router.go(-1);
    },
    closeType() {
      this.sujectApis = "";
      this.changeTypes();
    },
  },
};
</script>

<style lang="less" scoped>
.boxWidth {
  width: 700px;
}
.numInputs {
  width: 150px;
}
.checkboxSty {
  max-height: 210px;
  overflow: auto;
  display: flex;
  flex-direction: column;
}
.listBoxStys {
  flex-shrink: 0;
  padding: 0px 10px;
  border-radius: 8px;
  border: 1px solid #eee;
  margin-right: 10px;
  margin-bottom: 6px;
}
.closeIcons {
  color: red;
  cursor: pointer;
  margin-left: 6px;
}
.ach {
  display: flex;
  align-items: center;
  overflow: hidden;
}
.clh {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}
.imgBoxins {
  width: 375px;
  height: 220px;
  text-align: center;
  img {
    height: 100%;
  }
}
.iconStsz {
  font-size: 40px;
  color: #67c23a;
  cursor: pointer;
}
.BusBoxs {
  min-height: 100px;
  border: 1px solid #a4a4a4;
  margin-bottom: 10px;
  border-radius: 8px;
  max-height: 280px;
  padding: 10px;
  overflow-y: auto;
  .fot_Le {
    border-radius: 12px;
    border: 1px solid yellowgreen;
    padding: 0px 10px;
    height: 30px;
    line-height: 30px;
    text-align: center;
    float: left;
    margin-right: 6px;
    margin-bottom: 6px;
    .icon_clear {
      margin-left: 2px;
      font-size: 15px;
      color: #f56c6c;
      cursor: pointer;
    }
  }
}
.marg_play {
  display: flex;
  margin-bottom: 15px;
  .spans {
    text-align: right;
    width: 80px;
  }
}
/deep/.el-button {
  border-radius: 8px;
}
/deep/.el-dialog {
  border-radius: 8px;
  .el-dialog__header {
    padding: 0;
    .hearders {
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0px 18px 0px 20px;
      border-bottom: 1px solid #e2e2e2;
      .leftTitle {
        font-size: 14px;
        font-weight: bold;
        color: #2f4378;
      }
      .rightBoxs {
        display: flex;
        align-items: center;
        img {
          width: 14px;
          height: 14px;
          margin-left: 13px;
          cursor: pointer;
        }
      }
    }
  }
  .el-dialog__footer {
    padding: 0;
    .dialog-footer {
      padding: 0px 40px;
      height: 70px;
      border-top: 1px solid #e2e2e2;
      display: flex;
      align-items: center;
      justify-content: center;
    }
  }
}
.centerStys {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 18px;
}
.dis_fos {
  align-items: flex-end;
  display: flex;
  justify-content: space-between;
}
.margin_bs {
  margin-bottom: 6px;
}
</style>
