<template>
  <div id="index">
    <data-form :span="12" :fields="fields" :rules="rules" v-model="form" labelWidth="150px" @save="toSave"> </data-form>
  </div>
</template>

<script>
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: config } = createNamespacedHelpers('config');
export default {
  name: 'index',
  props: {},
  components: {},
  data: function () {
    return {
      fields: [
        { label: '系统名称', model: 'title' },
        { label: 'logo', model: 'logo', type: 'upload', limit: 1, url: '/files/point/config/upload' },
        { label: '购物赠送积分', model: 'buyPoint', type: 'number' },
        { label: '积分计划', model: 'pointPlan' },
        { label: '分享图片', model: 'share', type: 'upload', limit: 1, url: '/files/point/config/upload' },
      ],
      rules: {},
      form: {},
    };
  },
  computed: {
    ...mapState(['user']),
  },
  created() {
    this.search();
  },
  methods: {
    ...config(['query', 'fetch', 'update']),
    //执行查询
    async search() {
      const res = await this.fetch();
      if (this.$checkRes(res)) {
        this.$set(this, `form`, res.data);
        if (this.form.config) {
          this.form = {
            ...this.form,
            logo: this.form.config.logo,
            buyPoint: this.form.config.buyPoint,
            pointPlan: this.form.config.pointPlan,
            share: this.form.config.share,
          };
        }
      }
    },
    async toSave({ data }) {
      let config = { logo: data.logo, buyPoint: data.buyPoint, pointPlan: data.pointPlan, share: data.share };
      data.config = config;
      const res = await this.update(data);
      this.$checkRes(res, '操作成功', '操作失败');
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

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