<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="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>
      <el-form-item label="Hospital CN: " prop="HospitalNameCN">
        <el-input v-model="form.HospitalNameCN" />
      </el-form-item>
      <el-form-item label="Hospital EN: " prop="HospitalName">
        <el-input v-model="form.HospitalName" />
      </el-form-item>
      <el-form-item
        label="Affiliated University CN: "
        prop="UniversityAffiliatedCN"
      >
        <el-input v-model="form.UniversityAffiliatedCN" />
      </el-form-item>
      <el-form-item
        label="Affiliated University EN: "
        prop="UniversityAffiliated"
      >
        <el-input v-model="form.UniversityAffiliated" />
      </el-form-item>
      <el-form-item label="Country CN: " prop="CountryCN">
        <el-input v-model="form.CountryCN" />
      </el-form-item>
      <el-form-item label="Country EN: " prop="Country">
        <el-input v-model="form.Country" />
      </el-form-item>
      <el-form-item label="Province CN: " prop="ProvinceCN">
        <el-input v-model="form.ProvinceCN" />
      </el-form-item>
      <el-form-item label="Province EN: " prop="Province">
        <el-input v-model="form.Province" />
      </el-form-item>
      <el-form-item label="City CN: " prop="CityCN">
        <el-input v-model="form.CityCN" />
      </el-form-item>
      <el-form-item label="City EN: " 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"
          >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: {
        HospitalNameCN: [
          { required: true, message: 'Please specify', trigger: 'blur' },
          { max: 50, message: 'The maximum length is 50' },
        ],
        UniversityAffiliated: [
          { max: 100, message: 'The maximum length is 100' },
        ],
        UniversityAffiliatedCN: [
          { max: 100, message: 'The maximum length is 100' },
        ],
        Country: [{ max: 50, message: 'The maximum length is 50' }],
        CountryCN: [
          { required: true, message: 'Please specify', trigger: 'blur' },
          { max: 50, message: 'The maximum length is 50' },
        ],
        Province: [{ max: 50, message: 'The maximum length is 50' }],
        ProvinceCN: [
          { required: true, message: 'Please specify', trigger: 'blur' },
          { max: 50, message: 'The maximum length is 50' },
        ],
        City: [{ max: 50, message: 'The maximum length is 50' }],
        CityCN: [
          { required: true, message: 'Please specify', trigger: 'blur' },
          { max: 50, message: 'The maximum length is 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('Saved successfully')
              this.$refs['hospitalForm'].resetFields()
              this.$emit('getList')
              this.$emit('close')
            }
          })
          .catch(() => {
            this.btnLoading = false
          })
      })
    },
    handleCancel() {
      this.$emit('close')
    },
  },
}
</script>
