<template>
  <div>
    <back-box>
      <template slot="title">
        <span>学校设置</span>
      </template>
      <template slot="btns" v-if="!code">
        <el-tooltip effect="dark" content="导入学校名单" placement="bottom">
          <el-button
            type="primary"
            icon="el-icon-upload2"
            size="mini"
            plain
            circle
            @click="diaUp = true"
          ></el-button>
        </el-tooltip>
      </template>
    </back-box>
    <div class="page-content">
      <div class="content-box">
        <i class="el-icon-edit" @click="diaSchool = true"></i>
        <ul class="school-info">
          <li class="school-item">
            <span class="s1">学校名称：</span>
            <span class="s2">{{ school.schoolName || "--" }}</span>
          </li>
          <li class="school-item">
            <span class="s1">授课端管理密码：</span>
            <span class="s2">{{ school.managePwd || "--" }}</span>
          </li>
          <li class="school-item">
            <span class="s1">联系人：</span>
            <span class="s2">{{ school.contactPerson || "--" }}</span>
          </li>
          <li class="school-item">
            <span class="s1">手机号码：</span>
            <span class="s2">{{ school.contactPhone || "--" }}</span>
          </li>
          <li class="school-item">
            <span class="s1">学段：</span>
            <span class="s2">{{
              school.sections | getSections(school.sections)
            }}</span>
          </li>
          <li class="school-item">
            <span class="s1">所属集团：</span>
            <span class="s2">{{ school.tenantName || "--" }}</span>
          </li>
        </ul>
        <div class="grade-box">
          <p class="h-title">年级管理</p>
          <ul class="grade-info">
            <li
              class="grade-item"
              v-for="item in tableData"
              :key="item.grade"
              @click="setGrade(item)"
            >
              <p class="grade-name">{{ item.gradeName }}</p>
              <div class="grade-class">
                <p>
                  <i class="fa fa-building"></i>班级：{{
                    item.classList.length
                  }}个
                </p>
                <p>
                  <i class="fa fa-book"></i>科目：{{
                    item.subjectNames.length
                  }}个
                </p>
              </div>
            </li>
          </ul>
          <el-empty
            :image-size="100"
            v-if="!tableData.length && loading == false"
            description="没有更多数据"
          ></el-empty>
        </div>
      </div>
    </div>
    <el-dialog title="导入学校名单" :visible.sync="diaUp" width="400">
      <up-load id="downDevice" :url="url"  @upSuccess="upSuccess" fileName="学校名单">
        <p class="down-txt" slot="down">
          通过Excel名单导入学校名单，点击
          <el-link type="danger" @click="downExcel">模板下载</el-link> 。
        </p>
      </up-load>
      <div class="dialog-footer" slot="footer">
        <el-button @click="diaUp = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改年级信息" :visible.sync="diaGrade" width="400">
      <el-form
        class="form-box"
        :model="formGrade"
        :rules="rulesGrade"
        label-width="160px"
      >
        <el-form-item label="科目：" prop="subjectNames">
          <div class="subject-box" :class="showAll ? 'active' : ''">
            <span class="showAll" @click="showAll = !showAll">{{
              showAll ? "收起" : "更多..."
            }}</span>
            <el-checkbox-group v-model="formGrade.subjectNames">
              <el-checkbox
                v-for="item in subjectList"
                :label="item"
                :key="item"
                >{{ item }}</el-checkbox
              >
            </el-checkbox-group>
          </div>
          <el-col :span="8">
            <el-input
              placeholder="添加科目"
              v-model.trim="subjectName"
              maxlength="30"
            >
              <i
                slot="suffix"
                class="el-input__icon el-icon-plus"
                @click="addSubjectName"
              ></i>
            </el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="班级：">
          <span v-for="(item, index) in formGrade.classList" :key="item.id">{{
            `${item.className}${
              index == formGrade.classList.length - 1 ? "" : "、"
            }`
          }}</span>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="editGrade">确 定</el-button>
        <el-button @click="diaGrade = false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改学校信息" :visible.sync="diaSchool" width="400">
      <el-form
        ref="formSchool"
        class="form-box"
        :model="formSchool"
        :rules="rulesSchool"
        label-width="160px"
      >
        <el-form-item label="学段：" prop="sections">
          <el-checkbox-group v-model="formSchool.sections">
            <el-checkbox
              v-for="item in sectionsList"
              :label="item.id"
              :key="item.id"
              >{{ item.name }}</el-checkbox
            >
          </el-checkbox-group>
        </el-form-item>
        <el-form-item label="授课端管理密码：" prop="managePwd">
          <el-col :span="10">
            <el-input
              maxlength="20"
              v-model="formSchool.managePwd"
              show-password
              placeholder="请输入授课端管理密码"
            ></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="联系人：" prop="contactPerson"
          ><el-col :span="10"
            ><el-input
              maxlength="30"
              v-model="formSchool.contactPerson"
              placeholder="请输入联系人"
            ></el-input></el-col
        ></el-form-item>
        <el-form-item label="手机号码：" prop="contactPhone"
          ><el-col :span="10"
            ><el-input
              v-model="formSchool.contactPhone"
              type="number"
              oninput="if(value.length > 11) value = value.slice(0,11)"
              placeholder="请输入联系人手机号码"
            ></el-input></el-col
        ></el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="editSchool">确 定</el-button>
        <el-button @click="diaSchool = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { downloadFile } from "@/utils";
