<!--
 * @Author: xiejiang
 * @Date: 2024-11-06 09:53:55
 * @LastEditors: xiejiang
 * @LastEditTime: 2024-12-11 14:09:22
 * @Description: 抽屉
 * Copyright（c）2024 by 好老师教育科技有限公司 All right Reserved.
-->

<template>
  <t-drawer
    v-model:visible="visible"
    :close-btn="true"
    :size="width"
    :footer="true"
    size-draggable
    :close-on-overlay-click="false"
    v-bind="$attrs"
    :on-close="closeDrawer"
  >
    <template #header>
      <div>{{ title }}</div>
    </template>
    <template #body>
      <slot />
    </template>
    <template #footer>
      <div class="flex items-center justify-center">
        <t-button variant="outline" @click="closeDrawer" v-if="cancelBtn">取消</t-button>
        <t-button @click="confirm" :loading="loading">{{ confirmBtnText }}</t-button>
      </div>
    </template>
  </t-drawer>
</template>

<script setup lang="ts" name="Drawer">
interface DrawerProps {
  drawerVisible: boolean // 双向绑定的值 ==>必传
  title?: string
  loading?: boolean // 确定按钮是否加载中 ==>非必传（默认值false）
  width?: string // 抽屉宽度 ==>非必传（默认值480px）
  confirmBtnText?: string // 确定按钮文字 ==>非必传（默认值取消）
  cancelBtn?: boolean // footer里的取消按钮是否显示
}
const props = withDefaults(defineProps<DrawerProps>(), {
  title: '',
  width: '480px',
  showFooter: true,
  loading: false,
  cancelBtn: true,
  confirmBtnText: '确定'
})
const visible = ref(props.drawerVisible)
// const emit = defineEmits(['update:drawerVisible', 'close', 'confirm'])
const emit = defineEmits<{
  'update:drawerVisible': [value: boolean]
  close: []
  confirm: []
}>()
const closeDrawer = () => {
  emit('update:drawerVisible', false)
  emit('close')
}
const confirm = () => {
  emit('confirm')
}
watch(
  () => props.drawerVisible,
  val => {
    visible.value = val
  },
  { immediate: true }
)
</script>

<style lang="less">
.t-drawer__footer {
  padding-top: 10px;
  padding-bottom: 12px;
  height: 48px;
}

.t-drawer__header {
  min-height: 48px;
}

.t-drawer__body {
  padding: 20px;
}
</style>
