<template>
  <div id="hierarchicalManagement">
    <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 type="text" @click="addClick(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="1040px"
      :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>
      <div style="padding: 30rpx">
        <el-form
          label-position="right"
          label-width="80px"
          :model="formLabelAlign"
        >
          <el-row>
            <el-col :span="24" style="display: flex; align-items: center">
              <el-form-item label="等级">
                <el-input-number
                  v-model="formLabelAlign.grade"
                  controls-position="right"
                  :min="1"
                  :max="10"
                  size="small"
                ></el-input-number>
              </el-form-item>
              <el-form-item label="等级名称">
                <el-input
                  v-model="formLabelAlign.gradeName"
                  size="small"
                ></el-input>
              </el-form-item>
              <el-form-item label="所需积分">
                <div style="display: flex; align-items: center">
                  <el-input-number
                    v-model="formLabelAlign.needIntegralStart"
                    controls-position="right"
                    :min="0"
                    size="small"
                  ></el-input-number>
                  ~
                  <el-input-number
                    v-model="formLabelAlign.needIntegralEnd"
                    controls-position="right"
                    :min="0"
                    size="small"
                  ></el-input-number></div></el-form-item
            ></el-col>
            <el-col
              :span="24"
              style="display: flex; align-items: center; flex-wrap: wrap"
              v-for="(item, index) in formLabelAlign.activityGradePrivilegesVo"
              :key="index"
            >
              <el-form-item label="等级特权">
                <el-select
                  v-model="item.priType"
                  placeholder="请选择"
                  size="small"
                >
                  <el-option
                    v-for="items in options"
                    :key="items.value"
                    :label="items.label"
                    :value="items.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="特权范围" v-if="item.priType !== 2">
                <el-select
                  v-model="item.courseVo"
                  placeholder="特权范围"
                  clearable
                  filterable
                  multiple
                  collapse-tags
                >
                  <el-option
                    v-for="itemz in optionsList"
                    :key="itemz.value"
                    :label="itemz.label"
                    :value="itemz.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                :label="item.priType === 0 ? '优惠金额' : '折扣'"
                v-if="item.priType !== 2 || item.priType !== 3"
              >
                <el-input v-model="item.couponPrice" size="small">
                  <template slot="append">￥</template></el-input
                ></el-form-item
              >
              <el-form-item
                label="有效期"
                v-if="item.priType !== 2 || item.priType !== 3"
              >
                <el-input v-model="item.endTimeSum" size="small"
                  ><template slot="append">月</template></el-input
                ></el-form-item
              >
              <el-form-item :label="item.priType === 3 ? '课时' : '数量'">
                <el-input-number
                  v-model="item.quantity"
                  controls-position="right"
                  :min="0"
                  size="small"
                ></el-input-number
              ></el-form-item>
              <el-form-item label="操作">
                <el-button @click="delList(index)">删除</el-button>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-button @click="addList">新增</el-button>
            </el-col>
            <el-col
              :span="24"
              style="display: flex; align-items: center; flex-wrap: wrap"
            >
              <el-form-item label="备注">
                <el-input
                  type="textarea"
                  :rows="2"
                  placeholder="请输入内容"
                  v-model="formLabelAlign.remarks"
                >
                </el-input
              ></el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <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: "未定义",
        },
      },
      // optionsList: [{
      //   label: '课程',
      //   value: 1,
      //   children:[]
      // },{
      //   label: '题库',
      //   value: 2,
      //   children:[]
      // }],
      optionsList: [],
      options: [
        {
          label: "优惠券",
          value: 0,
        },
        {
          label: "折扣券",
          value: 1,
        },
        {
          label: "解锁题库",
          value: 2,
        },
        {
          label: "解锁课程",
          value: 3,
        },
      ],
      // 表单
      tableSet: [
        {
          label: "等级",
          prop: "grade",
          hidden: true,
        },
        {
          label: "名称",
          prop: "gradeName",
          hidden: true,
        },
        {
          label: "所需积分",
          prop1: "needIntegralStart",
          prop2: "needIntegralEnd",
          hidden: true,
          scope: "moreLists",
        },
        {
          label: "用户数量",
          prop: "userNum",
          hidden: true,
        },
        {
          label: "备注",
          prop: "remarks",
          hidden: true,
        },
      ],
      formLabelAlign: {
        activityGradePrivilegesVo: [],
      },
      tableData: [], //表单数据
      total: 0, //一共多少条
      pageSize: 10, //每页多少条数据
      currentPage: 1, //当前页码
      statusPop: 0,
      fullscreen: false,
      dialogBox: false,
    };
  },
  mounted() {
    this.search();
    this.getCn();
  },
  methods: {
    addList() {
      this.formLabelAlign.activityGradePrivilegesVo.push({});
    },
    delList(v) {
      this.formLabelAlign.activityGradePrivilegesVo.splice(v, 1);
    },
    changeList(v) {
      console.log(v);
    },
    getCn() {
      var self = this;
      this.$api.inquireCourseList({ status: 1 }).then((res) => {
        var arrNull = [];
        res.rows.forEach((item, index) => {
          arrNull.push({ label: item.courseName, value: item.courseId });
        });
        this.optionsList = arrNull;
      });
    },
    search() {
      this.loading = true;
      var data = {
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      };
      this.$api.inquiresystemgradeList(data).then((res) => {
        this.tableData = res.rows;
        this.total = res.total;
        this.navText.index = res.total;
      });
      this.loading = false;
    },
    init() {
      this.search();
    },
    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.formLabelAlign = {};
          })
          .catch(() => {});
      }
    },
    rulesTableSumbit() {
      if (
        this.formLabelAlign.grade === "" ||
        this.formLabelAlign.grade == undefined ||
        this.formLabelAlign.grade === null
      ) {
        this.$message.error("请输入等级");
        return;
      }
      if (
        this.formLabelAlign.gradeName === "" ||
        this.formLabelAlign.gradeName == undefined ||
        this.formLabelAlign.gradeName === null
      ) {
        this.$message.error("请输入等级名称");
        return;
      }
      if (
        this.formLabelAlign.needIntegralStart === "" ||
        this.formLabelAlign.needIntegralStart == undefined ||
        this.formLabelAlign.needIntegralStart === null
      ) {
        this.$message.error("请输入所需积分最小值");
        return;
      }
      if (
        this.formLabelAlign.needIntegralEnd === "" ||
        this.formLabelAlign.needIntegralEnd == undefined ||
        this.formLabelAlign.needIntegralEnd === null
      ) {
        this.$message.error("请输入所需积分最大值");
        return;
      }
      var data = {
        grade: this.formLabelAlign.grade,
        gradeName: this.formLabelAlign.gradeName,
        needIntegralEnd: this.formLabelAlign.needIntegralEnd,
        needIntegralStart: this.formLabelAlign.needIntegralStart,
        remarks: this.formLabelAlign.remarks,
      };
      if(this.statusPop === 1){
        data.activityGradePrivilegeAddBos = this.formLabelAlign.activityGradePrivilegesVo
        this.$api.addsystemgrade(data).then(res => {
          if(res.code === 200){
            this.$message.success('新增成功')
            this.search()
            this.dialogBox = false
          }
        })
      }else{
        data.activityGradePrivilegeEditBos = this.formLabelAlign.activityGradePrivilegesVo
        data.id = this.formLabelAlign.id
        this.$api.editsystemgrade(data).then(res => {
          if(res.code === 200){
            this.$message.success('修改成功')
            this.search()
            this.dialogBox = false
          }
        })
      }
    },
    addClick(v, int) {
      if (int === undefined) {
        this.statusPop = 1;
        this.formLabelAlign = {
          activityGradePrivilegesVo: [],
        };
      } else {
        this.$api.obtainsystemgrade(v.id).then((res) => {
          if (res.code === 200) {
            this.formLabelAlign = res.data;
            this.formLabelAlign.activityGradePrivilegesVo.forEach(
              (item, index) => {
                item.priType = Number(item.priType);
              }
            );
          }
        });
        this.statusPop = int;
      }
      this.dialogBox = true;
    },
    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;
  }
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

