<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">
          <tPolicyDeclarationForm :ruleForm="ruleForm" @submitForm="submitForm" @resetForm="resetForm" :id="id"  :loading="loading" :subject_classification_list="subject_classification_list" :subject_headings_list="subject_headings_list" :company_description_list="company_description_list"></tPolicyDeclarationForm>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

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

  export default {
    name: 'detail',
    props: {},
    components: {
      detailTopInfo, //头部导航
      tPolicyDeclarationForm,
    },
    data: () => ({
      display: 'block',
      topTitle: '权威发布管理',
      ruleForm: {},
      loading: true,
      subject_classification_list: [],
      subject_headings_list: [],
      company_description_list: [],
    }),
    created() {
      this.select();
      this.selectDict(['subject_classification','subject_headings','company_description']);
    },
    computed: {
      id() {
        return this.$route.query.id;
      },
      ...mapState(['user']),
    },
    methods: {
      ...tPolicyDeclaration(['create', 'delete', 'update', 'fetch', 'query','state']),
      ...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) {
          if(data.current_state !== '0'){
            this.$message.error('只有"未审核"的政策允许修改');
            return ;
          }
          res = await this.update(data);
          if (res.errcode === 0) {
            this.$message({
              message: '信息修改成功',
              type: 'success',
            });
          }
        } else {
          data.current_approval_id = this.user.uid;
          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: '/tPolicyDeclaration/index' });
      },
    },
  };
</script>

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