<template>
  <div id="colleges">
    <table-list
      :tableSets="tableSet"
      :tableData="tableData"
      :navText="navText"
      @addClick="addClick"
      :loading="loading"
      @editInfo="editInfo"
    >
      <template slot="btn" slot-scope="props">
        <el-button type="text" @click="addClick(props.scope.row, 0)"
          >修改</el-button
        >
        <el-button type="text" @click="del(props.scope.row)">删除</el-button>
      </template>
    </table-list>
    <pagination
      :total="total"
      :pageSize="pageSize"
      :currentPage="currentPage"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <el-dialog
      :visible.sync="dialogVisible"
      width="610px"
      @closed="loadingClose"
      :show-close="false"
      :close-on-click-modal="false"
    >
      <div slot="title" class="hearders">
        <div class="leftTitle">
          {{ statusPop === 1 ? "添加" : statusPop === 0 ? "修改" : "详情" }}
        </div>
        <div class="rightBoxs">
          <img src="@/assets/images/Close@2x.png" alt="" @click="close" />
        </div>
      </div>
      <div>
        <el-form
          label-position="right"
          label-width="110px"
          :model="listData"
          :rules="rules"
          ref="listData"
        >
          <el-form-item
            v-for="(items, indexs) in listitem"
            :key="indexs"
            :label="items.label"
            :prop="items.prop"
          >
            <el-radio-group
              v-if="items.scope === 'status'"
              v-model="listData[items.prop]"
            >
              <el-radio
                v-for="(item, index) in items.options"
                :key="index"
                :label="item.value"
                :disabled="statusPop === 2"
                >{{ item.label }}</el-radio
              >
            </el-radio-group>
            <el-input
              :disabled="statusPop === 2"
              v-else-if="items.scope === 'textarea'"
              type="textarea"
              v-model="listData[items.prop]"
            ></el-input>
            <div v-else-if="items.scope === 'activeType'">
              <div :class="changeHeight ? 'ach' : 'clh'">
                <div
                  v-for="(item, index) in zhType"
                  :key="index"
                  class="listBoxStys"
                >
                  {{ item.educationName + " - " + item.projectName }}
                  <i
                    class="el-icon-error closeIcons"
                    @click="closeType(item.id)"
                  ></i>
                </div>
              </div>
              <el-button
                size="mini"
                v-if="zhType.length > 2"
                @click="changeType"
                >{{ changeHeight ? "展开" : "关闭" }}</el-button
              >
            </div>
            <div
              v-else-if="items.scope === 'certificate'"
              style="display: flex; align-items: center"
            >
              <el-select
                v-model="eduType"
                placeholder="请选择教育类型"
                @change="eduChange"
              >
                <el-option
                  v-for="item in itemOption1"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
              <el-popover
                placement="bottom"
                width="200"
                trigger="click"
                :disabled="eduType ? false : true"
              >
                <el-checkbox-group v-model="cauType" class="checkboxSty">
                  <el-checkbox
                    v-for="item in itemOption2"
                    :label="item.value"
                    :key="item.value"
                    >{{ item.label }}</el-checkbox
                  >
                </el-checkbox-group>
                <el-button slot="reference" style="margin-left: 12px" @click="getMessage"
                  >请选择项目类型</el-button
                >
              </el-popover>
              <span style="margin-left: 10px">注：可多选</span>
            </div>
            <el-select
              v-model="listData[items.prop]"
              filterable
              placeholder="请选择所在省份"
              v-else-if="items.prop === 'schoolProvinceId'"
              @change="changeJYList"
            >
              <el-option
                v-for="item in optionsSF"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
            <el-select
              v-model="listData[items.prop]"
              filterable
              placeholder="请选择地/市"
              v-else-if="items.prop === 'schoolCityId'"
              @change="changeXMList(listData)"
            >
              <el-option
                v-for="item in optionsDS"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
            <el-input
              :disabled="statusPop === 2"
              v-else
              v-model="listData[items.prop]"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button
          type="primary"
          v-if="statusPop !== 2"
          :loading="disabledBtn"
          @click="submit('listData')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import searchBox from "@/components/searchBox";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
