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

<script>
import detail1 from './parts/detail_1.vue';
import adminFrame from '@frame/src/components/mobile-frame/mobile-main.vue';
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: adminLogin } = createNamespacedHelpers('adminLogin');
export default {
  name: 'detail',
  props: {},
  components: {
    adminFrame,
    detail1,
  },
  data: function () {
    return {
      form: {},
      pidList: [],
    };
  },
  async created() {
    await this.search();
  },
  methods: {
    ...adminLogin(['query', 'fetch', 'update', 'create']),
    async search() {
      let res = await this.query({ role: '1', code: this.user.code });
      if (this.$checkRes(res)) {
        this.$set(this, `pidList`, res.data);
      }
      if (this.id) {
        res = await this.fetch(this.id);
        if (this.$checkRes(res)) {
          res.data.pidName = this.pidList.find((i) => i.id == res.data.pid).name;
          this.$set(this, `form`, res.data);
        }
      }
    },
    // 查看详情
    async onSubmit(data) {
      data.role = '2';
      if (data.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/mechanism/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>
