<template>
  <div id="detail">
    <data-form :fields="fields" :data="data" @save="toSave" returns="/adminCenter/online" @filterReturn="filterReturn">
      <template #options="{ item }">
        <template v-if="item.model === 'column_name'">
          <el-option v-for="(i, index) in columnList" :key="`columns-name-${index}`" :label="i" :value="i"></el-option>
        </template>
        <template v-if="item.model === 'province'">
          <el-option v-for="(i, index) in provinceList" :key="`province-${index}`" :label="i.name" :value="i.code"></el-option>
        </template>
        <template v-if="item.model === 'city'">
          <el-option v-for="(i, index) in cityList" :key="`city-${index}`" :label="i.name" :value="i.code"></el-option>
        </template>
      </template>
      <template #radios="{ item }">
        <template v-if="item.model === 'status'">
          <el-radio v-for="(i, index) in statusList" :key="`status-${index}`" :label="i.value">{{ i.label }}</el-radio>
        </template>
      </template>
      <template #custom="{ item }">
        <template v-if="item.model === 'image'">
          <e-upload url="/files/cysci/online_image/upload" v-model="data[item.model]"></e-upload>
        </template>
      </template>
    </data-form>
  </div>
</template>

<script>
const _ = require('lodash');
const { onlineType, progressType } = require('@common/dict/index');
import { mapState, createNamespacedHelpers } from 'vuex';
const { mapActions: online } = createNamespacedHelpers('online');
const { mapActions: code } = createNamespacedHelpers('code');
export default {
  name: 'onlineDetail',
  props: {},
  components: {},
  data: function () {
    return {
      columnList: onlineType,
      statusList: progressType,
      data: {
        image: [],
        status: '0',
      },
      fields: [
        { label: '栏目名称', model: 'column_name', type: 'select' },
        { label: '标题', model: 'title' },
        { label: '封面', model: 'image', custom: true },
        { label: '举办方', model: 'organizers' },
        { label: '联系人', model: 'contacts' },
        { label: '联系电话', model: 'phone' },
        { label: '省', model: 'province', type: 'select', filterReturn: true },
        { label: '市', model: 'city', type: 'select' },
        { label: '举办开始时间', model: 'start_time', type: 'datetime' },
        { label: '举办结束时间', model: 'end_time', type: 'datetime' },
        { label: '信息简介', model: 'brief' },
        { label: '进行状态', model: 'status', type: 'radio' },
      ],
      xzqhList: [],
      provinceList: [],
      cityList: [],
    };
  },
  created() {
    this.getXzqh();
    if (this.id) this.search();
  },
  methods: {
    ...online(['fetch', 'create', 'update']),
    ...code(['xzqh']),
    async search() {
      const res = await this.fetch(this.id);
      if (this.$checkRes(res)) {
        this.$set(this, `data`, res.data);
      }
    },
    async toSave({ data }) {
      let dup = _.cloneDeep(data);
      let res;
      if (_.get(dup, 'id')) {
        res = await this.update(dup);
      } else {
        res = await this.create(dup);
      }
      if (this.$checkRes(res, '保存成功', '保存失败')) {
        if (!this.$dev_mode) this.$router.push('/adminCenter/news');
      }
    },
    async getXzqh(code) {
      if (!code) {
        const res = await this.xzqh();
        if (this.$checkRes(res)) {
          this.$set(this, `provinceList`, res.data);
        }
      } else {
        const res = await this.xzqh({ code });
        if (this.$checkRes(res)) {
          this.$set(this, `cityList`, res.data);
        }
      }
    },
    filterReturn({ data, model }) {
      if (model === 'province') this.getXzqh(data);
    },
  },
  computed: {
    ...mapState(['user', 'menuParams']),
    pageTitle() {
      return `${this.$route.meta.title}`;
    },
    id() {
      return this.$route.query.id;
    },
  },
  metaInfo() {
    return { title: this.$route.meta.title };
  },
};
</script>

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