<template>
  <el-dialog
    title="退款"
    :visible.sync="dialogVisible"
    width="500px"
    append-to-body
    @open="handleOpen"
  >
    <el-form :model="form" :rules="rules" ref="form" label-width="6em" v-loading="loading" size="small">
      <el-row>
        <form-col :span="24" label="骑行退款" prop="ridingRefund" :tip="`最多可退 ${detail.actualReceivedRidingFee || 0} 元`">
          <el-input-number 
            v-model="form.ridingRefund" 
            :max="detail.actualReceivedRidingFee || 0" 
            :min="0" 
            :precision="2" 
            style="width: 100%;" 
            controls-position="right"
          />  
        </form-col>
        <form-col :span="24" label="调度退款" prop="dispatchRefund" :tip="`最多可退 ${detail.actualReceivedDispatchFee || 0} 元`">
          <el-input-number 
            v-model="form.dispatchRefund" 
            :min="0" 
            :max="detail.actualReceivedDispatchFee || 0"
            :precision="2" 
            style="width: 100%;" 
            controls-position="right"
          />
        </form-col>
        <form-col :span="24" label="管理退款" prop="manageRefund" :tip="`最多可退 ${detail.actualReceivedManageFee || 0} 元`">
          <el-input-number 
            v-model="form.manageRefund" 
            :min="0" 
            :max="detail.actualReceivedManageFee || 0"
            :precision="2" 
            style="width: 100%;" 
            controls-position="right"
          />
        </form-col>
        <form-col :span="24" label="车损退款" prop="deductionRefund" :tip="`最多可退 ${detail.actualReceivedDeductionFee || 0} 元`">
          <el-input-number 
            v-model="form.deductionRefund" 
            :min="0" 
            :max="detail.actualReceivedDeductionFee || 0"
            :precision="2" 
            style="width: 100%;" 
            controls-position="right"
          />
        </form-col>
        <form-col :span="24" label="总计退款">
          {{ totalRefundAmount  | fix2 | dv }} 元
        </form-col>
        <form-col :span="24" label="退款原因" prop="reason">
          <el-input v-model="form.reason" type="textarea" placeholder="请输入退款原因" show-word-limit maxlength="200" />
        </form-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSubmit" :loading="submitLoading" icon="el-icon-wallet">确认退款</el-button>
      <el-button @click="dialogVisible = false" icon="el-icon-close">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getOrder, refundOrder } from '@/api/bst/order';
import { isEmpty } from '@/utils';

export default {
  props: {
    id: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      detail: {},
      form: {},
      loading: false,
      submitLoading: false,
      rules: {
        ridingRefund: [
          { required: true, message: '请输入骑行费退款金额', trigger: 'blur' },
        ],
        dispatchRefund: [
          { required: true, message: '请输入调度费退款金额', trigger: 'blur' },
        ],
        manageRefund: [
          { required: true, message: '请输入管理费退款金额', trigger: 'blur' },
        ],
        deductionRefund: [
          { required: true, message: '请输入车损费退款金额', trigger: 'blur' },
        ],
      }
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit('update:visible', val);
      }
    },
    totalRefundAmount() {
      return this.dv(this.form.ridingRefund) 
        + this.dv(this.form.dispatchRefund) 
        + this.dv(this.form.manageRefund) 
        + this.dv(this.form.deductionRefund);
    }
  },
  methods: {
    dv(val) {
      if (isEmpty(val)) {
        return 0;
      }
      return val;
    },
    getDetail() {
      this.loading = true;
      getOrder(this.id).then(response => {
        this.detail = response.data;
      }).finally(() => {
        this.loading = false;
      });
    },
    handleOpen() {
      this.getDetail();
      this.reset();
    },
    reset() {
      this.form = {
        orderId: this.id,
        ridingRefund: null,
        dispatchRefund: null,
        manageRefund: null,
        deductionRefund: null,
        reason: null,
      };
      this.resetForm('form');
    },
    handleSubmit() {
      this.$refs.form.validate().then(() => {
        this.submitLoading = true;
        refundOrder(this.form).then((response) => {
          if (response.code == 200) {
            this.$message.success('退款成功');
            this.dialogVisible = false;
            this.$emit('success');
          }
        }).finally(() => {
          this.submitLoading = false;
        });
      });
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
