<template>
  <el-dialog :model-value="visible" :width="width" title="拒绝退费" :close-on-press-escape="false"
    :close-on-click-modal="!loading" @close="close()">
    <div class="p-15px">
      <el-input v-model="reason" maxlength="500" resize="none" :autosize="{ minRows: 6, maxRows: 12 }" show-word-limit
        type="textarea" placeholder="请填写拒绝理由" />
    </div>
    <template #footer>
      <div text-center>
        <el-button plain class="w-100px" type="primary" @click="close()">关闭</el-button>
        <el-button class="w-100px" :loading="loading" type="primary" @click="confirm()">
          确定
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch, computed } from 'vue';
import { storeToRefs } from 'pinia';
import { ElMessage, ElMessageBox } from 'element-plus';
import { memberStore } from "@/store/member";
import { handleRefundOrder } from '@/api/hlw';


const emits = defineEmits(['close', 'change'])
const props = defineProps({
  width: { type: [String, Number], default: "500px" },
  data: { type: Object, default: () => ({}) },
  visible: { type: Boolean, default: false },
})
const { memberInfo } = storeToRefs(memberStore());


const reason = ref('')
const loading = ref(false)
function close() {
  if (loading.value) return;
  emits('close')
}

async function confirm() {
  if (reason.value.trim() === '') {
    ElMessage.info('请填写拒绝理由')
    return
  }
  if (loading.value) return;
  // await ElMessageBox.confirm('确定拒绝本次退费吗？', '提示', { type: 'warning' });
  loading.value = true;
  const { success, message } = await handleRefundOrder({ id: props.data._id, orderId: props.data.orderId, status: "REJECT", doctorCode: memberInfo.value.doctorNo, reason: reason.value.trim(), operatorId: memberInfo.value._id, operator: memberInfo.value.anotherName });
  loading.value = false;
  if (success) {
    ElMessage.success(message)
    close()
    emits('change')
  } else {
    ElMessage.error(message)
  }
}


watch(() => props.visible, n => {
  if (!n) return;
  reason.value = '';
  loading.value = false;
})

</script>