<template>
  <div class="page-container student-add-page">
    <div class="page-top-title-box">
      <p class="page-top-title-name-p">{{titleName}}</p>
      <p class="page-top-title-out-p" @click="outPageButton">返回</p>
      <p class="page-top-title-submit-p" v-if="!editType" @click="upDataClick">提交</p>
    </div>
    <div class="content-box scrollbar-box">
      <el-form :model="form" class="top-info-box" ref="form" :inline="true" :rules="rules" label-width="180px">
        <div class="left-info-box">
          <img :src="form.avatar" v-if="form.avatar">
          <img src="@/assets/ZDimages/basicsModules/tx_cion_one.png" v-if="!form.avatar">
          <el-upload
            v-if="!editType"
            class="certificate-avatar-uploader"
            :action="uploadImgUrl"
            :show-file-list="false"
            accept="image/jpeg,image/jpg,image/gif,image/png"
            :on-success="handleAvatarSuccess"
            :headers="headers"
            :before-upload="beforeAvatarUpload">
            <p>上传证件照</p>
          </el-upload>
        </div>
        <div class="right-info-box">
          <el-form-item label="姓名：" prop="userName">
            <el-input
              :disabled="editType"
              style="width:218px;"
              maxlength="50"
              v-model="form.userName"
              placeholder="请输入姓名"
              clearable
              size="small"
            />
          </el-form-item>
          <el-form-item label="性别：" prop="sex">
            <el-radio-group v-model="form.sex" v-if="!editType">
              <el-radio :label="1" style="margin-left:20px;width:70px;">男</el-radio>
              <el-radio :label="2">女</el-radio>
            </el-radio-group>
            <p v-if="editType" style="line-height:40px;color:#999;width:250px;padding-left:40px;">{{form.sex==1?'男':(form.sex==2?'女':'未知')}}</p>
          </el-form-item>
          <el-form-item label="学籍状态：" prop="userState">
            <el-radio-group v-model="form.userState" v-if="!editType">
              <el-radio :label="true" style="margin-left:20px;width:70px;">在读</el-radio>
              <el-radio :label="false">结业</el-radio>
            </el-radio-group>
            <p v-if="editType" style="line-height:40px;color:#999;width:250px;padding-left:40px;">{{form.userState?'在读':'结业'}}</p>
          </el-form-item>
          <el-form-item label="学号：" prop="account">
            <el-input
              style="width:218px;"
              :disabled="editType"
              maxlength="50"
              v-model="form.account"
              placeholder="请输入学号"
              clearable
              size="small"
            />
          </el-form-item>
          <el-form-item label="身份：" prop="position">
            <el-select
              @change="positionSelectClick"
              :disabled="editType"
              v-model="form.position"
              filterable
              placeholder="请选择身份">
              <el-option
                v-for="item in positionOptions"
                :key="item.postId"
                :label="item.postName"
                :value="item.postId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="手机号码：" prop="mobile">
            <el-input
              :disabled="editType"
              style="width:218px;"
              maxlength="11"
              onkeyup="this.value=this.value.replace(/[^\d.]/g,'')"
              v-model="form.mobile"
              placeholder="请输入手机号码"
              clearable
              size="small"
            />
          </el-form-item>
          <el-form-item label="所在学院：" prop="deptId">
            <el-select v-model="form.deptId" clearable
                       @change="systemUserMajorSelect(1)"
                       :disabled="editType" placeholder="请选择所在学院">
              <el-option
                v-for="item in deptOptions"
                :key="item.deptId"
                :label="item.deptName"
                :value="item.deptId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="专业：" prop="major">
            <el-select
              :disabled="editType"
              v-model="form.major"
              filterable
              @change="systemUserMajorSelect(2)"
              placeholder="请选择专业">
              <el-option
                v-for="item in majorOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="班级：" prop="grade">
            <el-select
              :disabled="editType"
              v-model="form.grade"
              filterable
              placeholder="请选择班级">
              <el-option
                v-for="item in gradeOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="出生日期：" prop="birthday">
            <el-date-picker
              :disabled="editType"
              v-model="form.birthday"
              type="date"
              placeholder="请选择出生日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="邮箱：" prop="email">
            <el-input
              :disabled="editType"
              style="width:218px;"
              maxlength="40"
              v-model="form.email"
              placeholder="请输入邮箱"
              clearable
              size="small"
            />
          </el-form-item>
          <el-form-item label="导师：" prop="tutorUserId">
            <el-select
              :disabled="editType"
              v-model="form.tutorUserId"
              filterable
              remote
              clearable
              reserve-keyword
              @change="tutorUserSelectClick"
              @clear="tutorUserClearClick"
              placeholder="请选择"
              :remote-method="tutorUserSelect">
              <el-option
                v-for="item in tutorUserOptions"
                :key="item.userId"
                :label="item.userName"
                :value="item.userId">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="学生类别：" prop="education">
            <el-select
              :disabled="editType"
              v-model="form.education"
              filterable
              placeholder="请选择学生类别">
              <el-option
                v-for="item in educationOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="校园卡号：" prop="cardNum">
            <el-input
              show-password
              :disabled="editType"
              style="width:218px;"
              onkeyup="this.value=this.value.replace(/^\s*|\s*$/g,'')"
              maxlength="30"
              v-model="form.cardNum"
              placeholder="请输入校园卡号"
              size="small"
            />
          </el-form-item>
          <el-form-item label="来源：" prop="source" v-if="propsData.userId">
            <p style="line-height:40px;color:#999;width:250px;padding-left:40px;">{{form.source==1?'统一认证平台':'智慧化管控系统'}}</p>
          </el-form-item>
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getToken } from "@/utils/auth";
import { getDeptList,systemUserAdd,systemUserUpdate,getDeptDropList,
  systemUserSelect,systemUserMajorSelect,systemPostSelect } from "@/api/commonality/permission";
