<template>
  <div class="app-container">
    <el-card shadow="always" style="width: 55%; margin: 0 auto">
      <p>
        <b>{{
          t("admin.form.data", {
            type: t("admin.common.company"),
          })
        }}</b>
      </p>
      <EnterpriseForm
        ref="enterpriseFormRef"
        v-model="enterpriseInfoForm"
        :isAdd="false"
        :labelWidth="labelWidth"
      />
      <div :style="{ marginLeft: labelWidth + 'px' }">
        <el-button type="primary" @click="submitEnterpriseForm"
          >{{ t("admin.common.submit") }}
        </el-button>
      </div>
    </el-card>
  </div>
</template>
<script name="BasicInfo" setup>
import { getInfo } from "@/api/login";
import { updateEnterprise } from "@/api/admin/enterprise";
import EnterpriseForm from "@/views/components/EnterpriseForm";
import { ElMessage } from "element-plus";
import { reactive, ref, toRefs } from "vue";
import { useI18n } from "vue-i18n";

const { t } = useI18n();
const data = reactive({
  PersonalInfoForm: {}, // 个人基本资料
  enterpriseInfoForm: {},
});

const { PersonalInfoForm, enterpriseInfoForm } = toRefs(data);

const labelWidth = 140;

// 获取基础信息用于回显
const getBasicInfo = async () => {
  const { data } = await getInfo();
  PersonalInfoForm.value = data.user ?? {};
  enterpriseInfoForm.value = data.enterprise ?? {};
};

const enterpriseFormRef = ref(null);

const submitEnterpriseForm = async (status) => {
  const enterpriseFormValid = await enterpriseFormRef.value.validateForm();
  if (enterpriseFormValid) {
    await updateEnterprise(enterpriseInfoForm.value);
    ElMessage.success(t("tips.updateEnterpriseInformationSuccess"));
    const { data } = await getInfo();
    enterpriseInfoForm.value = data.enterprise ?? {};
  } else {
    console.log("validation failed");
  }
};

getBasicInfo();
</script>
