<template>
  <p class="inquire-button-one" @click="upDataButton">提交</p>
</template>

<script>
  import { addUserTeacher, putUserTeacherNew } from "@/api/system/user_teacher";
  export default {
    name: 'addPageSubPagePublic',
    methods:{
      upDataButton(){
        let self = this;
        self.$parent.$refs["form"].validate(valid => {
          if (valid) {
            if(self.$parent.form.email){
              let re = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
              if(!re.test(self.$parent.form.email)){
                self.msgError('请输入正确的邮箱地址')
                return
              }
            }
            let list = self.$parent.toArray();
            let obj = JSON.parse(JSON.stringify(self.$parent.form));
            obj.permitMenus = [];
            if(obj.status == 0){
              for(let i=0;i<list.length;i++){
                let minObj = {
                  menuId:list[i].menuId,
                }
                if(list[i].dataScope){
                  minObj.dataScope = list[i].dataScope
                  if(list[i].dataScope == 3){
                    minObj.deptIds = list[i].deptIds;
                    minObj.deptIdsList = list[i].deptIdsList;
                  }
                }else{
                  minObj.dataScope = 0
                }
                obj.permitMenus.push(minObj)
              }
            }
            if(self.$parent.propsData.userId){
              //修改
              obj.userId = self.$parent.propsData.userId;
              self.putUserTeacherNew(obj);
            }else{
              //发布
              self.addUserTeacher(obj);
            }
          }
        });
      },
      putUserTeacherNew(obj){
        let self = this;
        putUserTeacherNew(obj).then(response => {
          if(response.code == 200){
            self.msgSuccess(response.msg);
            self.$parent.outPageButton();
          }else if(response.code==205){
            self.$confirm(response.msg, "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              //  确定
              self.$router.push({ path: '/comprehensive/laboratoryManagement/accessAuthorization' });
            }).catch(function() {
              //  取消
              self.$parent.outPageButton();
            });
          }
        });
      },
      addUserTeacher(obj){
        let self = this;
        addUserTeacher(obj).then(response => {
          if(response.code == 200){
            self.msgSuccess(response.msg);
            self.$parent.outPageButton();
          }else if(response.code==205){
            self.$confirm(response.msg, "警告", {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning"
            }).then(() => {
              //  确定
              self.$router.push({ path: '/comprehensive/laboratoryManagement/accessAuthorization' });
            }).catch(function() {
              //  取消
              self.$parent.outPageButton();
            });
          }
        });
      },
    }
  }
</script>

<style scoped>

</style>
