<script lang="ts" setup>
import { reactive, ref } from 'vue';
import { traduction } from '@/utils/language';
import Dialog from '@/components/Dialog/Dialog.vue';

const dialogRef = ref();
const show = ref(false);
const disabled = ref(true);
const dialogConfig = reactive({
  title: traduction('COMMON_CONFIRM'),
  content: traduction('COMMON_ASK_OK'),
  id: 'portDialog',
  alignItems: 'center',
});

defineProps({
  title: {
    type: String,
    default: '',
  },
  type: {
    type: String,
    default: '',
  },
  row: {
    type: Object,
    required: false,
    default: () => {},
  },
});

defineExpose({
  show() {
    show.value = true;
  },
  hide() {
    show.value = false;
  },
  enable: () => {
    disabled.value = false;
  },
  disable: () => {
    disabled.value = true;
  },
});
const emit = defineEmits(['close']);

// 发送关闭弹窗的事件，由外部调用关闭弹窗(不在该组件内自行关闭主要是考虑到在关闭弹窗时，外部调用可能会进行一系列的操作，如提示用户、接口保存等)
function closeDrawer(reason: boolean) {
  if (!reason) {
    dialogRef.value.show();
  } else {
    emit('close', reason);
  }
}

function closeModal(reason: boolean) {
  if (reason) {
    emit('close', false);
  }
  dialogRef.value.hide();
}
</script>

<template>
  <div v-if="show" v-reqKeepAlive.drawer class="drawer-box">
    <el-drawer
      v-model="show"
      :close-on-click-modal="false"
      class="custom-drawer"
      :show-close="false"
      :title="title"
      size="552px"
      :append-to-body="false"
      :z-index="2000"
    >
      <slot></slot>
      <div class="drawer-footer">
        <el-button
          :id="'customOkBtn'"
          type="primary"
          :disabled="disabled"
          @click="closeDrawer(true)"
        >
          {{ $t('COMMON_SAVE') }}
        </el-button>
        <el-button :id="'customCancelBtn'" @click="closeDrawer(false)">
          {{ $t('COMMON_CANCEL') }}
        </el-button>
      </div>
    </el-drawer>
    <Dialog ref="dialogRef" :config="dialogConfig" @close="closeModal"></Dialog>
  </div>
</template>

<style lang="scss" scoped>
.drawer-box {
  :deep(.custom-drawer) {
    .el-form {
      .el-form-item {
        margin-bottom: 24px;

        .el-form-item__label {
          padding-right: 24px;
          font-size: 12px;
        }
      }

      .el-form-item:nth-last-child(1) {
        margin-bottom: 0;
      }
    }

    .el-drawer__body {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      padding: 0;

      .root-container {
        padding: 0 24px 72px 24px;
      }

      .drawer-footer {
        position: fixed;
        bottom: 0;
        right: 0;
        z-index: 999999;
        width: 552px;
        background-color: var(--o-bg-color-base) !important;
        padding: 8px 24px;
        display: flex;
        align-items: center;
        justify-content: flex-end;
        box-shadow: 0 8px 16px 0 rgba(0, 0, 0, 0.2);
      }
    }
  }

  .drawer-container {
    position: relative;
    height: 100%;
  }

  :deep(.el-drawer__header) {
    font-size: 16px;
    color: var(--o-text-color-primary);
    font-weight: bold;
    padding: 24px 24px 16px 24px;
    margin-bottom: 0;
  }
  :deep(.custom-drawer.el-drawer) {
    top: 48px;
    border-radius: 2px 2px 0 0;
    height: calc(100% - 48px);
    min-height: 462px;
  }

  :deep(.el-drawer__body) {
    overflow: auto;
    padding-top: 0;
  }
}
</style>
