<template>
  <div class="hospital">
    <el-form
      ref="hospitalForm"
      size="small"
      :model="hospital"
      :rules="hospitalFormRules"
      label-width="150px"
      style="width: 800px"
    >
      <el-card class="Basic" shadow="never" size="small">
        <el-form-item label="医院名称: " prop="HospitalName">
          <el-input v-model="hospital.HospitalName" :disabled="disabled" />
        </el-form-item>
        <el-form-item label="医院编码: " prop="HospitalCode">
          <el-input
            v-model="hospital.HospitalCode"
            :disabled="disabled || hasPermi(['role:oa'])"
          />
        </el-form-item>

        <el-form-item label="医院别称: " prop="HospitalAliasName">
          <el-input v-model="hospital.HospitalAliasName" :disabled="disabled" />
        </el-form-item>

        <el-form-item label="国家: " prop="Country">
          <el-input v-model="hospital.Country" :disabled="disabled" />
        </el-form-item>
        <el-form-item label="省份: " prop="Province">
          <el-input v-model="hospital.Province" :disabled="disabled" />
        </el-form-item>
        <el-form-item label="城市: " prop="City">
          <el-input v-model="hospital.City" :disabled="disabled" />
        </el-form-item>
        <el-form-item label="地址: " prop="Address">
          <el-input v-model="hospital.Address" :disabled="disabled" />
        </el-form-item>
        <el-form-item label="联系方式: " prop="Phone">
          <el-input v-model="hospital.Phone" :disabled="disabled" />
        </el-form-item>
        <el-form-item label="未激活项目数: " prop="TrialKeepCount">
          <el-input
            v-model="hospital.TrialKeepCount"
            :disabled="disabled || hasPermi(['role:oa'])"
            type="number"
          />
        </el-form-item>
        <el-form-item label="是否可以连接互联网: ">
          <el-switch
            v-model="hospital.IsCanConnectInternet"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="是"
            inactive-text="否"
            :disabled="disabled"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="LOGO: " prop="HospitalLogoPath">
          <upload-logo
            :path.sync="hospital.HospitalLogoPath"
            :disabled="disabled"
          />
        </el-form-item>
      </el-card>
      <el-form-item v-hasPermi="['system:hospital:edit']">
        <el-button
          type="primary"
          size="small"
          style="margin: 10px 15px"
          :loading="btnLoading"
          :disabled="btnLoading"
          @click="handleSave"
          >保存</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { setHospital, getHospital } from "@/api/hospital.js";
import uploadLogo from "./uploadLogo.vue";
export default {
  name: "systemHospital",
  components: { "upload-logo": uploadLogo },
  data() {
    return {
      hospital: {
        HospitalName: null,
        HospitalAliasName: null,
        HospitalCode: null,
        Country: null,
        City: null,
        Province: null,
        Address: null,
        Phone: null,
        IsCanConnectInternet: true,
        Id: null,
        TrialKeepCount: null,
        HospitalLogoPath: null,
      },
      hospitalFormRules: {
        HospitalName: [
          { required: true, message: "请输入医院名称", trigger: "blur" },
        ],
        HospitalCode: [
          { required: true, message: "请输入医院编码", trigger: "blur" },
        ],
        HospitalLogoPath: [
          { required: true, message: "请上传医院logo", trigger: "blur" },
        ],
        TrialKeepCount: [
          { required: true, message: "请输入未激活项目数", trigger: "blur" },
        ],
        // HospitalAliasName: [
        //   { required: true, message: "请输入医院别称", trigger: "blur" },
        // ],
        // Country: [{ required: true, message: "请输入国家", trigger: "blur" }],
        // City: [{ required: true, message: "请输入所在城市", trigger: "blur" }],
        // Province: [
        //   { required: true, message: "请输入所在省份", trigger: "blur" },
        // ],
        // Address: [
        //   { required: true, message: "请输入医院地址", trigger: "blur" },
        // ],
        // Phone: [
        //   { required: true, message: "请输入医院联系方式", trigger: "blur" },
        // ],
      },
      btnLoading: false,
    };
  },
  created() {
    this.getInfo();
  },
  computed: {
    disabled() {
      return !this.hasPermi(["system:hospital:edit"]);
    },
  },
  methods: {
    async handleSave() {
      if (this.btnLoading) return;
      try {
        let validate = await this.$refs.hospitalForm.validate();
        if (!validate) return;
        this.btnLoading = true;
        let res = await setHospital(this.hospital);
        this.btnLoading = false;
        if (res.IsSuccess) {
          this.$message.success(this.$t("common:message:savedSuccessfully"));
        }
      } catch (err) {
        console.log(err);
      }
    },
    // 获取医院信息
    async getInfo() {
      try {
        let res = await getHospital();
        if (res.IsSuccess) {
          Object.keys(this.hospital).forEach((key) => {
            this.hospital[key] = res.Result[key];
          });
        }
      } catch (err) {
        console.log(err);
      }
    },
  },
};
</script>
<style scoped>
::v-deep .is-error.my_new_pwd {
  margin-bottom: 40px;
}
.hospital {
  width: 100%;
  height: 100%;
  overflow-y: auto;
}
</style>

