<template>
  <div class="app-container">
    <el-card shadow="always" style="width: 55%; margin: 0 auto">
      <EnterpriseForm
        v-model="form"
        :isAdd="false"
        :labelWidth="labelWidth"
        ref="enterpriseFormRef"
      />
      <div :style="{ marginLeft: labelWidth + 'px' }">
        <el-button @click="$router.go(-1)">{{
          t("admin.common.cancel")
        }}</el-button>
        <el-button type="primary" @click="submitForm">{{
          t("admin.common.submit")
        }}</el-button>
      </div>
    </el-card>
  </div>
</template>
<script setup>
import { insertEnterprise } from "@/api/identity/index";
import { ElMessage } from "element-plus";
import { reactive, ref, toRefs } from "vue";
import { useRouter } from "vue-router";
import EnterpriseForm from "../components/EnterpriseForm/index.vue";
import { useI18n } from "vue-i18n";
const { t } = useI18n();
const router = useRouter();
const labelWidth = 140;

const data = reactive({ form: {} });
const { form } = toRefs(data);
const enterpriseFormRef = ref();

const submitForm = async (status) => {
  const valid = await enterpriseFormRef.value.validateForm();
  if (valid) {
    insertEnterprise(form.value).then(() => {
      ElMessage.success(t("admin.common.addSuccess"));
      router.go(-1);
    });
  } else {
    console.log("validation failed");
  }
};
</script>
