<template>
  <div id="detail">
    <admin-frame :usePage="false" topType="2" :rightArrow="false" @back="back" :useNav="false">
      <template v-slot:info>
        <form-1 :form="form" @onSubmit="onSubmit"></form-1>
      </template>
    </admin-frame>
  </div>
</template>

<script>
import form1 from './parts/form-1.vue';
import adminFrame from '@frame/src/components/mobile-frame/mobile-main.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: answer_tea } = createNamespacedHelpers('answer_tea');
export default {
  name: 'detail',
  props: {},
  components: {
    adminFrame,
    form1,
  },
  data: function () {
    return {
      form: {},
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...answer_tea(['fetch', 'create', 'update']),
    async search() {
      if (this.id) {
        let res = await this.fetch(this.id);
        if (this.$checkRes(res)) {
          this.$set(this, `form`, res.data);
        }
      }
    },
    async onSubmit(data) {
      if (this.id) {
        let res = await this.update(data);
        if (this.$checkRes(res)) {
          this.$toast({ type: `success`, message: `操作成功` });
          this.back();
        }
      } else {
        let res = await this.create(data);
        if (this.$checkRes(res)) {
          this.$toast({ type: `success`, message: `操作成功` });
          this.back();
        }
      }
    },
    back() {
      this.$router.push({ path: `/account/answertea/index` });
    },
  },
  computed: {
    ...mapState(['user']),
    id() {
      return this.$route.query.id;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

<style lang="less" scoped></style>