export default {
  name:"Colleges",
  components: { searchBox, tableList, pagination },
  data() {
    return {
      changeHeight: true,
      loading: false, //当前表单加载是否加载动画
      navText: {
        title: "院校",
        index: 0,
        ch: "条",
        num: true,
        border:true,
        choice: true,
        addHide: false,
        backFatherBtn: {
          status: false,
          title: "未定义",
        },
      },
      // 表单
      tableSet: [
        {
          label: "院校编码",
          prop: "encoder",
          hidden: true,
        },
        {
          label: "院校名称",
          prop: "schoolName",
          hidden: true,
          scope: "editInfo",
        },
        {
          label: "适用项目类型",
          prop: "courseProjectTypes",
          hidden: true,
          scope: "mapTypes",
        },
        {
          label: "所在省份",
          prop: "schoolProvince",
          hidden: true,
        },
        {
          label: "所在地市",
          prop: "schoolCity",
          hidden: true,
        },
        {
          label: "状态",
          prop: "status",
          hidden: true,
          scope: "status",
        },
      ],
      tableData: [], //表单数据
      total: 0, //一共多少条
      pageSize: 10, //每页多少条数据
      currentPage: 1, //当前页码
      options1: [],
      options2: [],
      courseProjectType: [],
      itemOption1: [],
      itemOption2: [],
      eduType: "",
      cauType: [],
      zhType: [],
      optionsSF: [],
      optionsDS: [],
      // 弹窗字段
      listitem: [
        {
          label: "适用项目类型",
          scope: "certificate",
        },
        {
          label: "",
          scope: "activeType",
        },
        {
          label: "院校名称",
          prop: "schoolName",
        },
        {
          label: "所在省份",
          prop: "schoolProvinceId",
          scope: "select",
        },
        {
          label: "所在地市",
          prop: "schoolCityId",
          scope: "select",
        },
        {
          label: "状态",
          prop: "status",
          scope: "status",
          options: [
            {
              label: "启用",
              value: 1,
            },
            {
              label: "关闭",
              value: 0,
            },
          ],
        },
        {
          label: "备注",
          prop: "remark",
          scope: "textarea",
        },
      ],
      //   弹窗数据
      listData: {},
      statusPop: -1,
      dialogVisible: false,
      //表单验证
      rules: {
        schoolName: [
          { required: true, message: "请输入院校名称", trigger: "blur" },
        ],
        schoolProvinceId: [
          { required: true, message: "请选择所在省份", trigger: ["blur","change"] },
        ],
        schoolCityId: [
          { required: true, message: "请选择所在地市", trigger: ["blur","change"] },
        ],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
      disabledBtn: false,
    };
  },
  watch: {
    cauType: function () {
      this.changeTypes();
    },
  },
  mounted() {
    this.search();
    this.initOptions();
  },
  activated(){
    this.search();
    this.initOptions()
  },
  methods: {
    getMessage(){
      if(!this.eduType){
        this.$message.warning("请先选择教育类型")
      }
    },
    changeJYList() {
      if (this.listData.schoolCityId) {
        this.listData.schoolCityId = "";
      }
      this.getXMType(this.listData.schoolProvinceId);
    },
    changeXMList(option) {
        this.optionsDS.map((item, index) => {
          if (item.value === option.schoolCityId) {
            this.listData.schoolProvinceId = item.pid;
          }
        });
    },
    getEduType() {
      this.$api.inquireapplyAreas({ status: 1, areaType: 1 }).then((res) => {
        var arrays = [];
        res.rows.map((item, index) => {
          arrays.push({
            label: item.areaName,
            value: item.areaId,
          });
        });
        this.optionsSF = arrays;
      });
    },
    getXMType(id) {
      this.$api
        .inquireapplyCityList({
          status: 1,
          areaType: 2,
          parentId: id ? id : undefined,
        })
        .then((res) => {
          var arrays = [];
          res.rows.map((item, index) => {
            arrays.push({
              label: item.areaName,
              value: item.areaId,
              pid: item.parentId,
            });
          });
          this.optionsDS = arrays;
        });
    },
    changeType() {
      this.changeHeight = !this.changeHeight;
    },
    changeTypes() {
      var arrays = [];
      this.cauType.map((item) => {
        this.courseProjectType.map((items, indexs) => {
          if (items.id === item) {
            arrays.push(items);
          }
        });
      });
      this.zhType = arrays;
    },
    eduChange() {
      var array = [];
      this.options2.map((item) => {
        if (item.pid === this.eduType) {
          array.push(item);
        }
      });
      this.itemOption2 = array;
    },
    initOptions() {
      this.$api.inquireCourseEducationType({status:1}).then((res) => {
        var array = [];
        res.rows.map((item) => {
          array.push({ label: item.educationName, value: item.id });
        });
        this.options1 = array;
      });
      this.$api.inquireCourseProjectType({status:1}).then((res) => {
        var array = [];
        res.rows.map((item) => {
          array.push({
            label: item.projectName,
            value: item.id,
            pid: item.educationId,
          });
        });
        this.courseProjectType = res.rows;
        this.options2 = array;
      });
    },
    editInfo(v) {
      this.addClick(v, 0);
    },
    search(v) {
      this.loading = true;
      var data = {
        status: "0,1",
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      };
      this.$api.inquireUserSchool(data).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.navText.index = res.total;
      }).finally(()=>{
        this.loading = false;
      })
    },
    init() {
      this.search();
    },
    closeType(id) {
      this.cauType.splice(this.cauType.indexOf(id), 1);
    },
    del(v) {
      this.$alert(
        "确定删除此内容？<br />内容删除后将无法恢复，请慎重考虑",
        "提示",
        {
          dangerouslyUseHTMLString: true,
        }
      )
        .then(() => {
          var data = {
            id: v.id,
            status: -1,
          };
          this.$api.editUserSchool(data).then((res) => {
            this.$message.success("删除成功");
          this.$store.commit("SCHOOLLIST");
            this.search();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addClick(v, int) {
      this.getEduType();
      this.getXMType();
      if (v === undefined) {
        this.statusPop = 1;
        this.listData = {
          status: 1,
        };
        this.eduType = "";
        this.cauType = [];
        this.zhType = [];
      } else {
        this.statusPop = int;
        this.listData = JSON.parse(JSON.stringify(v));
        var array = [];
        this.eduType = "";
        this.listData.courseProjectTypes.map((item) => {
          array.push(item.id);
        });
        this.cauType = array;
      }
      this.itemOption1 = JSON.parse(JSON.stringify(this.options1));
      this.itemOption2 = JSON.parse(JSON.stringify(this.options2));
      this.$nextTick(() => {
        this.changeHeight = true;
        this.$refs.listData.clearValidate();
      });
      this.dialogVisible = true;
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.rulesTableSumbit();
        } else {
          return false;
        }
      });
    },
    loadingClose(){
      this.disabledBtn = false
    },
    rulesTableSumbit() {
       this.disabledBtn = true
      this.listData.schoolProjects = this.cauType;
      if (this.statusPop === 1) {
        this.$api.appUserSchool(this.listData).then((res) => {
          this.$message.success("新增成功");
          this.dialogVisible = false;
          this.$store.commit("SCHOOLLIST");
          this.search();
        }).catch(()=>{
          this.disabledBtn = false
        })
      }
      if (this.statusPop === 0) {
        this.$api.editUserSchool(this.listData).then((res) => {
          this.$message.success("修改成功");
          this.dialogVisible = false;
          this.$store.commit("SCHOOLLIST");
          this.search();
        }).catch(()=>{
          this.disabledBtn = false
        })
      }
    },
    close() {
      this.dialogVisible = false;
    },
    handleSizeChange(v) {
      this.pageSize = v;
      this.currentPage = 1;
      this.search();
    },
    handleCurrentChange(v) {
      this.currentPage = v;
      this.search();
    },
  },
};
</script>

<style lang="less" scoped>
/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: flex-end;
    }
  }
}
.imgBox {
  width: 100%;
  // height: 210px;
  border: 1px solid #e2e2e2;
  border-radius: 8px;
  padding: 8px 8px 3px;
  display: flex;
  flex-direction: column;
  align-items: center;
  .imgLabel {
    flex: 1;
    width: 100%;
    border: 1px dotted #e2e2e2;
    color: #999;
    font-size: 14px;
    cursor: pointer;
    border-radius: 8px;
    .msPhoto {
      display: flex;
      justify-content: center;
      align-items: center;
      max-width: 100%;
      max-height: 270px;
      img {
        max-width: 100%;
        max-height: 270px;
      }
    }
    .imgbbx {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;
      i {
        font-weight: bold;
        margin: 14px 0;
        font-size: 24px;
      }
    }
  }
  p {
    margin: 5px 0px;
  }
}
.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;
}
</style>

