<template>
  <el-form
    ref="hospitalForm"
    :model="form"
    :rules="rules"
    class="demo-ruleForm"
    size="small"
    label-width="170px"
  >
    <div class="base-dialog-body">
      <!-- Site -->
      <el-form-item :label="$t('institutions:hospitals:label:site')">
        <el-select
          v-model="form.SiteId"
          style="width: 100%"
          clearable
          filterable
          @change="siteChange"
        >
          <el-option
            v-for="item of siteList"
            :value="item.Id"
            :key="item.Id"
            :label="item.SiteName"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- Hospital CN -->
      <el-form-item :label="$t('institutions:hospitals:label:hospitalCN')" prop="HospitalNameCN">
        <el-input v-model="form.HospitalNameCN" />
      </el-form-item>
      <!-- Hospital EN -->
      <el-form-item :label="$t('institutions:hospitals:label:hospital')" prop="HospitalName">
        <el-input v-model="form.HospitalName" />
      </el-form-item>
      <!-- Affiliated University CN -->
      <el-form-item
        :label="$t('institutions:hospitals:label:universityAffiliatedCN')"
        prop="UniversityAffiliatedCN"
      >
        <el-input v-model="form.UniversityAffiliatedCN" />
      </el-form-item>
      <!-- Affiliated University EN -->
      <el-form-item
        :label="$t('institutions:hospitals:label:universityAffiliated')"
        prop="UniversityAffiliated"
      >
        <el-input v-model="form.UniversityAffiliated" />
      </el-form-item>
      <!-- Country CN -->
      <el-form-item :label="$t('institutions:hospitals:label:countryCN')" prop="CountryCN">
        <el-input v-model="form.CountryCN" />
      </el-form-item>
      <!-- Country EN -->
      <el-form-item :label="$t('institutions:hospitals:label:country')" prop="Country">
        <el-input v-model="form.Country" />
      </el-form-item>
      <!-- Province CN -->
      <el-form-item :label="$t('institutions:hospitals:label:provinceCN')" prop="ProvinceCN">
        <el-input v-model="form.ProvinceCN" />
      </el-form-item>
      <!-- Province EN -->
      <el-form-item :label="$t('institutions:hospitals:label:province')" prop="Province">
        <el-input v-model="form.Province" />
      </el-form-item>
      <!-- City CN -->
      <el-form-item :label="$t('institutions:hospitals:label:cityCN')" prop="CityCN">
        <el-input v-model="form.CityCN" />
      </el-form-item>
      <!-- City EN -->
      <el-form-item :label="$t('institutions:hospitals:label:city')" prop="City">
        <el-input v-model="form.City" />
      </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 { addOrUpdateHospital } from '@/api/dictionary'
import { getAllSiteList } from '@/api/global'
export default {
  name: 'HospitalForm',
  props: {
    data: {
      type: Object,
      default() {
        return {}
      },
    },
    model: {
      type: Object,
      default() {
        return {}
      },
    },
  },
  data() {
    return {
      btnLoading: false,
      form: {
        SiteId: null,
        Id: '',
        HospitalName: '',
        HospitalNameCN: '',
        UniversityAffiliated: '',
        UniversityAffiliatedCN: '',
        Country: '',
        CountryCN: '',
        Province: '',
        ProvinceCN: '',
        City: '',
        CityCN: '',
      },
      siteList: [],
      rules: {
        HospitalNameCN: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { max: 50, message: this.$t('common:ruleMessage:maxLength') + ' 50' },
        ],
        UniversityAffiliated: [
          { max: 100, message: this.$t('common:ruleMessage:maxLength') + ' 100' },
        ],
        UniversityAffiliatedCN: [
          { max: 100, message: this.$t('common:ruleMessage:maxLength') + ' 100' },
        ],
        Country: [{ max: 50, message: this.$t('common:ruleMessage:maxLength') + ' 50' }],
        CountryCN: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { max: 50, message: this.$t('common:ruleMessage:maxLength') + ' 50' },
        ],
        Province: [{ max: 50, message: this.$t('common:ruleMessage:maxLength') + ' 50' }],
        ProvinceCN: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { max: 50, message: this.$t('common:ruleMessage:maxLength') + ' 50' },
        ],
        City: [{ max: 50, message: this.$t('common:ruleMessage:maxLength') + ' 50' }],
        CityCN: [
          { required: true, message: 'Please specify', trigger: 'blur' },
          { max: 50, message: this.$t('common:ruleMessage:maxLength') + ' 50' },
        ],
      },
    }
  },
  mounted() {
    this.getAllSiteList()
    if (Object.keys(this.data).length && this.data.Id) {
      this.form = { ...this.data }
    }
  },
  methods: {
    siteChange(v) {
      this.siteList.forEach((o) => {
        if (o.Id === v) {
          this.form.CityCN = o.City
          this.form.HospitalNameCN = o.SiteNameCN
          this.form.HospitalName = o.SiteName
          this.form.ProvinceCN = o.Province
          this.form.CountryCN = o.Country
        }
      })
    },
    getAllSiteList() {
      getAllSiteList()
        .then((res) => {
          this.siteList = res.Result
          console.log(this.siteList)
        })
        .catch(() => {
          reject()
        })
    },
    handleSave() {
      this.$refs.hospitalForm.validate((valid) => {
        if (!valid) return
        this.btnLoading = true
        addOrUpdateHospital(this.form)
          .then((res) => {
            this.btnLoading = false
            if (res.IsSuccess) {
              this.$message.success(this.$t('common:message:savedSuccessfully'))
              this.$refs['hospitalForm'].resetFields()
              this.$emit('getList')
              this.$emit('close')
            }
          })
          .catch(() => {
            this.btnLoading = false
          })
      })
    },
    handleCancel() {
      this.$emit('close')
    },
  },
}
</script>
