<template>
  <el-dialog 
    title="还车审核" 
    :visible.sync="dialogVisible" 
    width="500px" 
    append-to-body 
    @open="handleOpen"
  > 
    <el-form :model="form" :rules="rules" ref="form" label-width="5em" v-loading="loading" size="small">
      <el-form-item label="车损费" prop="deductionFee">
        <el-input-number 
          v-model="form.deductionFee" 
          type="number" 
          placeholder="请输入车损费" 
          :min="0" 
          controls-position="right" 
          :max="detail.depositFee - detail.totalFee" 
          style="width: 100%;"
        />
      </el-form-item>
      <el-form-item label="审核意见" prop="remark">
        <el-input v-model="form.remark" type="textarea" placeholder="请输入审核意见" show-word-limit maxlength="200" />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="success" plain @click="handleSubmit(true)" icon="el-icon-check" :loading="submitLoading">通过</el-button>
      <el-button type="danger" plain @click="handleSubmit(false)" icon="el-icon-close" :loading="submitLoading">驳回</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {getOrder, verifyOrder} from '@/api/bst/order'

export default {
  props: {
    id: {
      type: String,
      default: null
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      detail: {},
      form: {},
      loading: false,
      submitLoading: false,
      rules: {
      }
    }
  },
  computed: {
    dialogVisible: {
      get() {
        return this.visible;
      },
      set(val) {
        this.$emit('update:visible', val);
      }
    },
    // 可退款金额
    canRefundAmount() {
      let payAmount = this.detail.payAmount || 0;  
      let payRefunded = this.detail.payRefunded || 0;
      let payRefunding = this.detail.payRefunding || 0;
      return payAmount - payRefunded - payRefunding;
    }
  },
  methods: {
    getDetail() {
      this.loading = true;
      getOrder(this.id).then(response => {
        this.detail = response.data;
        this.form.amount = this.canRefundAmount;
      }).finally(() => {
        this.loading = false;
      });
    },
    handleOpen() {
      this.getDetail();
      this.reset();
    },
    reset() {
      this.form = {
        id: this.id,
        pass: null,
        remark: null,
      };
      this.resetForm('form');
    },
    handleSubmit(pass) {
      this.$refs.form.validate().then(() => {
        this.$confirm(`确定${pass ? '通过' : '驳回'}吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.form.pass = pass;
          this.submitLoading = true;
          verifyOrder(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>