<template>
  <div id="examArrangement">
    <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 === 1 ||
            props.scope.row.status === 2 ||
            props.scope.row.status === 4
          "
          type="text"
          @click="changeStatus(props.scope.row, 3)"
          >发布</el-button
        >
        <el-button
          type="text"
          v-if="
            props.scope.row.status === 1 ||
            props.scope.row.status === 2 ||
            props.scope.row.status === 4
          "
          @click="addClick(props.scope.row, 0)"
          >修改</el-button
        >
        <el-button
          type="text"
          @click="changeStatus(props.scope.row, 0)"
          v-if="props.scope.row.status === 1 || props.scope.row.status === 4"
          >删除</el-button
        >
        <el-button
          type="text"
          @click="changeStatus(props.scope.row, 2)"
          v-if="props.scope.row.status === 3"
          >撤回</el-button
        >
      </template>
    </table-list>
    <pagination
      :total="total"
      :pageSize="pageSize"
      :currentPage="currentPage"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    />
    <el-dialog
      :visible.sync="dialogBox"
      width="580px"
      :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-form label-position="right" label-width="80px" :model="poppleData">
          <el-form-item label="考试类型">
            <el-cascader
             :disabled="statusPop === 2" 
              v-model="poppleData.examTypeId"
              :options="optionsTion"
              size="small"
              :props="{
                label: 'categoryName',
                value: 'categoryId',
                checkStrictly: true,
                emitPath: false,
              }"
            ></el-cascader>
          </el-form-item>
          <el-form-item label="考试期次">
            <el-input :disabled="statusPop === 2"  v-model="poppleData.examPeriod"></el-input>
          </el-form-item>
          <el-form-item label="报名时间">
            <el-date-picker
             :disabled="statusPop === 2" 
              v-model="poppleData.bmTime1"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              align="right"
              size="small"
              value-format="timestamp"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="考试时间">
            <div v-for="(items, indexs) in poppleData.arays" :key="indexs">
              <el-date-picker
               :disabled="statusPop === 2" 
                v-model="poppleData.arays[indexs]"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                align="right"
                size="small"
                value-format="timestamp"
              >
              </el-date-picker>
              <i
              v-if="statusPop !== 2" 
                style="color: red; font-size: 20px"
                class="el-icon-remove"
                @click="delListsTimes(indexs)"
              ></i>
            </div>
            <i
             v-if="statusPop !== 2" 
              style="color: rgb(45, 195, 32); font-size: 20px"
              class="el-icon-circle-plus"
              @click="addListsTimes"
            ></i>
          </el-form-item>
          <el-form-item label="考试地点">
            <el-input :disabled="statusPop === 2"  v-model="poppleData.examAdress"></el-input>
          </el-form-item>
          <el-form-item label="报名网址">
            <el-input :disabled="statusPop === 2"  v-model="poppleData.examUrl"></el-input>
          </el-form-item>
          <el-form-item label="备注">
            <el-input :disabled="statusPop === 2"  type="textarea" v-model="poppleData.remarks"></el-input>
          </el-form-item>
          <el-form-item label="发布方式">
            <el-radio-group v-model="poppleData.releaseNum">
              <el-radio :disabled="statusPop === 2" :label="0">手动发布</el-radio>
              <el-radio :disabled="statusPop === 2"  :label="1">定时发布</el-radio>
            </el-radio-group>
            <el-date-picker
            v-if="poppleData.releaseNum === 1"
             :disabled="statusPop === 2" 
              v-model="poppleData.releaseTime"
              type="datetime"
              placeholder="选择日期时间"
              size="small"
              value-format="timestamp"
            >
            </el-date-picker>
          </el-form-item>
        </el-form>
      </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";
