<template>
  <div id="examNum">
    <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
      @closed="loadingClose"
      :visible.sync="dialogVisible"
      width="560px"
      :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="150px"
          :model="listData"
          :rules="rules"
          ref="listData"
        >
          <el-form-item label="商品类型" prop="goodsType">
            <el-select
              v-model="listData.goodsType"
              placeholder="请选择"
              disabled
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="业务层级" prop="educationTypeId">
            <el-select
              v-model="listData.educationTypeId"
              placeholder="选择教育类型"
              @change="listData.businessId = ''"
              :disabled="statusPop === 0"
            >
              <el-option
                v-for="item in educationType"
                :key="item.id"
                :label="item.educationName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label=""
            prop="businessId"
            v-if="listData.educationTypeId"
          >
            <el-select
              v-model="listData.businessId"
              :disabled="statusPop === 0"
              placeholder="选择业务层次"
            >
              <el-option
                v-for="item in businessList"
                :key="item.id"
                :label="item.projectName + '-' + item.businessName"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="适用商品" prop="goodsId">
            <span v-if="!listData.businessId" style="color: #f56c6c"
              >请先选择业务层级</span
            >
            <el-checkbox-group
              v-model="listData.goodsId"
              v-else-if="listData.businessId && goodsList.length"
            >
              <el-checkbox
                v-for="(item, index) in goodsList"
                :key="index"
                :label="item.goodsId"
                :disabled="
                  item.goodsStatus === 0 &&
                  bfListData.goodsId.indexOf(item.goodsId) === -1
                "
                >{{ item.code + "-" + item.goodsName + "-" }}
                <span
                  :style="
                    item.goodsStatus === 0 &&
                    bfListData.goodsId.indexOf(item.goodsId) === -1
                      ? ''
                      : 'color:#F56C6C'
                  "
                  >￥{{ item.standPrice }}</span
                ></el-checkbox
              >
            </el-checkbox-group>
            <span style="color: #f56c6c" v-else>该业务层级下，无适用商品</span>
          </el-form-item>
          <el-form-item label="考试次数" prop="examNumber"
            ><el-input-number
              :controls="false"
              :disabled="statusPop === 2"
              v-model="listData.examNumber"
              :precision="0"
            ></el-input-number
          ></el-form-item>
          <el-form-item label="前培(刷题)次数" prop="doNumber"
            ><el-input-number
              :controls="false"
              :disabled="statusPop === 2"
              v-model="listData.doNumber"
              :precision="0"
            ></el-input-number
          ></el-form-item>
          <el-form-item label="状态" prop="">
            <el-radio-group v-model="listData.status">
              <el-radio
                v-for="(item, index) in statusOptions"
                :key="index"
                :label="item.value"
                :disabled="statusPop === 2"
                >{{ item.label }}</el-radio
              >
            </el-radio-group>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button
          type="primary"
          :loading="disabledBtn"
          v-if="statusPop !== 2"
          @click="submit('listData')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import searchBox from "@/components/searchBox";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
