<template>
  <div id="form-1">
    <el-row>
      <el-col :span="24" class="main animate__animated animate__backInRight">
        <el-col class="top-btn">
          <el-button type="primary" size="mini" @click="toBack()">返回</el-button>
        </el-col>
        <el-col :span="24" class="one">
          <data-form :span="24" :fields="fields" v-model="form" :rules="rules" @save="onSubmit">
            <template #status>
              <el-option v-for="i in statusList" :key="i.model" :label="i.label" :value="i.value"></el-option>
            </template>
            <template #show_index>
              <el-option v-for="i in statusList" :key="i.model" :label="i.label" :value="i.value"></el-option>
            </template>
          </data-form>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
const _ = require('lodash');
const moment = require('moment');
import { mapState, mapGetters, createNamespacedHelpers } from 'vuex';
const { mapActions } = createNamespacedHelpers('actTags');
const { mapActions: dictData } = createNamespacedHelpers('dictData');

export default {
  name: 'form-1',
  props: {},
  components: {},
  data: function () {
    return {
      form: {},
      rules: {
        label: [{ required: true, message: '请输入活动标签', trigger: 'blur' }],
        value: [{ required: false, message: '请输入编码', trigger: 'blur' }],
        status: [{ required: true, message: '请选择状态', trigger: 'change' }],
      },
      fields: [
        { label: '活动标签', model: 'label' },
        { label: '编码', model: 'value' },
        { label: '排序', model: 'sort', type: 'number' },
        { label: '状态', model: 'status', type: 'select' },
        { label: '标签图片', model: 'file', type: 'upload', url: '/files/point/actTags/upload' },
        { label: '是否在首页展示', model: 'show_index', type: 'select' },
      ],
      // 类型
      typeList: [],
      // 是否使用
      statusList: [],
    };
  },
  async created() {
    await this.searchOther();
    await this.search();
  },
  methods: {
    ...dictData({ dictQuery: 'query' }),
    ...mapActions(['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);
        }
      } else {
        this.$set(this, `form`, { status: '0', show_index: '0' });
      }
    },
    // 提交
    async onSubmit({ data }) {
      let res;
      if (data.id) res = await this.update(data);
      else res = await this.create(data);
      if (this.$checkRes(res)) {
        this.$message({ type: `success`, message: `维护信息成功` });
        this.toBack();
      }
    },
    // 查询其他信息
    async searchOther() {
      let res;
      // 是否使用
      res = await this.dictQuery({ code: 'status' });
      if (this.$checkRes(res)) {
        this.$set(this, `statusList`, res.data);
      }
    },
    // 返回
    toBack() {
      window.history.go('-1');
    },
  },
  computed: {
    id() {
      return this.$route.query.id;
    },
    status() {
      return this.$route.query.status;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
  watch: {
    test: {
      deep: true,
      immediate: true,
      handler(val) {},
    },
  },
};
</script>

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