export default {
  components: { searchBox, tableList, pagination },
  data() {
    return {
      loading: false, //当前表单加载是否加载动画
      navText: {
        title: "学员考试安排",
        index: 0,
        ch: "条",
        num: true,
        choice: true,
        addHide: false,
        backFatherBtn: {
          status: false,
          title: "未定义",
        },
      },
      tableSet: [
        {
          label: "考试类型",
          prop: "examType",
          hidden: true,
        },
        {
          label: "考试期次",
          prop: "examPeriod",
          hidden: true,
        },
        {
          label: "报名时间",
          prop1: "signStartTime",
          prop2: "signEndTime",
          hidden: true,
          scope: "TimeLists",
        },
        {
          label: "考试时间",
          prop: "examArrangementTimeVo",
          hidden: true,
          scope: "TimeVoBtn",
        },
        {
          label: "考试地点",
          prop: "examAdress",
          hidden: true,
        },
        {
          label: "报名网址",
          prop: "examUrl",
          hidden: true,
        },
        {
          label: "订阅人数",
          prop: "subscrPe",
          hidden: true,
        },
        {
          label: "报名人数",
          prop: "signUpPe",
          hidden: true,
        },
        {
          label: "发布时间",
          prop: "examTime",
          hidden: true,
          scope: "aTimeList",
        },
        {
          label: "发布方式",
          prop: "releaseNum",
          hidden: true,
          scope: "releaseType",
        },
        {
          label: "发布状态",
          prop: "status",
          hidden: true,
          scope: "releaseStatus",
        },
        {
          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",
          },
        },
        {
          label: "报名时间",
          prop: "TimeList1",
          scope: "datePicker",
        },
        {
          label: "考试时间",
          prop: "TimeList2",
          scope: "datePicker",
        },
        {
          label: "考试期次",
          prop: "examPeriod",
          placeholder: "请输入考试期次",
        },
      ], //搜索
      optionsTion: [],
      statusPop: 0,
      poppleData: {
        arays: [],
      },
      dialogBox: false,
      fullscreen: false,
    };
  },
  mounted() {
    this.search();
    this.initCascader();
  },
  methods: {
    addListsTimes() {
      this.poppleData.arays.push([]);
    },
    delListsTimes(v) {
      this.poppleData.arays.splice(v, 1);
    },
    search(v) {
      if (v === undefined) {
        v = {
          pageSize: this.pageSize,
          pageNum: this.currentPage,
        };
      }
      var data = {
        examTypeId: v.examTypeId,
        examPeriod: v.examPeriod,
        signStartTime: v.TimeList1 ? (v.TimeList1[0] / 1000).toFixed(0) : "",
        signEndTime: v.TimeList1 ? (v.TimeList1[1] / 1000).toFixed(0) : "",
        startTime: v.TimeList2 ? (v.TimeList2[0] / 1000).toFixed(0) : "",
        endTime: v.TimeList2 ? (v.TimeList2[1] / 1000).toFixed(0) : "",
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      };
      this.loading = true;
      this.$api.examarrangementlist(data).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.navText.index = res.total;
      });
      this.loading = false;
    },
    init() {
      this.search();
    },
    addClick(v, int) {
      if (int === undefined) {
        this.statusPop = 1;
        this.poppleData = {
          arays: [],
        };
      } else {
        this.$api.examarrangementId(v.id).then((res) => {
          var arays = [];
          res.data.bmTime1 = [
            res.data.signStartTime * 1000,
            res.data.signEndTime * 1000,
          ];
          res.data.releaseTime = Number(res.data.releaseTime) * 1000
          res.data.examArrangementTimeVo.forEach((item, index) => {
            arays.push([item.startTime * 1000, item.endTime * 1000]);
          });
          res.data.arays = arays;
          this.poppleData = res.data;
        });
        this.statusPop = int;
      }
      this.dialogBox = true;
    },
    rulesTableSumbit() {
      if (!this.poppleData.examTypeId) {
        this.$message.error("请选择考试类型");
        return;
      }
      if (!this.poppleData.examPeriod) {
        this.$message.error("请输入考试期次");
        return;
      }
      if (
        this.poppleData.bmTime1 === "" ||
        this.poppleData.bmTime1 == undefined ||
        this.poppleData.bmTime1 === null
      ) {
        this.$message.error("请选择报名时间");
        return;
      }
      if (this.poppleData.arays.length === 0) {
        this.$message.error("请选择考试时间");
        return;
      }
      if (!this.poppleData.examAdress) {
        this.$message.error("请输入考试地点");
        return;
      }
      if (!this.poppleData.examUrl) {
        this.$message.error("请输入报名网址");
        return;
      }
      if (
        this.poppleData.releaseNum === "" ||
        this.poppleData.releaseNum == undefined ||
        this.poppleData.releaseNum === null
      ) {
        this.$message.error("请选择发布方式");
        return;
      }
      if(this.poppleData.releaseNum === 1){
        if(!this.poppleData.releaseTime){
          this.$message.error("请选择定时发布时间");
        return;
        }
      }
      var objs = [];
      var typeNames = "";
      this.poppleData.arays.forEach((item, index) => {
        objs.push({
          endTime: (item[0] / 1000).toFixed(0),
          startTime: (item[1] / 1000).toFixed(0),
        });
      });
      this.optionsTion.forEach((item, index) => {
        if (item.categoryId === this.poppleData.examTypeId) {
          typeNames = item.categoryName;
        }
      });
      var data = {
        releaseNum: this.poppleData.releaseNum,
        remarks: this.poppleData.remarks,
        examUrl: this.poppleData.examUrl,
        examAdress: this.poppleData.examAdress,
        examPeriod: this.poppleData.examPeriod,
        examType: typeNames,
        examTypeId: this.poppleData.examTypeId,
        signStartTime: (this.poppleData.bmTime1[0] / 1000).toFixed(0),
        signEndTime: (this.poppleData.bmTime1[1] / 1000).toFixed(0),
        status: this.poppleData.status || 4,
      };
      if(this.poppleData.releaseNum === 1){
        data.releaseTime = (Number(this.poppleData.releaseTime) / 1000).toFixed(0)
      }
      if (this.statusPop === 1) {
        data.examArrangementTimeAddBo = objs;
        this.$api.addExamexamarrangement(data).then((res) => {
          if (res.code === 200) {
            this.$message.success("添加成功");
            this.search();
            this.dialogBox = false;
          }
        });
      } else {
        data.id = this.poppleData.id;
        data.examArrangementTimeEditBoList = objs;
        this.$api.editExamexamarrangement(data).then((res) => {
          if (res.code === 200) {
            this.$message.success("修改成功");
            this.search();
            this.dialogBox = false;
          }
        });
      }
    },
    changeStatus(v, int) {
      this.$api
        .editExamexamarrangement({ id: v.id, status: int })
        .then((res) => {
          if (res.code === 200) {
            if (int === 3) {
              this.$message.success("发布成功");
            } else if (int === 0) {
              this.$message.success("删除成功");
            } else if (int === 2) {
              this.$message.success("撤回成功");
            }
            this.search();
          }
        });
    },
    handleSizeChange(v) {
      this.pageSize = v;
      this.currentPage = 1;
      this.search();
    },
    handleCurrentChange(v) {
      this.currentPage = v;
      this.search();
    },
    //级联选择器数据
    initCascader() {
      this.$api
        .inquireProfessionClassification()
        .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;
    },
    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(() => {});
      }
    },
    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__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>
