<template>
  <el-form
    ref="customSiteForm"
    v-loading="loading"
    :model="form"
    :rules="rules"
    class="demo-ruleForm"
    size="small"
    label-width="150px"
  >
    <div class="base-dialog-body">
      <!-- 中心编号 -->
      <el-form-item :label="$t('trials:customSite:form:siteId')" prop="SiteCode">
        <el-input v-model="form.SiteCode" />
      </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="$t('trials:customSite:form:orgCode')">
        <el-input v-model="form.UniqueCode" />
      </el-form-item>
      <!-- 国家 -->
      <el-form-item :label="$t('trials:customSite:form:country')" prop="Country">
        <el-input v-model="form.Country" />
      </el-form-item>
      <!-- 城市 -->
      <el-form-item :label="$t('trials:customSite:form:city')" prop="City">
        <el-input v-model="form.City" />
      </el-form-item>
      <!-- 地址 -->
      <el-form-item :label="$t('trials:customSite:form:address')" prop="Address">
        <el-input v-model="form.Address" />
      </el-form-item>
      <!-- 附属医院 -->
      <el-form-item :label="$t('trials:customSite:form:hospital')">
        <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>
      <!-- 负责人 -->
      <el-form-item :label="$t('trials:customSite:form:director')" prop="DirectorName">
        <el-input v-model="form.DirectorName" />
      </el-form-item>
      <!-- 负责人电话 -->
      <el-form-item :label="$t('trials:customSite:form:directorPhone')" prop="DirectorPhone">
        <el-input v-model="form.DirectorPhone" />
      </el-form-item>
      <!-- 联系人 -->
      <el-form-item :label="$t('trials:customSite:form:contactor')" prop="ContactName">
        <el-input v-model="form.ContactName" />
      </el-form-item>
      <!-- 联系人电话 -->
      <el-form-item :label="$t('trials:customSite:form:contactorPhone')" 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: 'CustomSiteForm',
  props: {
    data: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      btnLoading: false,
      hospitalList: [],
      form: {
        Id: '',
        SiteCode: '',
        SiteName: '',
        SiteNameCN: '',
        Country: '',
        City: '',
        HospitalId: '',
        DirectorName: '',
        DirectorPhone: '',
        ContactName: '',
        ContactPhone: '',
        UniqueCode: '',
        Address: ''
      },
      rules: {
        SiteName: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { max: 50, message: `${this.$t('common:ruleMessage:maxLength')} 50` }
        ],
        SiteNameCN: [
          { required: true, message: this.$t('common:ruleMessage:specify'), trigger: 'blur' },
          { max: 50, message: `${this.$t('common:ruleMessage:maxLength')} 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.customSiteForm.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.$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
    }
  }

}
</script>