import { getDicts } from "@/api/commonality/noPermission";
export default {
  name: "addPage",
  props:{
    editType:{},
    propsData:{},
    titleName:{},
  },
  data() {
    return {
      uploadImgUrl: this.uploadUrl(), // 上传的图片服务器地址
      headers: {
        Authorization: getToken(),
      },
      menuList:[],
      form:{
        avatar:"",
        userName:"",
        sex:1,
        userState:true,
        account:"",
        deptId:"",
        position:"",     //身份
        major:"",
        grade:"",
        birthday:"",
        email:"",
        mobile:"",
        tutorUserId:"",
        education:"",
        cardNum:"",
      },
      rules:{
        gradeName: [{ required: true, message: "请输入班级名称", trigger: "blur" },
          { required: true, message: "请输入班级名称", validator: this.spaceJudgment, trigger: "blur" }],
        userName: [{ required: true, message: "请输入姓名", trigger: "blur" },
          { required: true, message: "请输入姓名", validator: this.spaceJudgment, trigger: "blur" }],
        account: [{ required: true, message: "请输入学号", trigger: "blur" },
          { required: true, message: "请输入学号", validator: this.spaceJudgment, trigger: "blur" }],
        deptId: [{ required: true, message: "请选择所在学院", trigger: "blur" }],
        userState: [{ required: true, message: "请选择在读状态", trigger: "blur" }],
        position: [{ required: true, message: "请选择身份", trigger: "blur" }],
        major: [{ required: true, message: "请选择专业", trigger: "blur" }],
        grade: [{ required: true, message: "请选择班级", trigger: "blur" }],
        tutorUserId: [{ required: true, message: "请选择导师", trigger: "blur" }],
        mobile: [
          { required: true, message: "请输入手机号码", trigger: "blur" },
          { required: true, message: "请输入手机号码", validator: this.spaceJudgment, trigger: "blur" }
        ],
      },
      textType:false,
      //学院数据列表
      deptOptions:[],
      //身份列表
      positionOptions:[],
      //导师查询数据
      tutorUserOptions:[],
      //专业查询数据
      majorOptions:[],
      //班级查询数据
      gradeOptions:[],
      //学生类别查询数据
      educationOptions:[],
      //学籍状态查询数据
      workStatusOptions:[{dictLabel:'在读',dictValue:true},{dictLabel:'结业',dictValue:false}],
      //角色列表
      roleOptionselectOptions:[],
    }
  },
  created(){
    let self = this;
    if(this.propsData.userId){
      if(this.propsData.avatar){
        this.form.avatar = this.propsData.avatar;
      }else{
        this.form.avatar = '';
      }
      if(this.propsData.userName){
        this.form.userName = this.propsData.userName;
      }else{
        this.form.userName = '';
      }
      if(this.propsData.sex){
        this.form.sex = parseInt(this.propsData.sex);
      }else{
        this.form.sex = 1;
      }
      if(this.propsData.userState){
        this.form.userState = this.propsData.userState;
      }else{
        this.form.userState = true;
      }
      if(this.propsData.account){
        this.form.account = this.propsData.account;
      }else{
        this.form.account = '';
      }
      if(this.propsData.position){
        // this.form.position = parseInt(this.propsData.position);
        this.form.position = this.propsData.position;
      }else{
        this.form.position = '';
      }
      if(this.propsData.mobile){
        if(this.editType){
          this.form.mobile = this.propsData.mobile.substr(0, 3) + '****' + this.propsData.mobile.substr(7, 11);
        }else{
          this.form.mobile = this.propsData.mobile;
        }
      }else{
        this.form.mobile = '';
      }
      if(this.propsData.deptId){
        this.form.deptId = this.propsData.deptId;
      }else{
        this.form.deptId = '';
      }
      if(this.propsData.major){
        this.form.major = this.propsData.major;
      }else{
        this.form.major = '';
      }
      if(this.propsData.grade){
        this.form.grade = this.propsData.grade;
      }else{
        this.form.grade = '';
      }
      if(this.propsData.birthday){
        this.form.birthday = this.propsData.birthday;
      }else{
        this.form.birthday = '';
      }
      if(this.propsData.email){
        this.form.email = this.propsData.email;
      }else{
        this.form.email = '';
      }
      if(this.propsData.tutorUserId){
        this.form.tutorUserId = this.propsData.tutorUserId;
      }else{
        this.form.tutorUserId = '';
      }
      if(this.propsData.education){
        this.form.education = this.propsData.education;
      }else{
        this.form.education = '';
      }
      if(this.propsData.cardNumSimple){
        this.form.cardNum = this.propsData.cardNumSimple;
      }else{
        this.form.cardNum = '';
      }
    }
  },
  mounted(){
    this.getAll();
  },
  methods:{
    upDataClick(){
      let self = this;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if(this.form.email){
            let re = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;
            if(!re.test(this.form.email)){
              this.msgError('请输入正确的邮箱地址')
              return
            }
          }
          let obj = JSON.parse(JSON.stringify(this.form));
          // obj.position = obj.position+'';
          if(this.propsData.userId){
            //编辑
            obj.userId = this.propsData.userId
            systemUserUpdate(obj).then(response => {
              if(response.code==200){
                this.msgSuccess(response.message)
                this.outPageButton()
              }else if(response.code==205){
                this.$confirm(response.message, "警告", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning"
                }).then(() => {
                  //  确定
                  self.$router.push({ path: '/comprehensive/integratedManagement/accessAuthorization' });
                }).catch(function() {
                  //  取消
                  self.outPageButton();
                });
              }

            });
          }else{
            //新增
            obj.userType = 2
            systemUserAdd(obj).then(response => {
              if(response.code==200){
                this.msgSuccess(response.message)
                this.outPageButton()
              }else if(response.code==205){
                this.$confirm(response.message, "警告", {
                  confirmButtonText: "确定",
                  cancelButtonText: "取消",
                  type: "warning"
                }).then(() => {
                  //  确定
                  self.$router.push({ path: '/comprehensive/integratedManagement/accessAuthorization' });
                }).catch(function() {
                  //  取消
                  self.outPageButton();
                });
              }

            });
          }
        }
      });
    },
    //查询所有搜索接口数据
    getAll(){
      this.systemPostSelect();
      //获取学院
      getDeptDropList({level:2,deptType:1}).then(response => {
        this.$set(this,'deptOptions',response.data);
      });
      //学生类别查询
      getDicts('education').then(response => {
        this.$set(this,'educationOptions',response.data);
      });
      //获取导师
      if(this.propsData.tutorUserName){
        systemUserSelect({ userType:1, userName:this.propsData.tutorUserName?this.propsData.tutorUserName:'' }).then(response => {
          this.$set(this,'tutorUserOptions',response.data);
        });
      }
      if(this.form.deptId){
        systemUserMajorSelect({type:1,referId:this.form.deptId}).then(response => {
          this.$set(this,'majorOptions',response.data);
        });
      }
      if(this.form.major){
        systemUserMajorSelect({type:2,referId:this.form.major}).then(response => {
          this.$set(this,'gradeOptions',response.data);
        });
      }
    },
    //获取专业/班级
    systemUserMajorSelect(type){
      let obj = {};
      if(type == 1){
        //专业获取
        obj.type = 1;
        obj.referId = this.form.deptId;
      }else if(type == 2){
        //班级获取
        obj.type = 2;
        obj.referId = this.form.major;
      }
      systemUserMajorSelect(obj).then(response => {
        if(type == 1){
          this.$set(this.form,'major','');
          this.$set(this,'majorOptions',response.data);
          this.$set(this.form,'grade','');
        }else if(type == 2){
          this.$set(this.form,'grade','');
          this.$set(this,'gradeOptions',response.data);
        }
      });
    },
    systemPostSelect(){
      systemPostSelect({}).then(response => {
        let list = [];
        for(let i=0;i<response.data.length;i++){
          if(response.data[i].roleIds && response.data[i].postCode != 'school_level_admin' && response.data[i].postCode != 'college_admin'){
            list.push(response.data[i]);
          }
        }
        this.$set(this,'positionOptions',list)
      });
    },
    positionSelectClick(data){
      //选中
      let self = this;
      if(this.form.position){
        let num = 0;
        for(let i=0;i<self.positionOptions.length;i++){
          if(self.form.position == self.positionOptions[i].postId && self.positionOptions[i].roleIds){
            let list = self.positionOptions[i].roleIds
            self.$set(self.form,'roleIds',list);
            self.$set(self.form,'positionName',self.positionOptions[i].postName);
            num++
          }
        }
        if(num == 0){
          self.$set(self.form,'roleIds',[]);
        }
      }
      this.$forceUpdate();
    },
    //*************************导师查询
    tutorUserSelect(query){
      if (query !== '' && query.length > 0) {
        let obj = {
          userType:1,
          userName:query,
        }
        systemUserSelect(obj).then(response => {
          this.tutorUserOptions = response.data;
        });
      } else {
        this.tutorUserOptions = [];
      }
    },
    //导师选中触发
    tutorUserSelectClick(data){
    },
    //导师清空触发
    tutorUserClearClick(){
      this.tutorUserOptions = [];
    },
    //上传
    handleAvatarSuccess(res, file) {
      this.form.avatar = res.data.url;
      this.$forceUpdate()
    },
    beforeAvatarUpload(file) {
      let type = false;
      if (file.type == 'image/png' || file.type == 'image/jpeg' || file.type == 'image/jpg' || file.type == 'image/gif') {
        type = true;
      }else{
        this.msgError('只能上传png/jpeg/jpg/gif格式图片');
        type = false;
      }
      return type;
    },
    //返回事件
    outPageButton(){
      this.$parent.outPage(1);
    },
  }
}
</script>

