<template>
  <div id="detail">
    <el-row>
      <el-col :span="24">
        <el-col :span="24" class="top">
          <detailTopInfo :topTitle="topTitle" :display="display" @goBack="goBack"></detailTopInfo>
        </el-col>
        <el-col :span="24" class="main">
          <tPolicyInterpretationForm :ruleForm="ruleForm" @submitForm="submitForm" @resetForm="resetForm" :loading="loading" :subject_classification_list="subject_classification_list" :subject_headings_list="subject_headings_list"></tPolicyInterpretationForm>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
  import detailTopInfo from '@/layout/custom/detailTopInfo.vue';
  import tPolicyInterpretationForm from '@/layout/tPolicyInterpretation/tPolicyInterpretationForm.vue';
  import { createNamespacedHelpers, mapGetters } from 'vuex';
  const { mapActions: tPolicyInterpretation } = createNamespacedHelpers('tPolicyInterpretation');
  const { mapActions: dictionary } = createNamespacedHelpers('dictionary');

  export default {
    name: 'detail',
    props: {},
    components: {
      detailTopInfo, //头部导航
      tPolicyInterpretationForm,
    },
    data: () => ({
      display: 'block',
      topTitle: '政策解读管理',
      ruleForm: {},
      loading: true,
      subject_classification_list: [],
      subject_headings_list: [],
    }),
    created() {
      this.select();
      this.selectDict(['subject_classification','subject_headings']);
    },
    computed: {
      id() {
        return this.$route.query.id;
      },
    },
    methods: {
      ...tPolicyInterpretation(['create', 'delete', 'update', 'fetch', 'query']),
      ...dictionary({dictQuery:'query'}),
      // 返回
      goBack() {
        this.$router.go(-1);
      },
      async select() {
        this.$set(this, `loading`, false);
        if (this.id) {
          const res = await this.fetch(this.id);
          this.$set(this, `ruleForm`, res.data);
        }
        this.$set(this, `loading`, true);
      },
      filterDict(dict){
        return dict.filter((val) => {
          return val.name !== '不限';
        });
      },
      async selectDict(arr) {
        for (let value of arr) {
          let res = await this.dictQuery({skip:0,limit:100,type:value});
          this.$set(this, `${value}_list`, this.filterDict(res.data));
        }
      },
      // 提交
      async submitForm({ data }) {
        /*if(!data.image){
          this.$message.error('请上传图片');
          return ;
        }*/
        let res;
        if (this.id) {
          res = await this.update(data);
          if (res.errcode === 0) {
            this.$message({
              message: '信息修改成功',
              type: 'success',
            });
          }
        } else {
          res = await this.create(data);
          if (res.errcode === 0) {
            this.$message({
              message: '信息创建成功',
              type: 'success',
            });
          }
        }
        if (this.$checkRes(res)) this.resetForm();
      },
      // 取消
      resetForm() {
        this.$router.push({ path: '/tPolicyInterpretation/index' });
      },
    },
  };
</script>

<style lang="less" scoped>
  .top {
    height: 50px;
    margin: 0 0 10px 0;
  }
  .main {
    min-height: 765px;
    background: #ffffff;
    padding: 20px;
  }
</style>
