<script setup lang="ts" name="Modal">
const props = defineProps<{
  modelValue: boolean;
  text?: string;
  icon?: string; // 图标 '' 'success' 'error'
  width?: string;
  cancel?: string;
}>();

const emit = defineEmits(["update:modelValue", "confirm", "cancel"]);

const closeModal = () => {
  emit("update:modelValue", false);
  emit("cancel");
};

const confirm = () => {
  emit("confirm");
};
</script>

<template>
  <teleport to="body">
    <!-- @click.self="closeModal" -->
    <div v-if="modelValue" class="modal-overlay">
      <div class="modal-container" :style="{ width }">
        <div class="modal-header">
          <div>提示</div>
          <button class="close-btn" @click="closeModal">×</button>
        </div>
        <div class="modal-body">
          <div class="slot-warpper">
            <div v-if="icon">
              <el-icon :size="85" color="rgb(253, 84, 81)" class="icon" v-if="icon === 'error'">
                <Warning />
              </el-icon>
              <el-icon :size="85" color="rgb(44, 158, 247)" class="icon" v-if="icon === 'success'">
                <circle-check />
              </el-icon>
            </div>
            <div class="text">{{ text }}</div>
            <div style="padding: 20px; display: flex; justify-content: center;">
              <div class="button cancel" v-if="cancel" @click="closeModal">取消</div>
              <div class="button error" v-if="icon === 'error'" @click="confirm">确定</div>
              <div class="button success" v-if="icon === 'success'" @click="confirm">确定</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </teleport>
</template>

<style lang="scss" scoped>
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
  .modal-container {
    background: #f3f5fa;
    border: 0;
    border-radius: 4px;
    background-clip: padding-box;
    border-radius: 8px;
    min-width: 600px;
    .modal-header {
      padding: 16px;
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 16px;
      font-weight: bold;
      color: #7a8495;
      .close-btn {
        background: none;
        border: none;
        cursor: pointer;
        font-size: 31px;
        color: #bcc3cb !important;
      }
    }
  }

  .modal-body {
    padding: 0 8px 8px !important;
    background: #f3f5fa;
    border-radius: 4px;
    .slot-warpper {
      border: 1px solid #dce5eb;
      background: #fff;
      padding: 30px;
      border-radius: 4px;
      overflow: auto;
      height: 100%;
      display: flex;
      flex-direction: column;
      align-content: center;
      align-items: center;
      .icon{
        margin: 20px 0;
      }

      .text{
        font-size: 18px;
        font-weight: 700;
        color: #7a8495;
        word-break: break-word;
        display: block;
        overflow: hidden;
        padding: 0 30px;
      }
      .button{
        margin-right: 20px;
        cursor: pointer;
        width: 150px;
        height: 34px;
        line-height: 34px;
        text-align: center;
        color: #fff;
        border-radius: 4px;
        vertical-align: middle;
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;
        user-select: none;
      }
      .cancel{
        background: #bcc3cb;
      }
      .error{
        background: rgb(253, 84, 81);
      }
      .success{
        background: #2c9ef7;
      }
      .button:last-child{
        margin-right: 0;
      }
    }
  }
}
</style>