<template>
  <div id="schoolManagement">
    <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, 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
      :visible.sync="dialogBox"
      width="560px"
      :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="120px"
          :model="poppleData"
          :rules="rules"
          ref="poppleData"
        >
          <el-form-item label="学校名称" prop="schoolName">
            <el-input v-model="poppleData.schoolName"></el-input>
          </el-form-item>
          <el-form-item label="学校等级" prop="dictCode">
            <el-select
              v-model="poppleData.dictCode"
              placeholder="请选择学校等级"
            >
              <el-option
                v-for="item in optionsList"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="是否启用" prop="status">
            <el-radio-group v-model="poppleData.status">
              <el-radio :label="1">启用</el-radio>
              <el-radio :label="0">停用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="学校联系人">
            <el-input v-model="poppleData.contact"></el-input>
          </el-form-item>
          <el-form-item label="联系方式">
            <el-input v-model="poppleData.phone"></el-input>
          </el-form-item>
          <el-form-item label="优势专业">
            <el-input v-model="poppleData.advantageSpecialty"></el-input>
          </el-form-item>
          <el-form-item label="地点">
            <el-input v-model="poppleData.address"></el-input>
          </el-form-item>
          <el-form-item label="简介">
            <el-input
              type="textarea"
              :rows="2"
              placeholder="请输入简介"
              v-model="poppleData.introduction"
            ></el-input>
          </el-form-item>
        </el-form>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeBefore">关闭</el-button>
        <el-button @click="submitForm('poppleData')" type="primary"
          >确定</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: "未定义",
        },
      },
      //搜索
      formList: [
        {
          label: "学校名称",
          prop: "schoolName",
          placeholder: "请输入学校名称",
        },
        {
          label: "学校联系人",
          prop: "contact",
          placeholder: "请输入学校联系人",
        },
        {
          label: "联系方式",
          prop: "phone",
          placeholder: "请输入联系方式",
        },
        {
          label: "学校等级",
          prop: "dictCode",
          placeholder: "请选择学校等级",
          scope: "select",
          options: [],
        },
        {
          label: "学校状态",
          prop: "status",
          placeholder: "请选择学校状态",
          scope: "select",
          options: [
            {
              label: "启用",
              value: 1,
            },
            {
              label: "关闭",
              value: 0,
            },
          ],
        },
      ],
      // 表单
      tableSet: [
        {
          label: "学校名称",
          prop: "schoolName",
          hidden: true,
        },
        {
          label: "学校联系人",
          prop: "contact",
          hidden: true,
        },
        {
          label: "联系方式",
          prop: "phone",
          hidden: true,
        },
        {
          label: "学校等级",
          prop: "dictLabel",
          hidden: true,
        },
        {
          label: "优势专业",
          prop: "advantageSpecialty",
          hidden: true,
        },
        {
          label: "地址",
          prop: "address",
          hidden: true,
        },
        {
          label: "启用状态",
          prop: "status",
          hidden: true,
          scope: "status",
        },
        {
          label: "简介",
          prop: "introduction",
          hidden: true,
        },
      ],
      poppleData: {},
      tableData: [], //表单数据
      total: 0, //一共多少条+
      pageSize: 10, //每页多少条数据
      currentPage: 1, //当前页码
      fullscreen: false,
      dialogBox: false,
      optionsList: [],
      statusPop: 0,
      rules: {
        schoolName: [
          { required: true, message: "请输入学校名称", trigger: "blur" },
        ],
        dictCode: [
          { required: true, message: "请选择学校等级", trigger: "change" },
        ],
        status: [
          { required: true, message: "请选择是否启用", trigger: "change" },
        ],
      },
    };
  },
  mounted() {
    this.search();
    this.getSchoolType();
  },
  methods: {
    getSchoolType() {
      this.$api.obtaindictdata({ dictTypeId: 11 }).then((res) => {
        var listsT = [];
        res.rows.forEach((item, index) => {
          var ac = {
            value: item.dictCode,
            label: item.dictLabel,
          };
          listsT.push(ac);
        });
        this.formList.forEach((item, index) => {
          if (item.prop === "dictCode") {
            item.options = listsT;
            this.optionsList = listsT;
          }
        });
      });
    },
    submitForm(formName) {
      var self = this;
      this.$refs[formName].validate((valid) => {
        if (valid) {
          self.rulesTableSumbit();
        } else {
          return false;
        }
      });
    },
    search(v) {
      this.loading = true;
      if (v === undefined) {
        v = {
          pageSize: this.pageSize,
          pageNum: this.currentPage,
        };
      }
      var data = {
        schoolName: v.schoolName,
        contact: v.contact,
        phone: v.phone,
        dictCode: v.dictCode,
        status: v.status === undefined ? "0,1" : v.status,
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      };
      this.$api.inquireuserschoolList(data).then((res) => {
        this.tableData = res.data;
        this.total = res.total;
        this.navText.index = res.total;
      });
      this.loading = false;
    },
    init() {
      this.search();
    },
    rulesTableSumbit() {
      var data = {
        address: this.poppleData.address,
        advantageSpecialty: this.poppleData.advantageSpecialty,
        contact: this.poppleData.contact,
        dictCode: this.poppleData.dictCode,
        introduction: this.poppleData.introduction,
        phone: this.poppleData.phone,
        schoolCity: this.poppleData.schoolCity,
        schoolName: this.poppleData.schoolName,
        status: this.poppleData.status,
      };
      if(this.statusPop === 1){
        this.$api.adduserschool(data).then((res) => {
          if (res.code === 200) {
            this.$message.success("新增成功");
            this.search();
            this.dialogBox = false;
          }
        });
      }else{
        data.id = this.poppleData.id
        this.$api.edituserschool(data).then((res) => {
          if (res.code === 200) {
            this.$message.success("修改成功");
            this.search();
            this.dialogBox = false;
          }
        });
      }
    },
    del(options) {
      this.$api.edituserschool({ id: options.id, status: -1,schoolName:options.schoolName }).then((res) => {
        if (res.code === 200) {
          this.$message.success("删除成功");
          this.search();
        }
      });
    },
    closeBefore() {
      this.dialogBox = false;
      this.fullscreen = false;
      this.poppleData = {};
    },
    addClick(v, int) {
      if (v === undefined) {
        this.statusPop = 1;
        this.poppleData = {};
      } else {
        this.statusPop = int;
        this.$api.obtainuserschool(v.id).then((res) => {
          this.poppleData = res.data;
        });
      }
      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__body {
    padding: 0;
    .contentBox {
      padding: 20px 20px 5px;
      .el-col {
        padding: 0px 20px;
        margin-bottom: 30px;
        header {
          margin-bottom: 6px;
          color: #2f4378;
          font-size: 14px;
        }
      }
    }
  }
  .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>

