<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="12" class="one">
          <data-form :span="24" :fields="fields" v-model="form" :rules="rules" @save="onSubmit">
            <template #type>
              <el-option v-for="i in typeList" :key="i.model" :label="i.label" :value="i.value"></el-option>
            </template>
            <template #status>
              <el-option v-for="i in statusList" :key="i.model" :label="i.label" :value="i.value"></el-option>
            </template>
            <template #content>
              <editor v-model="form.content" />
            </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('banner');
const { mapActions: dictData } = createNamespacedHelpers('dictData');

export default {
  name: 'form-1',
  props: {},
  components: {
    editor: () => import('@/components/editor.vue'),
  },
  data: function () {
    return {
      form: {},
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        type: [{ required: false, message: '请选择类型', trigger: 'change' }],
        to: [{ required: false, message: '请输入跳转至', trigger: 'blur' }],
        status: [{ required: true, message: '请选择是否正在使用', trigger: 'change' }],
        url: [{ required: true, message: '请选择图片', trigger: 'change' }],
      },
      fields: [
        { label: '名称', model: 'name' },
        { label: '类型', model: 'type', type: 'select' },
        { label: '跳转至', model: 'to' },
        { label: '是否正在使用', model: 'status', type: 'select' },
        {
          label: '图片',
          model: 'url',
          type: 'upload',
          url: '/files/point/banner/upload',
        },
        { label: '内容', model: 'content', type: '' },
      ],
      // 类型
      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`, {});
        this.$set(this.form, `status`, this.status);
      }
    },
    // 提交
    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: 'banner_type' });
      if (this.$checkRes(res)) {
        this.$set(this, `typeList`, res.data);
      }
      // 是否使用
      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>
