<template>
  <el-form
    ref="userForm"
    size="small"
    :model="user"
    :rules="userFormRules"
    label-width="150px"
    style="width: 800px"
  >
    <el-card class="Basic" shadow="never" size="small">
      <div slot="header" class="clearfix">
        <span>Basic Information</span>
      </div>
      <el-form-item v-if="user.UserCode" label="S/N: " prop="UserCode">
        <el-input v-model="user.UserCode" disabled />
      </el-form-item>

      <el-form-item label="User ID: " prop="UserName">
        <el-input v-model="user.UserName" />
      </el-form-item>

      <el-form-item label="Surname: " prop="LastName">
        <el-input v-model="user.LastName" />
      </el-form-item>
      <el-form-item label="Given Name: " prop="FirstName">
        <el-input v-model="user.FirstName" />
      </el-form-item>
      <el-form-item label="Gender: " prop="Sex" style="margin-right: 40px">
        <el-radio-group v-model="user.Sex">
          <el-radio :label="1">Male</el-radio>
          <el-radio :label="0">Female</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="Email: " prop="EMail">
        <el-input v-model="user.EMail" />
      </el-form-item>
      <el-form-item label="Phone: " prop="Phone">
        <el-input v-model="user.Phone" />
      </el-form-item>
      <el-form-item v-if="type == 1" label="Disable:">
        <el-switch
          v-model="user.Status"
          :active-value="0"
          :inactive-value="1"
        />
      </el-form-item>
      <el-form-item label="User Type: " prop="UserTypeId">
        <el-select
          ref="userType"
          v-model="user.UserTypeId"
          size="small"
          placeholder="Please select"
          style="width: 100%"
          :disabled="user.CanEditUserType === false"
        >
          <el-option
            v-for="(userType, key) of userTypeOptions"
            :key="key"
            :label="userType.UserType"
            :value="userType.Id"
          />
        </el-select>
      </el-form-item>
    </el-card>

    <el-card
      class="Affiliation"
      shadow="never"
      style="margin-top: 10px"
      size="small"
    >
      <div slot="header" class="clearfix">
        <span>Affiliation</span>
      </div>
      <el-form-item prop="IsZhiZhun">
        <el-radio-group
          v-model="user.IsZhiZhun"
          @change="OrgnizationTypeChanged"
        >
          <el-radio :label="true">Internal</el-radio>
          <el-radio :label="false">External</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        v-show="user.IsZhiZhun === false"
        label="Organization Name: "
      >
        <el-input v-model="user.OrganizationName" />
      </el-form-item>

      <el-form-item label="Department: " prop="DepartmentName">
        <el-input v-model="user.DepartmentName" />
      </el-form-item>
      <el-form-item label="Position: " prop="PositionName">
        <el-input v-model="user.PositionName" />
      </el-form-item>
    </el-card>
    <el-form-item>
      <el-button
        type="primary"
        size="small"
        :disabled="isDisabled"
        style="margin: 10px 15px"
        @click="handleSave"
        >Save</el-button
      >
    </el-form-item>
  </el-form>
</template>
<script>
import {
  getUser,
  addUser,
  updateUser,
  getUserTypeListByUserType,
} from '@/api/admin.js'
export default {
  name: 'UserInfo',
  props: {
    userId: { type: String, default: '' },
  },
  data() {
    return {
      user: {
        UserName: '',
        LastName: '',
        FirstName: '',
        Sex: '',
        EMail: '',
        Phone: '',
        UserTypeId: '',
        IsZhiZhun: '',
        OrganizationName: '',
        DepartmentName: '',
        PositionName: '',
      },
      userFormRules: {
        UserName: [
          { required: true, message: 'Please specify', trigger: 'blur' },
          { max: 50, message: 'The maximum length is 50' },
        ],
        UserTypeId: [
          {
            required: true,
            message: 'Please Select',
            trigger: ['blur', 'change'],
          },
        ],
        IsZhiZhun: [
          {
            required: true,
            message: 'Please Select',
            trigger: ['blur', 'change'],
          },
        ],
        OrganizationName: [
          { required: true, message: 'Please specify', trigger: 'blur' },
        ],
        LastName: [
          { required: true, message: 'Please specify', trigger: 'blur' },
          { max: 50, message: 'The maximum length is 50' },
        ],
        FirstName: [
          { required: true, message: 'Please specify', trigger: 'blur' },
          { max: 50, message: 'The maximum length is 50' },
        ],
        Phone: [
          {
            max: 20,
            min: 7,
            message: 'The length is 7 to 20',
            trigger: ['blur'],
          },
        ],
        EMail: [
          {
            required: true,
            message: 'Please input the email address',
            trigger: 'blur',
          },
          {
            type: 'email',
            message: 'Please input the correct email address',
            trigger: ['blur'],
          },
          { max: 50, message: 'The maximum length is 50' },
        ],
        Sex: [{ required: true, message: 'Please specify', trigger: 'blur' }],
        Status: [
          { required: true, message: 'Please specify', trigger: 'blur' },
        ],
        DepartmentName: [{ max: 50, message: 'The maximum length is 50' }],
        PositionName: [{ max: 50, message: 'The maximum length is 50' }],
      },
      userTypeOptions: [],
      isDisabled: false,
      type: 0, // 1为编辑，0为新增
    }
  },
  created() {
    this.getUserTypeList()
    if (this.userId !== '') {
      this.getUserInfo()
      this.type = 1
    } else {
      this.type = 0
    }
  },
  methods: {
    handleSave() {
      this.$refs.userForm.validate((valid) => {
        if (valid) {
          this.isDisabled = true
          const selectedUserType = this.userTypeOptions.filter(
            (item) => item.Id === this.user.UserTypeId
          )
          if (selectedUserType.length > 0) {
            this.user.UserTypeEnum = selectedUserType[0].UserTypeEnum
          }
          if (this.user.Id) {
            updateUser(this.user)
              .then((res) => {
                this.isDisabled = false
                this.$message.success('Updated successfully')
              })
              .catch(() => {
                this.isDisabled = false
              })
          } else {
            addUser(this.user)
              .then((res) => {
                this.isDisabled = false
                this.user.Id = res.Result.Id
                this.user.UserCode = res.Result.UserCode
                this.$emit('getUserId', res.Result.Id)
                this.$message.success('Added successfully')
                this.$router.push({ path: '/system/user/list' })
              })
              .catch(() => {
                this.isDisabled = false
              })
          }
        }
      })
    },
    getUserTypeList() {
      getUserTypeListByUserType(0).then((res) => {
        if (res.IsSuccess) {
          this.userTypeOptions = res.Result
        }
      })
    },
    getUserInfo() {
      getUser({
        IdentityUserId: this.userId,
      }).then((res) => {
        this.user = res.Result
      })
    },
    OrgnizationTypeChanged(val) {
      this.user.OrganizationName = ''
    },
  },
}
</script>
