<template>
  <div id="detail">
    <el-row>
      <el-col :span="24" class="style">
        <el-col :span="24" class="top">
          <NavBar v-show="navShow" :title="title" :isleftarrow="isleftarrow"> </NavBar>
        </el-col>
        <el-col :span="24" class="main">
          <release :form="form" @onSubmitDraft="onSubmitDraft" @onSubmit="onSubmit"></release>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import NavBar from '@/layout/common/topInfo.vue';
import release from '@/layout/myProduct/release.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: mapMarketproduct } = createNamespacedHelpers('marketproduct');
export default {
  name: 'detail',
  props: {},
  components: {
    NavBar,
    release, //产品发布
  },
  data: () => ({
    // 头部标题
    title: '',
    // meta为true
    isleftarrow: '',
    // 返回
    navShow: true,
    // 发布产品
    form: {},
  }),
  created() {
    this.searchInfo();
  },
  computed: {
    ...mapState(['user']),
    id() {
      return this.$route.query.id;
    },
  },
  methods: {
    ...mapMarketproduct({ productFetch: 'fetch', productCreate: 'create', productUpdate: 'update' }),
    // 详情
    async searchInfo() {
      if (this.id) {
        let res = await this.productFetch(this.id);
        if (this.$checkRes(res)) {
          this.$set(this, `form`, res.data);
        }
      }
    },
    // 保存草稿
    async onSubmitDraft({ data }) {
      if (this.id) {
        let res = await this.productUpdate(data);
        if (this.$checkRes(res)) {
          this.$message({
            message: '草稿修改成功',
            type: 'success',
          });
          this.$router.push({ path: '/userCenter/myProduct/index' });
        } else {
          this.$message.error('草稿修改失败');
        }
      } else {
        data.status = '3';
        data.userid = this.user.uid;
        let res = await this.productCreate(data);
        if (this.$checkRes(res)) {
          this.$message({
            message: '草稿创建成功',
            type: 'success',
          });
          this.$router.push({ path: '/userCenter/myProduct/index' });
        } else {
          this.$message.error('草稿创建失败');
        }
      }
    },
    // 信息发布
    async onSubmit({ data }) {
      if (this.id) {
        data.status = '0';
        let res = await this.productUpdate(data);
        if (this.$checkRes(res)) {
          this.$message({
            message: '信息发布成功',
            type: 'success',
          });
          this.$router.push({ path: '/userCenter/myProduct/index' });
        } else {
          this.$message.error('信息发布失败');
        }
      } else {
        data.status = '0';
        data.userid = this.user.uid;
        let res = await this.productCreate(data);
        if (this.$checkRes(res)) {
          this.$message({
            message: '信息发布成功',
            type: 'success',
          });
          this.$router.push({ path: '/userCenter/myProduct/index' });
        } else {
          this.$message.error('信息发布失败');
        }
      }
    },
  },
  mounted() {
    this.title = this.$route.meta.title;
    this.isleftarrow = this.$route.meta.isleftarrow;
  },
};
</script>

<style lang="less" scoped>
.style {
  width: 100%;
  min-height: 667px;
  position: relative;
  background-color: #f9fafc;
}
.top {
  height: 46px;
  overflow: hidden;
  position: relative;
  z-index: 999;
}
.main {
  min-height: 570px;
}
.foot {
  position: absolute;
  bottom: 0;
}
</style>
