<!-- banner管理 -->
<template>
  <div class="detail_container">
    <bg-breadcrumb></bg-breadcrumb>
    <div class="main_container">
      <div class="content_top">
        <banner-form ref="bannerFormRef" @action="submitFormData"></banner-form>
      </div>
      <div class="content_bottom">
        <div>
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="submit">保存</el-button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { reactive, toRefs, ref, onBeforeMount, nextTick } from "vue";
import axios from "../../../../../request/http.js";
import { ElMessage } from "element-plus";
import { useRoute, useRouter } from "vue-router";
import bgBreadcrumb from "@/components/bg-breadcrumb.vue";
import bannerForm from "./banner-form.vue";
const router = useRouter();
const route = useRoute();
const bannerFormRef = ref(null);
const state = reactive({
  formData: {
    banner_name: "",
    link_address: "",
    banner_image: [],
    state: 1,
  },
});

const getDetail = (id) => {
  axios
    .get(`/apaas/system/v5/banner/${id}`)
    .then((res) => {
      if (res.data.code == 200) {
        const data = res.data.data;
        bannerFormRef.value.setForm({
          banner_name: data.banner_name,
          link_address: data.link_address,
          banner_image: data.banner_image ? [{ url: data.banner_image }] : [],
          state: data.state,
        });
      } else {
        ElMessage.error(res.data.data);
      }
    })
    .catch((err) => {
      console.log(err);
    });
};

const submit = () => {
  bannerFormRef.value.submitForm();
};

const submitFormData = (data) => {
  if (data) {
    Object.assign(state.formData, data);
    console.log(state.formData);
    if (route.query.id) {
      // 编辑
      let params = {
        ...state.formData,
        id: +route.query.id,
      };
      params.banner_image = params.banner_image && params.banner_image.length > 0 ? params.banner_image[0].url : "";
      axios.put(`/apaas/system/v5/banner`, params).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success(res.data.msg);
          router.go(-1);
        } else {
          ElMessage.error(res.data.data);
        }
      });
    } else {
      let params = {
        ...state.formData,
      };
      params.banner_image = params.banner_image && params.banner_image.length > 0 ? params.banner_image[0].url : "";
      axios.post(`/apaas/system/v5/banner`, params).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success(res.data.msg);
          router.go(-1);
        } else {
          ElMessage.error(res.data.data);
        }
      });
    }
  }
};

const cancel = () => {
  router.go(-1);
};

onBeforeMount(() => {
  if (route.query.id) {
    getDetail(route.query.id);
  }
});

const {} = toRefs(state);
</script>

<style lang="scss" scoped>
.detail_container {
  width: 100%;
  padding: 0 24px;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: stretch;
  .main_container {
    flex-grow: 1;
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: stretch;
    background-color: #fff;
    box-shadow: 0px 1px 4px 0px rgba(0, 7, 101, 0.15);
    border-radius: 6px;
    margin: 0 0 16px;
    position: relative;
    height: calc(100% - 62px);
    // padding: 24px;
    .content_top {
      flex: 1;
      display: flex;
      flex-direction: column;
      overflow: auto;
      padding: 24px;
    }
    .content_bottom {
      height: 68px;
      line-height: 68px;
      text-align: right;
      padding: 0 20px;
      border-top: 1px solid #e6e9ef;
      .el-button {
        width: 92px;
      }
    }
  }
}
</style>
