<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.expert") }) }}</b>
      </p>
      <ExpertForm
        ref="expertFormRef"
        v-model="expertForm"
        :isAdd="false"
        :labelWidth="labelWidth"
      />
      <div :style="{ marginLeft: labelWidth + 'px' }">
        <el-button type="primary" @click="submitExpertForm"
          >{{ t("admin.common.submit") }}
        </el-button>
      </div>
    </el-card>
  </div>
</template>
<script setup>
import { getInfo, updateExpert } from "@/api/admin/expert/account";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import { reactive } from "vue";
import { useI18n } from "vue-i18n";
import ExpertForm from "@/views/components/ExpertForm";

const { t } = useI18n();
const data = reactive({
  expertForm: {
    gender: 1,
  },
});

const { expertForm } = toRefs(data);

const router = useRouter();
const expertFormRef = ref();
const labelWidth = 140;

// 获取基础信息用于回显
const getBasicInfo = async () => {
  const { data } = await getInfo();
  if (data.casExpert) {
    if (data.casExpert.research)
      data.casExpert.researchs = data.casExpert.research?.split(",") ?? [];
    if (data.casExpert.area) data.casExpert.district = data.casExpert.area;
  }
  expertForm.value = data.casExpert ?? {};
};
getBasicInfo();

const form = reactive({
  // laboratory_id: undefined, // 所属实验室
  gender: 1,
});
ref();

async function submitExpertForm(status) {
  const valid = await expertFormRef.value.validateForm();
  if (valid) {
    expertForm.value.area = expertForm.value.district;
    updateExpert(expertForm.value).then((res) => {
      ElMessage.success(t("admin.common.editSuccess"));
      // router.back();
    });
  } else {
    console.log("validation failed");
  }
}
</script>
