<template>
  <div id="financeClaimDetail">
    <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">
            <financeClaimsForm
              :ruleForm="ruleForm"
              :dbList="dbList"
              :hkList="hkList"
              :lxList="lxList"
              :sxList="sxList"
              @submitForm="submitForm"
              @resetForm="resetForm"
            ></financeClaimsForm>
          </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 financeClaimsForm from '@/layout/financeclaims/financeClaimsForm.vue';
import { createNamespacedHelpers, mapGetters, mapState } from 'vuex';
const { mapActions: financeclaims } = createNamespacedHelpers('financeclaim');
export default {
  name: 'financeClaimDetail',
  props: {},
  components: {
    topInfo, //头部导航
    detailTop, //头部返回
    financeClaimsForm, //添加债权产品
  },
  data: () => ({
    display: 'none',
    topTitle: '债权产品信息',
    ruleForm: {
      news: [
        {
          news_name: '',
          news_info: '',
        },
      ],
      cattribute: [],
    },
    dbList: [
      { name: '保证', code: '2201' },
      { name: '抵押', code: '2202' },
      { name: '质押', code: '2203' },
    ],
    hkList: [
      { name: '等额本息', code: '2301' },
      { name: '等额本金', code: '2302' },
      { name: '一次性利随本清', code: '2303' },
    ],
    lxList: [
      { name: '抗疫', code: '2401' },
      { name: '科技', code: '2402' },
    ],
    sxList: [
      { name: '期限长', code: '2501' },
      { name: '速度快', code: '2502' },
      { name: '利率低', code: '2503' },
      { name: '通过率高', code: '2504' },
      { name: '额度高', code: '2505' },
    ],
  }),
  created() {
    this.searchInfo();
  },
  computed: {
    ...mapState(['user']),
    id() {
      return this.$route.query.id;
    },
  },
  methods: {
    ...financeclaims(['fclaimsList', 'orUpdate', 'dupdate', 'create', 'fetch', 'update']),
    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.finid = 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.uid = this.user.userid;
        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/financeClaimIndex' });
    },
    // 返回
    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>
