<template>
  <view v-if="visible" class="tips-popup-overlay" @click="handleCancel">
    <view class="tips-popup-content" @click.stop="preventClose">
      <view class="tips-header">
        <text class="tips-title">提示</text>
      </view>

      <view class="tips-body">
        <text class="tips-text">{{ content }}</text>
        <textarea
          v-if="showInput"
          v-model="reason"
          class="tips-input"
          :placeholder="inputPlaceholder"
          :maxlength="200"
        />
      </view>

      <view class="tips-footer">
        <view class="tips-btn cancel-btn" @click="handleCancel">
          <text class="tips-btn-text">取消</text>
        </view>
        <view class="tips-btn confirm-btn" @click="handleConfirm">
          <text class="tips-btn-text confirm-btn-text">确定</text>
        </view>
      </view>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { ref } from 'vue'

const visible = ref(false)
const content = ref('')
const showInput = ref(false)
const reason = ref('')
const inputPlaceholder = ref('请输入作废理由')
let confirmCallback: ((reason?: string) => void) | null = null
let cancelCallback: (() => void) | null = null

interface PopupOptions {
  text: string
  confirm?: (reason?: string) => void
  cancel?: () => void
  showInput?: boolean
  inputPlaceholder?: string
}

const show = (options: PopupOptions) => {
  content.value = options.text
  confirmCallback = options.confirm || null
  cancelCallback = options.cancel || null
  showInput.value = options.showInput || false
  inputPlaceholder.value = options.inputPlaceholder || '请输入作废理由'
  reason.value = ''
  visible.value = true
}

const hide = () => {
  visible.value = false
  content.value = ''
  confirmCallback = null
  cancelCallback = null
  showInput.value = false
  reason.value = ''
}

const handleConfirm = () => {
  if (confirmCallback) {
    confirmCallback(reason.value)
  }
  hide()
}

const handleCancel = () => {
  if (cancelCallback) {
    cancelCallback()
  }
  hide()
}

const preventClose = () => {}

defineExpose({
  show,
  hide
})
</script>

<style lang="scss" scoped>
.tips-popup-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.5);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 9999;
}

.tips-popup-content {
  width: 80%;
  max-width: 320px;
  background-color: #fff;
  border-radius: 8px;
  overflow: hidden;
}

.tips-header {
  padding: 20px 15px 15px;
  text-align: center;
  border-bottom: 1px solid #f0f0f0;
}

.tips-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.tips-body {
  padding: 20px 15px;
  text-align: center;
}

.tips-text {
  font-size: 15px;
  color: #666;
  line-height: 1.5;
}

.tips-input {
  width: 100%;
  height: 80px;
  margin-top: 12px;
  padding: 8px 10px;
  font-size: 14px;
  color: #333;
  background-color: #f5f5f5;
  border: 1px solid #ddd;
  border-radius: 4px;
  box-sizing: border-box;
}

.tips-footer {
  display: flex;
  flex-direction: row;
  border-top: 1px solid #f0f0f0;
}

.tips-btn {
  flex: 1;
  padding: 15px;
  text-align: center;
  cursor: pointer;
}

.cancel-btn {
  border-right: 1px solid #f0f0f0;
}

.tips-btn-text {
  font-size: 16px;
  color: #666;
}

.confirm-btn {
  background-color: #fff;
}

.confirm-btn-text {
  color: #4B8CD9;
  font-weight: 500;
}
</style>
