<template>
<el-dialog v-model="approvalDialogVisible" title="借用审批" width="600px" :close-on-click-modal="false">
        <div>
        <p><strong>申请编号：</strong>{{ approvalData.application_no }}</p>
        <p><strong>借用人：</strong>{{ approvalData.emergency_contact }}</p>
        <p><strong>课程：</strong>{{ approvalData.purpose }}</p>
        <p><strong>学号/工号：</strong>{{ approvalData.borrower_info.user_code }}</p>
        <p><strong>联系方式：</strong>{{ approvalData.emergency_phone }}</p>
        <p><strong>个人/团队：</strong>{{ approvalData.team_type_label }}</p>
        <p><strong>归还时间:</strong>{{ approvalData.expected_end_time }}</p>
        <p><strong>存放仓库：</strong>{{ approvalData.return_location }}</p>
        </div>
    
        <div v-if="showRejectReason" style="margin-top: 16px;">
        <el-input
            type="textarea"
            v-model="rejectReason"
            placeholder="请输入拒绝原因"
            rows="3"
        />
        </div>
    
        <template #footer>
        <el-button @click="approvalDialogVisible = false">关闭</el-button>
        <el-button type="danger" @click="handleReject">拒绝</el-button>
        <el-button type="primary" @click="handleApprove">通过</el-button>
        </template>
  </el-dialog>
</template>

<script lang="ts" setup name="BorrowApprovalDialog" >
import { ref } from 'vue';
import { BorrowingReview } from '../api';
import { ElMessage } from 'element-plus';
const approvalDialogVisible = ref(false);
const approvalData = ref<any>({});
const showRejectReason = ref(false);
const rejectReason = ref('');





function openApprovalDialog(row) {
  approvalData.value = row;
  approvalDialogVisible.value = true;
  showRejectReason.value = false;
  rejectReason.value = '';
}

defineExpose({ openApprovalDialog });



async function handleApprove() {
  try {
        await BorrowingReview(approvalData.value.id, 'approve', '同意借用');
        ElMessage.success('审批通过');
        approvalDialogVisible.value = false;
    } catch (err) {
        ElMessage.error('审批失败');
    }
}

function handleReject() {
    if (!showRejectReason.value) {
        showRejectReason.value = true;
        return;
    }

    if (!rejectReason.value.trim()) {
        ElMessage.warning('请填写拒绝原因');
        return;
    }

    try {
        BorrowingReview(approvalData.value.id, 'reject', rejectReason.value);
        ElMessage.success('审批拒绝');
        approvalDialogVisible.value = false;
    } catch (err) {
        ElMessage.error('拒绝失败');
    }
}


</script>

