<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 #to_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>
          </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('indexModule');
const { mapActions: dictData } = createNamespacedHelpers('dictData');
export default {
  name: 'form-1',
  props: {},
  components: {},
  data: function () {
    return {
      form: {},
      rules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
        to_type: [{ required: false, message: '请选择类型', trigger: 'change' }],
        to: [{ required: false, message: '请输入跳转至', trigger: 'blur' }],
        status: [{ required: true, message: '请选择是否正在使用', trigger: 'change' }],
        url: [{ required: true, message: '请选择图标', trigger: 'change' }],
        sort: [{ required: true, message: '请输入排序', trigger: 'blur' }],
      },
      fields: [
        { label: '名称', model: 'name' },
        { label: '跳转类型', model: 'to_type', type: 'select' },
        { label: '跳转至', model: 'to' },
        { label: '是否正在使用', model: 'status', type: 'select' },
        { label: '图标', model: 'url', type: 'upload', url: '/files/point/indexModule/upload' },
        { label: '排序', model: 'sort', type: 'number' },
      ],
      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: 'to_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>
