<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 v-model="form.SiteCode" disabled />
      </el-form-item>

      <!-- 中心名称 -->
      <el-form-item :label="$t('trials:customSite:form:siteName')" prop="SiteName">
        <el-input v-model="form.SiteName" />
      </el-form-item>
      <!-- 中心名称（CN） -->
      <el-form-item :label="$t('trials:customSite:form:siteName') + '（CN）'" v-if="!show" prop="SiteNameCN">
        <el-input v-model="form.SiteNameCN" />
      </el-form-item>

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

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

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

      <el-form-item label="City: " prop="City">
        <el-input v-model="form.City" />
      </el-form-item>
      <el-form-item label="Address: " prop="Address">
        <el-input 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 v-model="form.DirectorName" />
      </el-form-item>

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

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

      <el-form-item label="Contact Phone: " 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">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: 'Please specify', trigger: 'blur' },
          { max: 50, message: 'The maximum length is 50' }
        ],
        SiteNameCN: [
          { required: true, message: 'Please specify', trigger: 'blur' },
          { max: 50, message: 'The maximum length is 50' }
        ],
        SiteCode: [
          { required: true, message: 'Please specify', trigger: 'blur' },
          { max: 50, message: 'The maximum length is 50' }
        ],
        Country: [
          { required: true, message: 'Please specify', trigger: 'blur' },
          { max: 50, message: 'The maximum length is 50' }
        ],
        City: [
          { required: true, message: 'Please specify', trigger: 'blur' },
          { max: 50, message: 'The maximum length is 50' }
        ],
        // HospitalId: [
        //   { required: true, message: 'Please specify', trigger: 'blur' }
        // ],
        Address: [
          { required: true, message: 'Please specify', trigger: 'blur' }
        ],
        DirectorName: [
          { max: 50, message: 'The maximum length is 50' }
        ],
        DirectorPhone: [
          { max: 50, message: 'The maximum length is 50' }
        ],
        ContactName: [
          { max: 50, message: 'The maximum length is 50' }
        ],
        ContactPhone: [
          { max: 50, message: 'The maximum length is 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('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>
