<template>
  <el-form
    ref="hospitalForm"
    :model="form"
    :rules="rules"
    class="demo-ruleForm"
    size="small"
    label-width="170px"
  >
    <div class="base-dialog-body">
      <el-form-item label="Hospital CN: " prop="SiteId">
        <el-select v-model="form.SiteId" style="width: 100%" 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>
      <el-form-item label="Hospital EN: " prop="HospitalName">
        <el-input :maxLength="$inputMax" v-model="form.HospitalName" />
      </el-form-item>
      <el-form-item label="Affiliated University CN: " prop="UniversityAffiliatedCN">
        <el-input :maxLength="$inputMax" v-model="form.UniversityAffiliatedCN" />
      </el-form-item>
      <el-form-item label="Affiliated University EN: " prop="UniversityAffiliated">
        <el-input :maxLength="$inputMax" v-model="form.UniversityAffiliated" />
      </el-form-item>
      <el-form-item label="Country CN: " prop="CountryCN">
        <el-input :maxLength="$inputMax" v-model="form.CountryCN" />
      </el-form-item>
      <el-form-item label="Country EN: " prop="Country">
        <el-input :maxLength="$inputMax" v-model="form.Country" />
      </el-form-item>
      <el-form-item label="Province CN: " prop="ProvinceCN">
        <el-input :maxLength="$inputMax" v-model="form.ProvinceCN" />
      </el-form-item>
      <el-form-item label="Province EN: " prop="Province">
        <el-input :maxLength="$inputMax" v-model="form.Province" />
      </el-form-item>
      <el-form-item label="City CN: " prop="CityCN">
        <el-input :maxLength="$inputMax" v-model="form.CityCN" />
      </el-form-item>
      <el-form-item label="City EN: " prop="City">
        <el-input :maxLength="$inputMax" 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">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 { 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: {
        SiteId: [{ required: true, message: '请注明', trigger: 'blur' },
          { max: 50, message: '最大长度为 50' }],
        HospitalName: [{ required: true, message: '请注明', trigger: 'blur' }, { max: 50, message: '最大长度为 50' }],
        UniversityAffiliated: [{ max: 100, message: '最大长度为 100' }],
        UniversityAffiliatedCN: [{ max: 100, message: '最大长度为 100' }],
        Country: [{ required: true, message: '请注明', trigger: 'blur' },
          { max: 50, message: '最大长度为 50' }],
        CountryCN: [{ max: 50, message: '最大长度为 50' }],
        Province: [{ required: true, message: '请注明', trigger: 'blur' },
          { max: 50, message: '最大长度为 50' }],
        ProvinceCN: [{ max: 50, message: '最大长度为 50' }],
        City: [{ required: true, message: '请注明', trigger: 'blur' },
          { max: 50, message: '最大长度为 50' }],
        CityCN: [{ max: 50, message: '最大长度为 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.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('Saved successfully')
            this.$refs['hospitalForm'].resetFields()
            this.$emit('getList')
            this.$emit('close')
          }
        }).catch(() => {
          this.btnLoading = false
        })
      })
    },
    handleCancel() {
      this.$emit('close')
    }
  }

}
</script>
