<template>
  <div style="width: 600px">
    <block-header title="创建百灵美选结清减免单" />
    <el-form
      size="small"
      label-suffix=":"
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="120px"
      class="demo-ruleForm"
    >
      <el-form-item label="借款编号" prop="loanNo">
        <el-col :span="20">
          <el-input
            v-model="ruleForm.loanNo"
            placeholder="请正确填写借款编号"
          ></el-input>
        </el-col>
        <el-button type="primary" @click="settlement">结清试算</el-button>
      </el-form-item>
      <el-form-item v-if="settleData.principleAmt">
        <div class="tableBox">
          <div class="boxItem">
            <p class="Itemleft">应还本金</p>
            <p>{{ settleData.principleAmt }}</p>
          </div>
          <div class="boxItem">
            <p class="Itemleft">应还利息</p>
            <p>{{ settleData.interestAmt }}</p>
          </div>
          <div class="boxItem">
            <p class="Itemleft">应还服务费</p>
            <p>{{ settleData.serviceAmt }}</p>
          </div>
          <div class="boxItem">
            <p class="Itemleft">应还融担费</p>
            <p>{{ settleData.burdenAmt }}</p>
          </div>
          <div class="boxItem">
            <p class="Itemleft">应还违约金</p>
            <p>{{ settleData.breakAmt }}</p>
          </div>
          <div class="boxItem">
            <p class="Itemleft">应还款总额</p>
            <p>{{ settleData.settleAmt }}</p>
          </div>
          <div class="boxItem">
            <p class="Itemleft">退款金额</p>
            <p>{{ settleData.refundAmt }}</p>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="减免金额" prop="derateAmt">
        <el-col :span="20">
          <el-input
            type="number"
            v-model="ruleForm.derateAmt"
            placeholder="请输入减免金额"
            @blur="submitAmt"
          ></el-input>
        </el-col>
      </el-form-item>
      <el-form-item v-if="settleData.principleAmt">
        <div class="tableBox">
          <div class="boxItem">
            <p class="Itemleft">减免违约金</p>
            <p>{{ derateData.derateBreak }}</p>
          </div>
          <div class="boxItem">
            <p class="Itemleft">减免服务费</p>
            <p>{{ derateData.derateFu }}</p>
          </div>
          <div class="boxItem">
            <p class="Itemleft">减免融担费</p>
            <p>{{ derateData.derateBurden }}</p>
          </div>
          <div class="boxItem">
            <p class="Itemleft">减免利息</p>
            <p>{{ derateData.derateXi }}</p>
          </div>
          <div class="boxItem">
            <p class="Itemleft">减免本金</p>
            <p>{{ derateData.derateBen }}</p>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="描述信息" prop="applyDesc">
        <el-col :span="20">
          <el-input
            v-model="ruleForm.applyDesc"
            placeholder="请输入减免描述信息"
          ></el-input>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" @click="createDerate('ruleForm')"
          >确 定</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "DerateCreate",

  props: {},
  data() {
    return {
      ruleForm: {
        loanNo: "",
        derateAmt: "",
        applyUser: "",
        applyDesc: "",
      },
      rules: {
        loanNo: [
          { required: true, message: "请正确填写借款编号", trigger: "blur" },
        ],
        derateAmt: [
          { required: true, message: "请输入减免金额", trigger: "blur" },
        ],
        applyDesc: [
          { required: true, message: "请输入减免描述信息", trigger: "blur" },
        ],
      },
      loginName: "",
      settleData: {},
      derateData: {},
    };
  },
  mounted() {
    this.loginName = localStorage.getItem("loginName");
  },

  methods: {
    //结清试算
    settlement() {
      this.$$get(`/back/countSettle`, {
        loanNo: this.ruleForm.loanNo,
      })
        .then((res) => {
          if (res.data.code != 200) {
            this.$message.error(res.data.message);
            return;
          }
          this.$message({ message: "提交成功!", type: "success" });
          console.log(res.data.result, "结果");
          this.settleData = res.data.result;
          //   setTimeout(() => {
          //     window.location.reload();
          //   }, 1500);
        })
        .catch((err) => {
          this.$message.error("试算失败！！！");
        });
    },
    //减免金额
    submitAmt() {
      console.log(this.ruleForm.derateAmt, "000000");
      this.$$post(`/loan/amountSettle`, {
        loanNo: this.ruleForm.loanNo,
        derateAmt: this.ruleForm.derateAmt,
      })
        .then((res) => {
          if (res.data.code != 200) {
            this.$message.error(res.data.message);
            return;
          }
          this.$message({ message: "提交成功!", type: "success" });
          console.log(res.data.result, "试算结果");
          this.derateData = res.data.result;
        })
        .catch((err) => {
          this.$message.error("试算失败！！！");
        });
    },
    /** 提交点击 */
    createDerate() {
        const {derateBen,derateBreak,derateBurden,derateFu,derateXi}=this.derateData
      this.$refs["ruleForm"].validate((valid) => {
        if (!valid) {
          return false;
        }
        this.$confirm("操作将创建减免单, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            this.$$post(`/loan/createLrdDerate`, {
              loanNo: this.ruleForm.loanNo,
              derateAmt: this.ruleForm.derateAmt,
              applyDesc: this.ruleForm.applyDesc,
              applyUser: this.loginName,
              derateBen,
              derateBreak,
              derateBurden,derateFu,derateXi
            })
              .then((res) => {
                if (res.data.code != 200) {
                  this.$message.error(res.data.message);
                  return;
                }
                this.$message({ message: "提交成功!", type: "success" });
                setTimeout(() => {
                  window.location.reload();
                }, 1500);
              })
              .catch((err) => {
                this.$message.error("提交减免单失败！！！");
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消提交",
            });
          });
      });
    },
  },
};
</script>

<style scoped>
.tableBox {
  width: 400px;
  border: 1px solid #eee;
  /* padding: 20px; */
}
.tableBox .boxItem {
  border-bottom: 1px solid #eee;
  display: flex;
}
.tableBox .boxItem:last-child {
  border: none;
}
.tableBox .boxItem p {
  padding: 5px 10px;
}
.tableBox .Itemleft {
  border-right: 1px solid #eee;
  width: 45%;
}
</style>
