<template>
  <div class="AddPlanWrapper clearfix">
    <crumbs title="医院用户管理-添加用户" class="topBack" :param="$route.params" linkTo="HospitalUserCDSS"></crumbs>
    <div class="AddPlanBox">
      <el-row :gutter="20">
        <el-col :span="16">
          <el-form ref="form" :model="form" label-width="110px" :rules="rules">
            <el-form-item label="所属医院：" class="selectMedicine" prop="hospitalId">
              <el-select v-model="form.hospitalId" placeholder="请选择" clearable style="width: 100%">
                <el-option
                  v-for="item in HospitalInfoList"
                  :label="item.name"
                  :value="item.id"
                  :key="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="用户名" prop="userName">
              <el-input v-model="form.userName" placeholder="用作登录,6-20位,可输入字母、数字和符号"></el-input>
            </el-form-item>

            <el-form-item label="联系人姓名">
              <el-input v-model="form.linkman" placeholder="请输入"></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="onSubmit" :disabled="saveDisable">确定</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>

<script>
import api from '@api/cdss.js';
export default {
  name: 'AddHospitalUserCDSS',
  data() {
    var numreg = /^[a-zA-Z0-9_\u4e00-\u9fa5]+$/;
    var numreg1 = /^[0-9a-zA-Z_]{1,}$/;
    var validatePass = (rule, value, callback) => {
      if (!numreg.test(value)) {
        callback(new Error('汉字、字母、数字和下划线'));
      } else {
        callback();
      }
    };
    var validatePass1 = (rule, value, callback) => {
      if (!numreg1.test(value)) {
        callback(new Error('字母、数字和下划线'));
      } else {
        callback();
      }
    };
    return {
      form: {
        hospitalId: '',
        userName: '',
        linkman: ''
      },
      rules: {
        userName: [
          { required: true, message: '用户名不能为空', trigger: 'change' },
          { min: 6, max: 20, message: '长度6-20位', trigger: 'blur' },
          { required: true, validator: validatePass, trigger: 'blur' }
        ],
        hospitalId: [
          { required: true, message: '请选择所属医院', trigger: 'change' },
          // { min: 2, max: 30, message: '长度4-15位', trigger: 'blur' },
          // { required: true, validator: validatePass1, trigger: 'blur' }
        ]
      },

      saveDisable: false,
      HospitalInfoList: []
    };
  },
  created() {
    this._getHospitalInfo();
  },

  methods: {
    onSubmit() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.saveDisable = true;
          let params = {
            hospitalId: this.form.hospitalId,
            linkman: this.form.linkman,
            username: this.form.userName
          };

          api.addUserRoleCDSS(params).then(res => {
            if (res.data.code === '0') {
              this.$message({
                showClose: true,
                message: '保存成功',
                type: 'success',
                duration: 1000
              });
              this.isSaveSuccess = true; // 保存成功,可正常退出
              this.$router.push({
                name: 'HospitalUserCDSS',
                params: Object.assign({}, this.$route.params, {
                  currentPage: 1
                })
              });
            } else {
              this.$message({
                showClose: true,
                message: res.data.msg,
                type: 'error',
                duration: 1000
              });
            }
            this.saveDisable = false;
          });
        }
      });
    },

    // 获取医院下拉列表
    _getHospitalInfo() {
      api.getHospitalInfo().then(res => {
        if (res.data.code === '0') {
          this.HospitalInfoList = res.data.data;
        }
      });
    }
  }
};
</script>

<style lang="less" scoped>
.AddPlanWrapper {
  min-width: 940px;
  .AddPlanBox {
    padding: 20px 60px 120px 60px;
    margin: 70px 20px 0 20px;
    background: #fff;
  }
  color: #606266;
  .topBack {
    top: 0;
  }
  .title {
    background-color: #f2f2f2;
    display: flex;
    .handleIcon {
      width: 30px;
      cursor: pointer;
      height: 40px;
      display: flex;
      justify-content: center;
      align-items: center;
      img {
        width: 20px;
        height: 20px;
      }
      .open {
        transform: rotate(180deg);
      }
      .close {
        transform: rotate(0deg);
      }
    }
    .titlwSwitch {
      width: 120px;
    }
    h4 {
      flex: 1;
    }
    .titlwSwitchStatus {
      margin-left: 16px;
    }
  }
  .sub {
    .planItem {
      display: flex;
      .sort {
        width: 60px;
        display: flex;
        .top {
          display: flex;
          justify-content: center;
          align-items: center;
          width: 30px;
          cursor: pointer;
          img {
            width: 10px;
            height: 14px;
          }
        }
        .down {
          width: 30px;
          cursor: pointer;
          display: flex;
          justify-content: center;
          align-items: center;
          img {
            width: 10px;
            height: 14px;
          }
        }
      }
      .openOrClose {
        display: flex;
        flex: 1;
        .planInfo {
          width: 140px;
        }
        .switch {
        }
        .planStatus {
          margin-left: 16px;
        }
      }
      .showNum {
        display: flex;
        width: 160px;
        /deep/.el-input--small {
          width: 60px;
        }
      }
    }
  }
  .el-button {
    float: right;
  }
  .plus-icon-enter-active {
    transition: all 0.8s;
  }
  .plus-icon-enter {
    opacity: 0;
    margin-top: 12px;
  }
  .plus-icon-leave-active {
    transition: all 0.8s;
  }
  .plus-icon-leave-active {
    opacity: 0;
    margin-top: 12px;
  }
}
.leaveBox {
  /deep/ .leaveBtn {
    // margin-right: 46px;
    background-color: #d7d7d7 !important;
    border-color: transparent;
  }
}
</style>