<template>
  <div id="manageMoneyDetail">
    <el-row>
      <el-col :span="24" class="debt">
        <el-col :span="24" class="top">
          <topInfo :topTitle="topTitle" :display="display"></topInfo>
        </el-col>
        <el-col :span="24" class="main">
          <el-col :span="24" class="back">
            <detailTop @goBack="goBack"></detailTop>
          </el-col>
          <el-col :span="24" class="info">
            <manageMoneyForm :ruleForm="ruleForm" @submitForm="submitForm" @resetForm="resetForm"></manageMoneyForm>
          </el-col>
        </el-col>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import topInfo from '@/layout/common/topInfo.vue';
import detailTop from '@/layout/common/detailTop.vue';
import manageMoneyForm from '@/layout/financeclaims/manageMoneyForm.vue';
import { createNamespacedHelpers, mapGetters, mapState } from 'vuex';
const { mapActions: managemoney } = createNamespacedHelpers('managemoney');
export default {
  name: 'manageMoneyDetail',
  props: {},
  components: {
    topInfo, //头部导航
    detailTop, //头部返回
    manageMoneyForm, //添加理财产品
  },
  data: () => ({
    display: 'none',
    topTitle: '理财产品信息',
    ruleForm: {},
  }),
  created() {
    this.searchInfo();
  },
  computed: {
    ...mapState(['user']),
    id() {
      return this.$route.query.id;
    },
  },
  methods: {
    ...managemoney(['create', 'fetch', 'update', 'orUpdate']),
    async searchInfo() {
      if (this.id) {
        const res = await this.fetch(this.id);
        this.$set(this, `ruleForm`, res.data);
      }
    },
    // 提交
    async submitForm({ data }) {
      let res;
      let befupdate;
      let finaldata;
      if (this.id) {
        var orupdate = {};
        orupdate.mmid = this.id;
        befupdate = await this.orUpdate(orupdate);
        if (befupdate.data.status == 'SUCCESS') {
          res = await this.update(data);
          if (res.errcode === 0) {
            this.$message({
              message: '信息修改成功',
              type: 'success',
            });
          }
        } else {
          this.$message({
            message: '该消息已发布不可修改',
            type: 'error',
          });
        }
      } else {
        finaldata = data;
        finaldata.jgid = this.user.userid;
        //     alert(JSON.stringify(finaldata));
        res = await this.create(finaldata);
        if (res.errcode === 0) {
          this.$message({
            message: '信息创建成功',
            type: 'success',
          });
        }
      }
      if (this.$checkRes(res)) this.resetForm();
      console.log(res.data);
    },
    // 取消
    resetForm() {
      this.$router.push({ path: '/financeclaims/manageMoneyIndex' });
    },
    // 返回
    goBack() {
      this.$router.go(-1);
    },
  },
};
</script>

<style lang="less" scoped>
.debt {
  padding: 20px;
}
.top {
  border-bottom: 1px solid #ccc;
}
.main {
  border-radius: 10px;
  margin: 20px 0 0 0;
  box-shadow: 0 0 3px #666;
}
.main .back {
  padding: 10px 0 10px 15px;
}
</style>
