<template>
  <div class="p-2">
    <el-card style="overflow-y: scroll;">
    <div :style=divStyle>
    <div>
      <h4 style="margin-left: 70px;">基本信息</h4>
    </div>
    <el-divider />
    <div style="width: 800px;margin-left: 100px;">
      <el-form ref="hospitalFormRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="医院名称" prop="hospitalName">
          <el-input v-model="form.hospitalName" placeholder="请输入" size="large"  :disabled="editBtn"/>
        </el-form-item>
        <el-form-item label="医院logo" prop="hospitalLogo">
          <imageUpload v-model="form.hospitalLogo" :limit="1" :isShowTip="false" :disabled="editBtn" />
        </el-form-item>
        <el-form-item label="医院等级" prop="hospitalLevel">
          <el-select v-model="form.hospitalLevel" placeholder="请选择" :disabled="editBtn" size="large">
                <el-option v-for="dict in hospital_level" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入" size="large" maxlength="11" :disabled="editBtn"/>
        </el-form-item>
        <el-form-item label="住院床位" prop="bedSpace">
          <el-input v-model="form.bedSpace" placeholder="请输入" size="large" :disabled="editBtn"/>
        </el-form-item>
        <el-form-item label="所属地区" prop="region">
          <el-input v-model="form.region" placeholder="请输入" size="large" :disabled="editBtn"/>
        </el-form-item>
        <el-form-item label="医院描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入" :rows="10" :disabled="editBtn" />
        </el-form-item>
      </el-form>
    </div>
  </div>
  </el-card>  
  <el-card shadow="always" class="card-footer">
    <div>
        <el-button type="primary" size="large" @click="submitForm" v-hasPermi="['system:hospital:add']" v-if="!editBtn">
          &nbsp;&nbsp;&nbsp;保存&nbsp;&nbsp;&nbsp;
        </el-button>
        <el-button type="primary" size="large" @click="editBtn=false" v-hasPermi="['system:hospital:add']" v-if="editBtn">
          &nbsp;&nbsp;&nbsp;编辑&nbsp;&nbsp;&nbsp;
        </el-button>
      </div>
  </el-card>
  </div>  
</template>

<script setup name="Hospital" lang="ts">
import {  addHospital, updateHospital,getHospitalData } from '@/api/system/hospital';
import {   HospitalQuery, HospitalForm } from '@/api/system/hospital/types';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { hospital_level } = toRefs<any>(proxy?.useDict('hospital_level'));
 

const editBtn = ref<boolean>(true);
const buttonLoading = ref(false);
const loading = ref(true); 
const divStyle = ref({
  height: '700px'
});
 


const hospitalFormRef = ref<ElFormInstance>();
 

const initFormData: HospitalForm = {
  hospitalId: undefined,
  hospitalName: undefined,
  hospitalLogo: undefined,
  hospitalLevel: undefined,
  phone: undefined,
  bedSpace: undefined,
  region: undefined,
  description: undefined,
  status: undefined,
}
const data = reactive<PageData<HospitalForm, HospitalQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    hospitalName: undefined,
    hospitalLogo: undefined,
    hospitalLevel: undefined,
    phone: undefined,
    bedSpace: undefined,
    region: undefined,
    description: undefined,
    status: undefined,
    params: {
    }
  },
  rules: {    
    hospitalName: [
      { required: true, message: "医院名称不能为空", trigger: "blur" }
    ],
  }
});

const {  form, rules } = toRefs(data);

/** 查询医院管理列表 */
const getList = async () => {
  loading.value = true;
  const res = await getHospitalData();  
  if(res.data){    
    form.value=res.data;  
  }  
}
 
 

/** 提交按钮 */
const submitForm = () => {
  hospitalFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {       
      buttonLoading.value = true;
      if (form.value.hospitalId) {
        await updateHospital(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addHospital(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");     
      await getList();
    }
  });
}
 

onMounted(() => {
  getList();
});
</script>

<style lang="scss" scoped>
.card-footer{  
    z-index: 99;
    bottom: 0px;
    left: calc(var(--left-menu-max-width) + var(--app-content-padding));
    right: 0px;
    height: 90px;
    display: flex;
    justify-content: center;
    align-items: center;     
}
</style>
