<template>
	<view class="total-page page_env-160 page-box sys-list-background-color task-bg">
		<Nav title="完善贷后" :genre="1" is_fixed></Nav>
		<view class="m-20">
			<view class="sys-background-fff p-lr20 p-t20 r-30">
				<view class="size-28 sys-weight-600">贷款信息</view>
				<en-select label="贷款银行" v-model="postLoanData.bank_id" :local-data="bankData"
					placeholder="请选择贷款银行"></en-select>
				<en-input label="贷款金额" v-model="postLoanData.thousand_money" type="digit" placeholder="请输入贷款金额" rightText="万元"></en-input>
				<en-input label="贷款期数" type="number" v-model="postLoanData.refund_num" placeholder="请输入贷款期数"></en-input>
				<en-input label="年化利率" :is_border="false" v-model="postLoanData.interest_rate" type="digit" placeholder="请输入年化利率"
					rightText="%"></en-input>
			</view>
			<view class="sys-background-fff m-t20 p-lr20 p-t20 r-30">
				<view class="size-28 sys-weight-600">贷款信息</view>
				<en-select label="还款方式" v-model="postLoanData.repayment_type" :local-data="repaymentTypeData"
                   placeholder="请选择还款方式"></en-select>
				<en-date label="还款日期" v-model="postLoanData.repayment_date" type="date" placeholder="请输入还款日期"></en-date>
				<en-input label="还款金额" :is_border="false" v-model="postLoanData.repayment_money"  placeholder="请输入还款金额"
					rightText="元"></en-input>
			</view>
		</view>
		<EnButton text="保存" @onSubmit="setPostLoan"></EnButton>
	</view>
</template>

<script>
	import EnInput from "@/components/en-from/en-input/en-input.vue";
	import EnSelect from "@/components/en-from/en-select/en-select.vue";
  import tools from "@/service/tools";
  import {getTaskOptions, setPostLoan} from "@/api/task";
  import EnDate from "@/components/en-from/en-date/en-date.vue";

	export default {
		components: {
      EnDate,
			EnInput,
			EnSelect
		},
		data() {
			return {
				number: '',
        businessId: '',
        postLoanData:{
          'bank_id':'',
          'loans_money':'',
          'thousand_money':'',
          'interest_rate':'',
          'repayment_money':'',
          'refund_num':'',
          'repayment_type':'1',
          'repayment_date':'',
        },
        bankData:[],
        repaymentTypeData:[],
			}
		},
    watch:{
      'postLoanData.thousand_money':function (){
        if(this.postLoanData.thousand_money===''){
          this.postLoanData.loans_money=''
        }else {
          this.postLoanData.loans_money=this.postLoanData.thousand_money*10000
        }
      },
      'postLoanData.loans_money':function (){
        this.getRepaymentMoney()
      },
      'postLoanData.interest_rate':function (){
        this.getRepaymentMoney()
      },
      'postLoanData.refund_num':function (){
        this.getRepaymentMoney()
      },
      'postLoanData.repayment_type':function (){
        this.getRepaymentMoney()
      },
    },
    onLoad(data) {
      if (data.id === undefined) {
        tools.leftClick()
      } else {
        this.businessId = data.id
      }
      this.getTaskOptions()
    },
		methods: {
      getRepaymentMoney(){
        console.log(this.postLoanData)
        if(this.postLoanData.repayment_type==='1'){
          this.postLoanData.repayment_money=tools.getAverageCapitalPlusInterest(this.postLoanData.loans_money,this.postLoanData.refund_num,this.postLoanData.interest_rate)
        }else if(this.postLoanData.repayment_type==='2'){
          this.postLoanData.repayment_money=tools.getAverageCapital(this.postLoanData.loans_money,this.postLoanData.refund_num,this.postLoanData.interest_rate)
        }else {
          this.postLoanData.repayment_money=tools.getInterestFirst(this.postLoanData.loans_money,this.postLoanData.interest_rate)
        }
        console.log(' this.postLoanData.repayment_money', this.postLoanData.repayment_money)
      },
      async getTaskOptions() {
        const res = await getTaskOptions({'type':6})
        if (res.code === 1) {
          this.bankData = res.data.bankData
          this.bankData.map((val) => {
            val.text = val.name;
          });
          this.repaymentTypeData = res.data.repaymentTypeData
          this.repaymentTypeData.map((val) => {
            val.text = val.name;
          });
        }
      },
      setPostLoan(){
        setPostLoan({'businessId':this.businessId,'postLoan':this.postLoanData}).then((res)=>{
          if(res.code===1){
            tools.success(res.msg)
            uni.$emit('newTaskList')
            uni.$emit('newTaskInfo')
            setTimeout(()=>{
             tools.leftClick()
            },1500)
          }else {
            tools.error(res.msg)
          }
        })
      },
			onSave() {}
		}
	}
</script>

<style>

</style>