<style scoped lang="scss">
.student-add-page{
  .top-info-box{
    flex:1;
    display: flex;
    padding:40px 20px 0;
    .left-info-box{
      margin-right:20px;
      img{
        width:100px;
        height:120px;
      }
      p{
        width: 80px;
        height: 26px;
        border: 1px solid #0183FA;
        border-radius: 6px;
        line-height:24px;
        font-size:12px;
        color:#0183FA;
        text-align: center;
        margin:24px 10px;
      }
    }
  }
  .for-button-list{
    background: #E5F2FE;
    margin:0 20px;
    display: flex;
    .for-title-p{
      width:175px;
      height:80px;
      line-height:80px;
      font-size:16px;
      color:#333;
      text-align: center;
    }
    .for-button-max-box{
      flex:1;
      .for-button-min-box{
        font-size:16px;
        display: inline-block;
        overflow: hidden;
        height:30px;
        line-height:30px;
        border-radius:6px;
        margin:25px 28px 0 0;
        cursor: pointer;
        i{
          height:30px;
          line-height:30px;
          font-size:16px;
          color:#fff;
          margin-right:6px;
        }
      }
      .colorAA{
        color: #ffffff;
        background: #0183FA;
        padding:0 26px 0 16px;
      }
      .colorBB{
        padding:0 26px;
        color: #333;
        background: #E0E0E0;
      }
    }
  }
  .template-name-box{
    display: flex;
    .template-name-p{
      font-weight:500;
      height:80px;
      line-height:80px;
      font-size:16px;
      color:#333;
      margin-left:20px;
      flex: 1;
    }
    .template-name-button{
      width:80px;
      height:40px;
      line-height:40px;
      margin:20px 20px 0 0;
    }
    .template-name-button-one{
      width:180px;
      height:40px;
      line-height:40px;
      margin:20px 40px 0 0;
      color:#fff;
      background: #0183FA;
      border: 1px solid #E0E0E0;
      cursor: pointer;
      font-size: 14px;
      text-align: center;
      border-radius: 6px;
    }
  }
  .table-for-max-box{
    border:1px solid #D7D7D7;
    margin:0 20px;
    *{
      margin:0;
      padding:0;
    }
    .table-title-box{
      background: rgba(1,131,250,0.1);
      display: flex;
      p{
        font-size:14px;
        font-weight:700;
        color:#333;
        line-height:48px;
        padding-left:20px;
      }
      p:nth-child(1){
        width:615px;
        border-right:1px solid #D7D7D7;
      }
    }
    .table-for-big-box{
      border-top:1px solid #D7D7D7;
      display: flex;
      font-size:14px;
      .max-title-box-null{
        width:615px!important;
      }
      .max-title-box{
        width:230px;
        border-right:1px solid #D7D7D7;
        min-height:48px;
        position: relative;
        p{
          position: absolute;
          top:50%;
          left:20px;
          height:18px;
          line-height:18px;
          margin-top:-9px;
        }
      }
      .max-right-box{
        flex:5;
        min-height:48px;
        .big-box:nth-child(1){
          border:none!important;
        }
        .big-box-null{
          line-height:48px;
          margin-left:20px;
          color:#606266;
        }
        .big-box{
          display: flex;
          border-top:1px solid #D7D7D7;
          .big-title-box-null{
            width:385px!important;
          }
          .big-title-box{
            width:180px;
            border-right:1px solid #D7D7D7;
            min-height:48px;
            position: relative;
            overflow: hidden;
            p{
              position: absolute;
              top:50%;
              left:20px;
              height:48px;
              line-height:48px;
              margin-top:-24px;
            }
          }
          .big-right-box-null{
            display: flex;
            .scope-box{
              width:430px;
              position: relative;
              overflow: hidden;
              border-right:1px solid #D7D7D7;
              p{
                position: absolute;
                top:50%;
                left:20px;
                height:48px;
                line-height:48px;
                margin-top:-24px;
              }
            }
            .permission-box{
              flex:1;
              position: relative;
              overflow: hidden;
              display: flex;
              .check-left-p{
                margin-left:20px;
                height:48px;
                line-height:48px;
              }
              .check-button-box{
                width:130px;
                height:36px;
                margin:6px 0 0 36px;
                line-height:36px;
                display: flex;
                color:#333;
                cursor: pointer;
                img{
                  width:16px;
                  height:16px;
                  margin:10px 14px 0 19px;
                }
              }
              .check-button-box-colorA{
                background: #CCE6FE;
                color:#0183FA;
              }
              .check-button-box-colorB{
                background: #E0E0E0;
                color:#333;
              }
            }
          }
          .big-right-box{
            flex:5;
            min-height:48px;
            .min-box:nth-child(1){
              border:none!important;
            }
            .min-box{
              display: flex;
              border-top:1px solid #D7D7D7;
              .min-title-box{
                width:205px;
                border-right:1px solid #D7D7D7;
                min-height:48px;
                position: relative;
                overflow: hidden;
                p{
                  position: absolute;
                  top:50%;
                  left:20px;
                  height:48px;
                  line-height:48px;
                  margin-top:-24px;
                }
              }
              .min-right-box{
                flex:5;
                min-height:48px;
                display: flex;
                .scope-box{
                  width:430px;
                  position: relative;
                  overflow: hidden;
                  border-right:1px solid #D7D7D7;
                  p{
                    position: absolute;
                    top:50%;
                    left:20px;
                    height:48px;
                    line-height:48px;
                    margin-top:-24px;
                  }
                }
                .permission-box{
                  flex:1;
                  position: relative;
                  overflow: hidden;
                  display: flex;
                  .check-left-p{
                    margin-left:20px;
                    height:48px;
                    line-height:48px;
                  }
                  .check-button-box{
                    width:130px;
                    height:36px;
                    margin:6px 0 0 36px;
                    line-height:36px;
                    display: flex;
                    color:#333;
                    cursor: pointer;
                    img{
                      width:16px;
                      height:16px;
                      margin:10px 14px 0 19px;
                    }
                  }
                  .check-button-box-colorA{
                    background: #CCE6FE;
                    color:#0183FA;
                  }
                  .check-button-box-colorB{
                    background: #E0E0E0;
                    color:#333;
                  }
                }
              }
            }
          }
        }
      }
    }
  }
  .bottom-button-box{
    display: flex;
    width:600px;
    margin:30px auto 10px;
    .null-p{
      flex:1;
    }
    .button-p-1{
      width:100px;
      margin-right:20px;
    }
    .button-p-2{
      width:100px;
      margin-right:20px;
    }
    .button-p-3{
      width:180px;
    }
  }
}
</style>
<style lang="scss">
.student-add-page{
  .top-info-box{
    .el-form-item{
      height:70px;
    }
    .el-input--small{
      width:250px!important;
    }
    .el-select{
      width:250px!important;
    }
    .el-input{
      width:250px!important;
    }
    .el-input__inner{
      width:250px!important;
    }
    .el-radio-group{
      width:250px!important;
    }
    .is-disabled{
      width:250px!important;
    }

    .switch .el-switch__label {
      position: absolute;
      display: none;
      color: #fff !important;
    }
    .switch .el-switch__label--right {
      z-index: 1;
    }
    .switch .el-switch__label--right span{
      margin-left: 10px;
    }
    .switch .el-switch__label--left {
      z-index: 1;
    }
    .switch .el-switch__label--left span{
      margin-left: 24px;
    }
    .switch .el-switch__label.is-active {
      display: block;
    }
    .switch.el-switch .el-switch__core,
    .el-switch .el-switch__label {
      width: 64px !important;
      margin: 0;
    }
  }
}
</style>
