<template>
  <el-form
    ref="siteForm"
    v-loading="loading"
    :model="form"
    :rules="rules"
    class="demo-ruleForm"
    size="small"
    label-width="150px"
  >
    <div class="base-dialog-body">
      <el-form-item label="Site Code: " v-if="form.Id" prop="SiteCode">
        <el-input :maxLength="$inputMax" v-model="form.SiteCode" disabled />
      </el-form-item>

      <el-form-item label="Site Name: " prop="SiteName">
        <el-input :maxLength="$inputMax" v-model="form.SiteName" />
      </el-form-item>

      <el-form-item label="Alias Name: ">
        <el-input :maxLength="$inputMax" v-model="form.AliasName" />
      </el-form-item>

      <el-form-item label="组织机构代码: ">
        <el-input :maxLength="$inputMax" v-model="form.UniqueCode" />
      </el-form-item>

      <el-form-item label="Country: " prop="Country">
        <el-input :maxLength="$inputMax" v-model="form.Country" />
      </el-form-item>

      <el-form-item label="City: " prop="City">
        <el-input :maxLength="$inputMax" v-model="form.City" />
      </el-form-item>
      <el-form-item label="Address: " prop="Address">
        <el-input :maxLength="$inputMax" v-model="form.Address" />
      </el-form-item>

      <el-form-item label="Affiliated Hospital: ">
        <el-select
          v-model="form.HospitalId"
          clearable
          placeholder="Please select"
          style="width:100%;"
        >
          <el-option
            v-for="item in hospitalList"
            :key="item.Id"
            :label="item.HospitalName"
            :value="item.Id"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="Director Name: " prop="DirectorName">
        <el-input :maxLength="$inputMax" v-model="form.DirectorName" />
      </el-form-item>

      <el-form-item label="Director Phone: " prop="DirectorPhone">
        <el-input :maxLength="$inputMax" v-model="form.DirectorPhone" />
      </el-form-item>

      <el-form-item label="Contact Name: " prop="ContactName">
        <el-input :maxLength="$inputMax" v-model="form.ContactName" />
      </el-form-item>

      <el-form-item label="Contact Phone: " prop="ContactPhone">
        <el-input :maxLength="$inputMax" v-model="form.ContactPhone" />
      </el-form-item>
    </div>
    <div class="base-dialog-footer" style="text-align:right;margin-top:10px;">
      <el-form-item>
        <el-button :disabled="btnLoading" size="small" type="primary" @click="handleCancel">Cancel</el-button>
        <el-button size="small" type="primary" :loading="btnLoading" @click="handleSave">Save</el-button>
      </el-form-item>
    </div>
  </el-form>
</template>
<script>
import { addOrUpdateSite } from '@/api/dictionary'
import store from '@/store'
export default {
  name: 'SiteForm',
  props: {
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      btnLoading: false,
      hospitalList: [],
      form: {
        Id: '',
        SiteCode: '',
        SiteName: '',
        AliasName: null,
        Country: '',
        City: '',
        HospitalId: '',
        DirectorName: '',
        DirectorPhone: '',
        ContactName: '',
        ContactPhone: '',
        UniqueCode: '',
        Address: ''
      },
      rules: {
        SiteName: [
          { required: true, message: '请注明', trigger: 'blur' },
          { max: 50, message: '最大长度为 50' }
        ],
        SiteCode: [
          { required: true, message: '请注明', trigger: 'blur' },
          { max: 50, message: '最大长度为 50' }
        ],
        Country: [
          { required: true, message: '请注明', trigger: 'blur' },
          { max: 50, message: '最大长度为 50' }
        ],
        City: [
          { required: true, message: '请注明', trigger: 'blur' },
          { max: 50, message: '最大长度为 50' }
        ],
        // HospitalId: [
        //   { required: true, message: '请注明', trigger: 'blur' }
        // ],
        Address: [
          { required: true, message: '请注明', trigger: 'blur' }
        ],
        DirectorName: [
          { max: 50, message: '最大长度为 50' }
        ],
        DirectorPhone: [
          { max: 50, message: '最大长度为 50' }
        ],
        ContactName: [
          { max: 50, message: '最大长度为 50' }
        ],
        ContactPhone: [
          { max: 50, message: '最大长度为 50' }
        ]
      },
      loading: false
    }
  },
  mounted() {
    this.initForm()
  },
  methods: {
    handleSave() {
      this.$refs.siteForm.validate(valid => {
        if (!valid) return
        this.btnLoading = true
        addOrUpdateSite(this.form).then(res => {
          this.btnLoading = false
          if (res.IsSuccess) {
            this.$message.success('Saved successfully')
            this.$refs['siteForm'].resetFields()
            this.$emit('getList')
            this.$emit('close')
          }
        }).catch(() => {
          this.btnLoading = false
        })
      })
    },
    handleCancel() {
      this.$emit('close')
    },
    async initForm() {
      this.loading = true
      this.hospitalList = await store.dispatch('global/getHospital')
      this.loading = false
      if (Object.keys(this.data).length && this.data.Id) {
        this.form = { ...this.data }
      }
    }
  }

}
</script>