export default {
  filters: {
    getSections(val) {
      let arr = val.split(",");
      let sections = arr.map((item) => {
        let txt;
        switch (item) {
          case "0":
            txt = "未知";
            break;
          case "1":
            txt = "小学";
            break;
          case "2":
            txt = "初中";
            break;
          case "3":
            txt = "高中";
            break;
          case "4":
            txt = "大学";
            break;
          case "7":
            txt = "未知";
            break;
          default:
            txt = "未知";
        }
        return txt;
      });
      return sections.join(",");
    },
  },
  data() {
    return {
      code: "",
      loading: false,
      url: "xxx",
      diaUp: false,
      diaSchool: false,
      diaGrade: false,
      showAll: false, //修改年级科目显示
      school: {
        schoolName: "",
        managePwd: "",
        contactPerson: "",
        contactPhone: "",
        sections: "",
        tenantName: "",
      },
      tableData: [],
      formSchool: {
        sections: "",
        managePwd: "",
        contactPerson: "",
        contactPhone: "",
      },
      rulesSchool: {
        sections: [{ required: true, message: "请选择学段", trigger: "blur" }],
        managePwd: [
          { required: true, message: "请填写管理密码", trigger: "blur" },
          {
            min: 8,
            max: 20,
            message: "长度在 8 到 20 个字符",
            trigger: "blur",
          },
        ],
      },
      formGrade: {
        gradeName: "",
        subjectNames: [],
        classList: [],
      },
      rulesGrade: {
        subjectNames: [
          { required: true, message: "请选择科目", trigger: "blur" },
        ],
      },
      subjectName: "",
      sectionsList: [
        { id: "1", name: "小学" },
        { id: "2", name: "初中" },
        { id: "3", name: "高中" },
        { id: "4", name: "大学" },
        { id: "7", name: "高补" },
      ],
      subjectList: [],
    };
  },
  created() {
    this.code = localStorage.getItem("csCode") || "";
    this._QueryDataSchool();
    this._QueryDataGrade();
    this._QuerySubject();
  },
  methods: {
    upSuccess(){//导入成功
    this.diaUp = false
      this._QueryDataSchool();
      this._QueryDataGrade();
    },
    setGrade(obj) {
      this.formGrade.subjectNames = obj.subjectNames;
      this.formGrade.classList = obj.classList;
      this.formGrade.gradeName = obj.gradeName;
      this.showAll = false;
      this.diaGrade = true;
    },
    addSubjectName() {
      if (!this.subjectName) {
        this.$message.warning("请填写科目名称");
        return;
      }
      this.subjectList.push(this.subjectName);
      this.formGrade.subjectNames.push(this.subjectName);
      this.subjectName = "";
    },
    editSchool() {
      if (!this.formSchool.sections.length) {
        this.$message.error("请选择学段！");
        return;
      }
      if (!this.formSchool.managePwd) {
        this.$message.error("请填写密码！");
        return;
      }
      if (this.loading) {
        return;
      }
      this.$refs.formSchool.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let form = { ...this.formSchool };
          form.sections = this.formSchool.sections.join(",");
          const { data, status, info } = await this.$request.updateSchool({
            ...form,
          });
          this.loading = false;
          if (status === 0) {
            this.$message.success("修改成功~");
            this.diaSchool = false;
            this._QueryDataSchool();
          } else {
            this.$message.error(info);
          }
        } else {
          this.$message.error("数据有误，请检查！");
        }
      });
    },
    async editGrade() {
      if (!this.formGrade.subjectNames.length) {
        this.$message.error("请选择科目！");
        return;
      }
      if (this.loading) {
        return;
      }
      this.loading = true;
      const { data, status, info } = await this.$request.updateGrade({
        gradeName: this.formGrade.gradeName,
        subjectNames: this.formGrade.subjectNames,
      });
      this.loading = false;
      if (status === 0) {
        this.$message.success("修改成功~");
        this.diaGrade = false;
        this._QueryDataGrade();
      } else {
        this.$message.error(info);
      }
    },
    async _QueryDataSchool() {
      this.loading = true;
      const { data, status, info } = await this.$request.schoolDetail();
      this.loading = false;
      console.log(status);
      if (status === 0) {
        this.school = { ...data };
        for (let key in this.formSchool) {
          this.formSchool[key] = data[key] || "";
        }
        this.formSchool.sections = this.formSchool.sections.split(",");
      } else {
        this.$message.error(info);
      }
    },
    async _QueryDataGrade() {
      this.loading = true;
      const { data, status, info } = await this.$request.gradeList();
      this.loading = false;
      console.log(status);
      if (status === 0) {
        this.tableData = data.list || [];
      } else {
        this.$message.error(info);
      }
    },
    async _QuerySubject() {
      const { data, status, info } = await this.$request.subjectList();
      if (status === 0) {
        this.subjectList = [...data.subjectNames] || [];
      } else {
        this.$message.error(info);
      }
    },
    async downExcel() {
      let data = await this.$request.downDevice({
        id: this.id,
      });
      if (data && !data.code) {
        let blob = new Blob([data], {
          type: "application/vnd.ms-excel;charset=utf-8",
        });
        downloadFile(`设备信息.xlsx`, blob);
      } else {
        this.$message.error(data.message);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.page-content {
  padding: 20px;
  .content-box {
    background: #f8f8f8;
    border-radius: 16px;
    position: relative;
    .el-icon-edit {
      position: absolute;
      top: 12px;
      right: 12px;
      padding: 5px;
      font-size: 18px;
      cursor: pointer;
      &:hover {
        color: #36f;
      }
    }
  }
  .school-info {
    display: flex;
    flex-wrap: wrap;
    padding: 16px 0;
    border-bottom: 0.5px solid #f2f2f2;
    .school-item {
      width: 50%;
      line-height: 48px;
      padding-left: 100px;
      display: flex;
      box-sizing: border-box;
      .s1 {
        width: 160px;
        font-size: 15px;
        color: #888;
      }
      .s2 {
        flex: 1;
      }
    }
  }
  .grade-box {
    padding: 20px;
    .grade-info {
      display: flex;
      flex-wrap: wrap;
      padding: 20px;
      .grade-item {
        width: calc((100% - 100px) / 3);
        margin-right: 50px;
        margin-bottom: 40px;
        box-sizing: border-box;
        padding: 12px 16px;
        border-radius: 10px;
        box-shadow: 1px 1px 3px #888;
        cursor: pointer;
        transition: all 0.5s;
        &:hover {
          transform: translate(-2px, -5px);
          box-shadow: 5px 5px 10px #888;
        }
        &:nth-of-type(3n) {
          margin-right: 0;
        }
      }
      .grade-name {
        font-size: 16px;
        font-weight: bold;
        line-height: 18px;
        padding-bottom: 12px;
      }
      .grade-class {
        display: flex;
        justify-content: space-between;
        font-size: 15px;
        padding-right: 20px;
        .fa {
          font-size: 18px;
          margin-right: 5px;
          color: #a4a4a4;
        }
        .fa-book {
          font-size: 20px;
        }
      }
    }
  }
}
.form-box {
  margin: 0 20px;
  .subject-box {
    height: 90px;
    overflow: hidden;
    position: relative;
    &.active {
      height: auto;
      overflow: auto;
    }
    .showAll {
      position: absolute;
      bottom: 0;
      right: 10px;
      font-size: 12px;
      color: #7f7f7f;
      cursor: pointer;
      padding: 2px;
      &:hover {
        color: #667ffd;
      }
    }
  }
}
.el-icon-plus {
  cursor: pointer;
  &:hover {
    color: #667ffd;
  }
}
</style>