export default {
  name: "ExamNum",
  components: { searchBox, tableList, pagination },
  data() {
    return {
      disabledBtn: false,
      loading: false, //当前表单加载是否加载动画
      navText: {
        title: "配置考试次数前培次数",
        index: 0,
        ch: "条",
        num: true,
        choice: false,
        border: true,
        addHide: false,
        backFatherBtn: {
          status: false,
          title: "未定义",
        },
      },
      // 表单
      tableSet: [
        {
          label: "考次编码",
          prop: "code",
          hidden: true,
        },
        {
          label: "商品类型",
          prop: "goodsType",
          hidden: true,
          scope: "isOptions",
          options: [
            {
              label: "视频",
              value: 1,
            },
            {
              label: "题库",
              value: 2,
            },
            {
              label: "补考",
              value: 3,
            },
            {
              label: "前培",
              value: 4,
            },
          ],
        },
        {
          label: "教育类型",
          prop: "educationName",
          hidden: true,
        },
        {
          label: "业务层次",
          prop1: "projectName",
          prop2: "businessName",
          scope: "InfoMore",
          hidden: true,
        },
        {
          label: "适配商品",
          prop: "examNumberGoodsVos",
          prop1: "code",
          prop2: "goodsName",
          prop3: "standPrice",
          hidden: true,
          scope: "moreGoodsList",
        },
        {
          label: "考试次数",
          prop: "examNumber",
          hidden: true,
        },
        {
          label: "前培（刷题）次数",
          prop: "doNumber",
          hidden: true,
        },
        {
          label: "状态",
          prop: "status",
          hidden: true,
          scope: "status",
        },
      ],
      tableData: [], //表单数据
      total: 0, //一共多少条
      pageSize: 10, //每页多少条数据
      currentPage: 1, //当前页码
      //   弹窗数据
      listData: {
        examNumber: 0,
        doNumber: 0,
      },
      statusPop: -1,
      dialogVisible: false,
      options: [
        {
          label: "视频",
          value: 1,
        },
        {
          label: "题库",
          value: 2,
        },
        {
          label: "补考",
          value: 3,
        },
        {
          label: "前培",
          value: 4,
        },
      ],
      statusOptions: [
        {
          label: "启用",
          value: 1,
        },
        {
          label: "关闭",
          value: 0,
        },
      ],
      //表单验证
      rules: {
        goodsType: [
          { required: true, message: "请选择商品类型", trigger: "change" },
        ],
        educationTypeId: [
          { required: true, message: "请选择教育类型", trigger: "change" },
        ],
        businessId: [
          { required: true, message: "请选择业务层次", trigger: "change" },
        ],
        goodsId: [
          {
            type: "array",
            required: true,
            message: "请至少选择一个商品",
            trigger: "change",
          },
        ],
        examNumber: [
          {
            required: true,
            type: "number",
            min: 0,
            message: "请重新输入考试次数",
            trigger: "blur",
          },
        ],
        doNumber: [
          {
            required: true,
            type: "number",
            min: 0,
            message: "请重新输入前培（刷题）次数",
            trigger: "blur",
          },
        ],
        status: [{ required: true, message: "请选择状态", trigger: "change" }],
      },
      busList: [],
      businessList: [],
      goodsList: [],
      bfListData: {},
      examNumber: 0,
      doNumber: 0,
    };
  },
  computed: { ...mapGetters(["educationType"]) },
  mounted() {
    this.getDict();
    this.search();
  },
  activated() {
    this.getDict();
    this.search();
  },
  watch: {
    "listData.educationTypeId"(value) {
      this.businessList = this.busList.filter((item) => {
        return item.educationId == value;
      });
    },
    "listData.businessId"(value) {
      if (value) {
        if (this.statusPop === 1) {
          this.listData.goodsId = [];
        }
        this.$api
          .inquiresystemnumberlistGoods({ businessId: value, goodsType: 1 })
          .then((res) => {
            this.goodsList = res.rows;
          });
      }
    },
  },
  methods: {
    loadingClose() {
      this.disabledBtn = false;
    },
    getDict() {
      this.$api.inquirebusinessList({ status: 1 }).then((res) => {
        this.busList = res.rows;
      });
    },
    editInfo(v) {
      this.addClick(v, 0);
    },
    search(v) {
      this.loading = true;
      var data = {
        status: "0,1",
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      };
      this.$api
        .inquiresystemnumber(data)
        .then((res) => {
          this.tableData = res.rows;
          this.total = res.total;
          this.navText.index = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    init() {
      this.search();
    },
    del(v) {
      this.$alert(
        "确定删除此内容？<br />内容删除后将无法恢复，请慎重考虑",
        "提示",
        {
          dangerouslyUseHTMLString: true,
        }
      )
        .then(() => {
          var data = {
            examNumberId: v.examNumberId,
            status: -1,
          };
          this.$api.editsystemnumber(data).then((res) => {
            this.$message.success("删除成功");
            this.search();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addClick(v, int) {
      if (v === undefined) {
        this.goodsList = [];
        this.statusPop = 1;
        this.examNumber = 0;
        this.doNumber = 0;
        this.bfListData = {
          goodsId: [],
        };
        this.listData = {
          goodsType: 1,
          status: 1,
          businessId: "",
          goodsId: [],
          examNumber: 0,
          doNumber: 0,
        };
        this.rules.examNumber[0].min = 0;
        this.rules.doNumber[0].min = 0;
        this.rules.examNumber[0].message = "考试次数不得小于" + 0 + "次";
        this.rules.doNumber[0].message = "前培次数不得小于" + 0 + "次";
        this.$nextTick(() => {
          this.$refs.listData.clearValidate();
        });
        this.dialogVisible = true;
      } else {
        this.statusPop = int;
        this.$api.obtainsystemnumber(v.examNumberId).then((res) => {
          this.$api
            .inquiresystemnumberlistGoods({
              businessId: res.data.businessId,
              goodsType: 1,
            })
            .then((result) => {
              this.goodsList = result.rows;
            });
          this.bfListData = JSON.parse(JSON.stringify(res.data));
          this.rules.examNumber[0].min = this.bfListData.examNumber;
          this.rules.examNumber[0].message =
            "考试次数不得小于" + this.bfListData.examNumber + "次";
          this.rules.doNumber[0].min = this.bfListData.doNumber;
          this.rules.doNumber[0].message =
            "前培次数不得小于" + this.bfListData.doNumber + "次";
          this.examNumber = this.bfListData.examNumber;
          this.doNumber = this.bfListData.doNumber;
          this.listData = res.data;
          this.$nextTick(() => {
            this.$refs.listData.clearValidate();
          });
          this.dialogVisible = true;
        });
      }
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.rulesTableSumbit();
        } else {
          return false;
        }
      });
    },
    rulesTableSumbit() {
      this.disabledBtn = true;
      var data = JSON.parse(JSON.stringify(this.listData));
      this.busList.map((item) => {
        if (item.id === data.businessId) {
          data.projectId = item.projectId;
        }
      });
      if (this.statusPop === 1) {
        this.$api
          .appsystemnumber(data)
          .then((res) => {
            this.$message.success("新增成功");
            this.dialogVisible = false;
            this.search();
          })
          .catch(() => {
            this.disabledBtn = false;
          });
      }
      if (this.statusPop === 0) {
        this.$api
          .editsystemnumber(data)
          .then((res) => {
            this.$message.success("修改成功");
            this.dialogVisible = false;
            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-checkbox-group {
  max-height: 260px;
  overflow-y: auto;
  border: 1px solid #eee;
  padding: 10px;
}
/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>

