<template>
  <div>
    <back-box>
      <template slot="title">
        <span>学校设置</span>
      </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">{{ sections }}</span>
          </li>
          <li class="school-item">
            <span class="s1">所属集团：</span>
            <span class="s2">{{ school.tenantName || "--" }}</span>
          </li>
          <li class="school-item">
            <span class="s1">基站登录密码 ：</span>
            <span class="s2">{{ school.stationPwd || "--" }}</span>
          </li>
        </ul>
        <div class="grade-box">
          <p class="h-title">
            年级管理
            <span v-if="!code" class="popconfirm-box" @click="diaUpgradeGrade = true">
              年级升级<i class="fa fa-level-up"></i>
            </span>
          </p>
          <ul class="grade-info">
            <li
              class="grade-item"
              v-for="item in tableData"
              :key="item.grade"
            >
              <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 :close-on-click-modal="false" 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.code"
              :key="item.code"
              :disabled="defaultSections.includes(item.code)"
              >{{ 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="stationPwd">
          <el-col :span="10">
            <el-input
              maxlength="6"
              v-model="formSchool.stationPwd"
              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>
    <el-dialog :close-on-click-modal="false" title="班级升级" :visible.sync="diaUpgradeGrade" width="400">
      <p>注意该操作会将所有年级升级，请谨慎操作！</p>
      <div class="dialog-footer" slot="footer">
        <el-button type="danger" @click="_UpgradeGrade()">确认升级</el-button>
        <el-button type="primary" @click="diaUpgradeGrade = false"
          >取 消</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { downloadFile } from "@/utils";
export default {
  data() {
    return {
      code: "", //长水跳转标志
      loading: false,
      url: "/api_html/school/manager/importClassAndStudent",
      diaSchool: false,
      school: {
        schoolName: "",
        managePwd: "",
        contactPerson: "",
        contactPhone: "",
        sections: "",
        tenantName: "",
      },
      tableData: [],
      formSchool: {
        //修改学校
        sections: "",
        managePwd: "",
        stationPwd: "",
        contactPerson: "",
        contactPhone: "",
      },
      rulesSchool: {
        sections: [{ required: true, message: "请选择学段", trigger: "blur" }],
        managePwd: [
          { required: true, message: "请填写管理密码", trigger: "blur" },
          {
            min: 8,
            max: 20,
            message: "长度在 8 到 20 个字符",
            trigger: "blur",
          },
        ],
        stationPwd: [
          { required: true, message: "请填写基站登陆密码", trigger: "blur" },
          {
            min: 6,
            max: 6,
            message: "长度为 6 个字符",
            trigger: "blur",
          },
        ],
      },
      subjectName: "",
      sectionsList: [],
      diaUpgradeGrade: false, //班级升级
      defaultSections: [],
    };
  },
  computed: {
    sections: function () {
      let arr = this.formSchool.sections || [];
      let sections = [];
      this.sectionsList.map((item) => {
        if (arr.includes(item.code)) {
          sections.push(item.name);
        }
      });
      return sections.join(",");
    },
  },
  created() {
    this.code = localStorage.getItem("csCode") || "";
    this._QueryDataSchool();
    this._QueryDataGrade();
    this._QuerySectionList();
  },
  methods: {
   
    editSchool() {
      //保存修改学校信息
      if (!this.formSchool.sections.length) {
        this.$message.error("请选择学段！");
        return;
      }
      if (!this.formSchool.managePwd) {
        this.$message.error("请填写密码！");
        return;
      }
      if (!this.formSchool.stationPwd) {
        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();
            this._QueryDataGrade();
          } else {
            this.$message.error(info);
          }
        } else {
          this.$message.error("数据有误，请检查！");
        }
      });
    },
    
    async _UpgradeGrade() {
      this.loading = true;
      const { data, status, info } = await this.$request.upgradeGrade();
      this.loading = false;
      if (status === 0) {
        this.$message.success("升级成功~");
        this.diaUpgradeGrade = false;
        this._QueryDataGrade();
      } else {
        this.$message.error(info);
      }
    },

    async _QuerySectionList() {
      //学校学段
      const { data, status, info } = await this.$request.sectionList();
      console.log(status);
      if (status === 0) {
        this.sectionsList = data?.list || [];
      } else {
        this.$message.error(info);
      }
    },
    async _QueryDataSchool() {
      //学校详情
      this.loading = true;
      const { data, status, info } = await this.$request.schoolDetail();
      this.loading = false;
      if (status === 0) {
        this.school = { ...data };
        for (let key in this.formSchool) {
          if (key == "sections") {
            this.formSchool.sections = data[key].split(",").map((item) => {
              console.log(Number(item));
              return Number(item);
            });
          } else {
            this.formSchool[key] = data[key] || "";
          }
        }
        this.defaultSections = [...this.formSchool.sections];
      } 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);
      }
    },
  },
};
</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;
    .h-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      .popconfirm-box {
        cursor: pointer;
        font-size: 14px;
        color: #999;
        .fa-level-up {
          font-size: 16px;
          padding-left: 2px;
        }
        &:hover {
          color: #f30;
        }
      }
    }
    .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;
        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;
}
.el-icon-plus {
  cursor: pointer;
  &:hover {
    color: #667ffd;
  }
}
:deep(.el-checkbox-group) {
  display: flex;
  flex-wrap: wrap;
  .p1 {
    padding-right: 30px;
    position: relative;
    &:hover {
      .el-icon-delete {
        display: block;
      }
      .is-checked + .el-icon-delete {
        display: none;
      }
    }
  }
  .el-icon-delete {
    font-size: 14px;
    color: #999;
    position: absolute;
    right: 10px;
    top: 13px;
    cursor: pointer;
    display: none;
  }
}
</style>