<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-form-item label="退款类型" prop="refundType">
        <el-radio-group v-model="form.refundType" placeholder="请选择退款类型" style="width: 100%;" @change="handleRefundTypeChange">
          <el-radio-button :label="OrderRefundType.DEPOSIT">押金 ({{ detail.canDepositRefundAmount | fix2 | dv }} 元)</el-radio-button>
          <el-radio-button :label="OrderRefundType.RIDE">订单费用 ({{ detail.canRideRefundAmount | fix2 | dv }} 元)</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="退款金额" prop="amount">
        <el-input-number 
          v-model="form.amount" 
          :max="canRefundAmount" 
          :min="0" 
          :placeholder="`请输入退款金额，最多可退款 ${canRefundAmount} 元`" 
          :precision="2" 
          style="width: 100%;" 
          controls-position="right"
        />
      </el-form-item>
      <el-form-item label="退款原因" prop="reason">
        <el-input v-model="form.reason" type="textarea" placeholder="请输入退款原因" show-word-limit maxlength="200" />
      </el-form-item>
    </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 { OrderRefundType } from '@/utils/enums';

export default {
  props: {
    id: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      OrderRefundType,
      detail: {},
      form: {},
      loading: false,
      submitLoading: false,
      rules: {
        amount: [
          { required: true, message: '请输入退款金额', trigger: 'blur' },
        ],
        refundType: [
          { required: true, message: '请选择退款类型', trigger: 'blur' },
        ],
      }
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit('update:visible', val);
      }
    },
    // 可退款金额
    canRefundAmount() {
      if (this.detail == null || this.detail.id == null) {
        return 0;
      }
      if (this.form.refundType == OrderRefundType.DEPOSIT) {
        return this.detail.canDepositRefundAmount;
      } else if (this.form.refundType == OrderRefundType.RIDE) {
        return this.detail.canRideRefundAmount;
      }
      return 0;
    }
  },
  methods: {
    handleRefundTypeChange() {
      this.form.amount = null;
    },
    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,
        refundType: OrderRefundType.DEPOSIT,
        amount: 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>
