<template>
  <div class="app-container">
    <el-card shadow="always" style="width: 55%; margin: 0 auto">
      <ReleaseForm
        v-model="form"
        :isAdd="false"
        :labelWidth="labelWidth"
        ref="releaseFormRef"
      />
      <div :style="{ marginLeft: labelWidth + 'px' }">
        <el-button type="primary" @click="submitForm(`3`)">保存草稿</el-button>
        <el-button type="primary" @click="submitForm(`0`)">提交审核</el-button>
      </div>
    </el-card>
  </div>
</template>
<script setup name="ReleaseAchievement">
import {
  insertAchievement,
  expertAchievementInfo,
  updateExpertAchievement,
} from "@/api/admin/expert/achievement";
import { ElMessage } from "element-plus";
import { reactive, toRefs } from "vue";
import { useRoute, useRouter } from "vue-router";
import ReleaseForm from "../components/ReleaseForm";
const labelWidth = 140;

const data = reactive({
  form: { mode: 1 },
});
const { form } = toRefs(data);
const route = useRoute();
const router = useRouter();
const releaseFormRef = ref();
const { id } = route.query;

if (id) {
  expertAchievementInfo({ id }).then((resp) => {
    form.value = resp.data;
    form.value.keywords =
      form.value.keyword?.split(",").filter((el) => el !== "") ?? [];
    form.value.customers =
      form.value.customer?.split(",").filter((el) => el !== "") ?? [];
  });
}

const submitForm = async (status) => {
  if (await releaseFormRef.value.validateForm()) {
    if (id) {
      await updateExpertAchievement({ ...form.value, status });
      ElMessage.success("修改成果成功");
      router.back();
    } else {
      await insertAchievement({ ...form.value, status });
      ElMessage.success("新增成果成功");
      router.back();
    }
  } else {
    console.log("校验未通过");
  }
};
</script>
