<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">
      <!-- Site Code -->
      <el-form-item :label="$t('institutions:sites:label:siteCode')" v-if="form.Id" prop="SiteCode">
        <el-input v-model="form.SiteCode" disabled />
      </el-form-item>

      <!-- 中心名称 -->
      <el-form-item :label="$t('institutions:sites:label:siteName')" prop="SiteName">
        <el-input v-model="form.SiteName" />
      </el-form-item>
      <!-- 中心名称（CN） -->
      <el-form-item :label="$t('institutions:sites:label:siteNameCN')" prop="SiteNameCN">
        <el-input v-model="form.SiteNameCN" />
      </el-form-item>
      <!-- Alias Name -->
      <el-form-item :label="$t('institutions:sites:label:aliasName')">
        <el-input v-model="form.AliasName" />
      </el-form-item>

      <!-- <el-form-item label="组织机构代码: ">
        <el-input v-model="form.UniqueCode" />
      </el-form-item> -->
      <!-- Country -->
      <el-form-item :label="$t('institutions:sites:label:country')" prop="Country">
        <el-select v-model="form.Country" style="width: 100%">
          <el-option v-for="item of $d.SiteCountry" :key="item.id" :label="item.label" :value="item.label" />
        </el-select>
      </el-form-item>
      <!-- City -->
      <el-form-item :label="$t('institutions:sites:label:city')" prop="City">
        <el-input v-model="form.City" />
      </el-form-item>
      <!-- Address -->
      <el-form-item :label="$t('institutions:sites:label:address')" prop="Address">
        <el-input v-model="form.Address" />
      </el-form-item>
      <!-- Affiliated Hospital -->
      <el-form-item :label="$t('institutions:sites:label:affiliatedHospital')">
        <el-select v-model="form.HospitalId" clearable style="width: 100%">
          <el-option v-for="item in hospitalList" :key="item.Id" :label="item.HospitalName" :value="item.Id" />
        </el-select>
      </el-form-item>
      <!-- Director Name -->
      <el-form-item :label="$t('institutions:sites:label:directorName')" prop="DirectorName">
        <el-input v-model="form.DirectorName" />
      </el-form-item>
      <!-- Director Phone -->
      <el-form-item :label="$t('institutions:sites:label:directorPhone')" prop="DirectorPhone">
        <el-input v-model="form.DirectorPhone" />
      </el-form-item>
      <!-- Contact Name -->
      <el-form-item :label="$t('institutions:sites:label:contactName')" prop="ContactName">
        <el-input v-model="form.ContactName" />
      </el-form-item>
      <!-- Contact Phone -->
      <el-form-item :label="$t('institutions:sites:label:contactPhone')" prop="ContactPhone">
        <el-input 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">{{
          $t('common:button:cancel') }}</el-button>
        <el-button size="small" type="primary" :loading="btnLoading" @click="handleSave">{{ $t('common:button: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: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          // { max: 50, message: 'The maximum length is 50' }
        ],
        SiteNameCN: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          // { max: 50, message: 'The maximum length is 50' }
        ],
        SiteCode: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { max: 50, message: this.$t('common:ruleMessage:maxLength') + ' 50' },
        ],
        Country: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { max: 50, message: this.$t('common:ruleMessage:maxLength') + ' 50' },
        ],
        City: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { max: 50, message: this.$t('common:ruleMessage:maxLength') + ' 50' },
        ],
        // HospitalId: [
        //   { required: true, message: 'Please specify', trigger: 'blur' }
        // ],
        Address: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
        ],
        DirectorName: [{ max: 50, message: this.$t('common:ruleMessage:maxLength') + ' 50' }],
        DirectorPhone: [{ max: 50, message: this.$t('common:ruleMessage:maxLength') + ' 50' }],
        ContactName: [{ max: 50, message: this.$t('common:ruleMessage:maxLength') + ' 50' }],
        ContactPhone: [{ max: 50, message: this.$t('common:ruleMessage:maxLength') + ' 50' }],
      },
      loading: false,
      show: false,
    }
  },
  mounted() {
    this.initForm()
    this.show = process.env.VUE_APP_OSS_PATH === '/usa/dist'
  },
  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(this.$t('common:message:savedSuccessfully'))
              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>
