<template>
  <div id="examGuide">
    <search-box :formList="formList" @search="search" @init="init" />
    <table-list
      :tableSets="tableSet"
      :tableData="tableData"
      :navText="navText"
      @addClick="addClick"
      :loading="loading"
    >
      <template slot="btn" slot-scope="props">
        <el-button type="text" @click="addClick(props.scope.row, 2)"
          >详情</el-button
        >
        <el-button
          v-if="props.scope.row.status === 2 || props.scope.row.status === 3"
          type="text"
          @click="changeFBI(props.scope.row, 1)"
          >发布</el-button
        >
        <el-button
          v-if="props.scope.row.status === 1"
          type="text"
          @click="changeFBI(props.scope.row, 2)"
          >撤回</el-button
        >
        <el-button
          v-if="props.scope.row.status === 2 || props.scope.row.status === 3"
          type="text"
          @click="addClick(props.scope.row, 0)"
          >修改</el-button
        >
        <el-button
          v-if="props.scope.row.status === 3"
          type="text"
          @click="changeFBI(props.scope.row, 0)"
          >删除</el-button
        >
      </template>
    </table-list>
    <pagination
      :total="total"
      :pageSize="pageSize"
      :currentPage="currentPage"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <el-dialog
      :visible.sync="dialogBox"
      width="920px"
      :show-close="false"
      :destroy-on-close="true"
      :before-close="closeBefore"
      :fullscreen="fullscreen"
    >
      <div slot="title" class="hearders">
        <div class="leftTitle">
          {{ statusPop === 1 ? "添加" : statusPop === 0 ? "修改" : "详情" }}
        </div>
        <div class="rightBoxs">
          <img
            src="@/assets/images/Max@2x.png"
            alt=""
            @click="fullscreen = !fullscreen"
          />
          <img src="@/assets/images/Close@2x.png" alt="" @click="closeBefore" />
        </div>
      </div>
      <el-row class="contentBox" :span="24">
        <el-col :span="24">
          <el-col :span="12">
            <el-col :span="24">
              <header>考试类型</header>
              <el-cascader
                style="width: 100%"
                v-model="poppleData.examTypeId"
                :options="optionsTion"
                :size="'medium'"
                @change="changeOptions"
                :disabled="statusPop === 2"
                clearable
                :props="{
                  label: 'categoryName',
                  value: 'categoryId',
                  checkStrictly: true,
                  emitPath: false,
                }"
              ></el-cascader>
            </el-col>
            <el-col :span="24">
              <header>专业：</header>
              <el-input
                :disabled="statusPop === 2"
                placeholder="请输入专业："
                v-model="poppleData.major"
              />
            </el-col>
          </el-col>
          <el-col :span="12">
            <el-col :span="24">
              <header>指引类型：</header>
              <el-radio-group
                v-model="poppleData.typeNum"
                :disabled="statusPop === 2"
                style="display: flex"
              >
                <el-radio :label="0">报名指引</el-radio>
                <el-radio :label="1">打印准考证</el-radio>
                <el-radio :label="2">成绩查询指引</el-radio>
              </el-radio-group>
            </el-col>
          </el-col>
          <el-col :span="24">
            <header>考试指南说明：</header>
            <div
              style="
                max-height: 700px;
                overflow-y: auto;
                border: 1px solid #e2e2e2;
                border-radius: 8px;
                padding: 13px;
              "
              v-if="statusPop === 2"
              v-html="poppleData.typeText"
            ></div>
            <editor
              v-else
              v-model="poppleData.typeText"
              :min-height="300"
              :max-height="700"
              :uploadStatus="uploadStatus"
            />
          </el-col>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeBefore">取 消</el-button>
        <el-button
          type="primary"
          @click="rulesTableSumbit"
          v-if="statusPop !== 2"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import searchBox from "@/components/searchBox";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
import Editor from "@/components/Editor";
export default {
  components: { searchBox, tableList, pagination, Editor },
  data() {
    return {
      uploadStatus: 2, //富文本组件传值
      loading: false, //当前表单加载是否加载动画
      navText: {
        title: "考试指南",
        index: 0,
        ch: "条",
        num: true,
        choice: true,
        addHide: false,
        backFatherBtn: {
          status: false,
          title: "未定义",
        },
      },
      poppleData: {},
      statusPop: 0,
      fullscreen: false,
      dialogBox: false,
      tableSet: [
        {
          label: "考试类型",
          prop: "examType",
          hidden: true,
        },
        {
          label: "专业",
          prop: "major",
          hidden: true,
        },
        {
          label: "指引类型",
          prop: "typeNum",
          hidden: true,
          scope: "typeStatus",
        },
        {
          label: "指引内容",
          prop: "typeText",
          hidden: true,
        },
        {
          label: "发布状态",
          prop: "status",
          hidden: true,
          scope: "statusFB",
        },
        {
          label: "发布时间",
          prop: "issueTime",
          hidden: true,
          scope: "aTimeList",
        },
        {
          label: "备注",
          prop: "remarks",
          hidden: true,
        },
      ], //表头信息
      tableData: [], //表单数据
      total: 0, //一共多少条
      pageSize: 10, //每页多少条数据
      currentPage: 1, //当前页码
      formList: [
        {
          label: "考试类型",
          prop: "examTypeId",
          scope: "cascader",
          placeholder: "选择考试类型",
          options: [],
          props: {
            label: "categoryName",
            value: "categoryId",
          },
        },
      ], //搜索
      optionsTion: [],
    };
  },
  mounted() {
    this.search();
    this.initCascader();
  },
  methods: {
    changeFBI(optoin, v) {
      this.$api
        .editGuideexamarrangement({ id: optoin.id, status: v })
        .then((res) => {
          if (res.code === 200) {
            if (v === 0) {
              this.$message.success("删除成功");
            } else if (v === 1) {
              this.$message.success("发布成功");
            } else if (v === 2) {
              this.$message.success("撤回成功");
            }
            this.search();
          }
        });
    },
    changeOptions(v) {
      console.log(v);
    },
    closeBefore() {
      if (this.statusPop === 2) {
        this.dialogBox = false;
        this.fullscreen = false;
      } else {
        this.$confirm("此操作将关闭窗口, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.dialogBox = false;
            this.fullscreen = false;
            this.poppleData = {};
          })
          .catch(() => {});
      }
    },
    totalScores() {},
    search(v) {
      if (v === undefined) {
        v = {
          pageSize: this.pageSize,
          pageNum: this.currentPage,
        };
      }
      var data = {
        examTypeId: v.examTypeId,
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      };
      this.loading = true;
      this.$api.examarrangementlistGuide(data).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.navText.index = res.total;
      });
      this.loading = false;
    },
    init() {
      this.search();
    },
    del() {},
    addClick(v, int) {
      if (int === undefined) {
        this.statusPop = 1;
      } else {
        this.statusPop = int;
        this.poppleData = v;
      }
      this.dialogBox = true;
    },
    async rulesTableSumbit(v, int) {
      if (/^\\d+$/.test(this.poppleData.examTypeId)) {
        this.$message.error("请选择考试类型");
        return;
      }
      if (/^\\d+$/.test(this.poppleData.typeNum)) {
        this.$message.error("请选择指引类型");
        return;
      }
      if (!this.poppleData.typeText) {
        this.$message.error("请输入考试指南");
        return;
      }
      var nameCode = "";
      const awitY = await this.$api.inquireProfessionClassification({
        status: 1,
      });
      awitY.rows.forEach((results, indexs) => {
        if (results.categoryId === this.poppleData.examTypeId) {
          nameCode = results.categoryName;
        }
      });
      var data = {
        examType: nameCode,
        examTypeId: this.poppleData.examTypeId,
        major: this.poppleData.major,
        status: this.poppleData.status || 3,
        typeNum: this.poppleData.typeNum,
        typeText: this.poppleData.typeText,
      };
      if (this.statusPop === 1) {
        this.$api.addGuideexamarrangement(data).then((res) => {
          if (res.code === 200) {
            this.$message.success("新增成功");
            this.search();
            this.dialogBox = false;
          }
        });
      } else {
        data.id = this.poppleData.id;
        this.$api.editGuideexamarrangement(data).then((res) => {
          if (res.code === 200) {
            this.$message.success("修改成功");
            this.search();
            this.dialogBox = false;
          }
        });
      }
    },
    close() {
      this.$confirm("是否关闭该窗口", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {})
        .catch(() => {});
    },
    handleSizeChange(v) {
      this.pageSize = v;
      this.currentPage = 1;
      this.search();
    },
    handleCurrentChange(v) {
      this.currentPage = v;
      this.search();
    },
    //级联选择器数据
    initCascader() {
      this.$api
        .inquireProfessionClassification({ status: 1 })
        .then((res) => {
          this.arrayChangeType(res.rows);
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 接口数据转换级联类型
    arrayChangeType(options) {
      let result = [];
      if (!Array.isArray(options)) {
        return result;
      }
      options.forEach((item) => {
        delete item.children;
      });
      let map = {};
      options.forEach((item) => {
        map[item.categoryId] = item;
      });
      options.forEach((item) => {
        let parent = map[item.pid];
        if (parent) {
          (parent.children || (parent.children = [])).push(item);
        } else {
          result.push(item);
        }
      });
      result = result.sort(this.sortBy("sort", true));
      result.forEach((item, index) => {
        if (item.children) {
          item.children.sort(this.sortBy("sort", true));
        }
      });
      this.formList.forEach((item, index) => {
        if (item.prop === "examTypeId") {
          item.options = result;
          this.optionsTion = result;
        }
      });
      return result;
    },
    sortBy(attr, rev) {
      //第二个参数没有传递 默认升序排列
      if (rev == undefined) {
        rev = 1;
      } else {
        rev = rev ? 1 : -1;
      }

      return function (a, b) {
        a = a[attr];
        b = b[attr];
        if (a < b) {
          return rev * -1;
        }
        if (a > b) {
          return rev * 1;
        }
        return 0;
      };
    },
  },
};
</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__body {
    padding: 0;
    .contentBox {
      padding: 20px 20px 5px;
      .el-col {
        padding: 0px 20px;
        margin-bottom: 30px;
        header {
          margin-bottom: 6px;
          color: #2f4378;
          font-size: 14px;
        }
      }
      .juscon {
        float: none;
        width: 50%;
        margin: 0 auto;
      }
    }
  }
  .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;
  }
}
</style>
