<template>
  <teleport to="#dialog">
    <div class="dialog-bg" v-if="show">
      <div class="dialog" :style="{ width: width + 'px' }">
        <div class="head">
          <h3>{{ title }}</h3>
          <el-icon @click="closeHandle" v-if="showClose || cornerClose">
            <Close />
          </el-icon>
        </div>
        <div class="content">
          <slot />
        </div>
        <div class="floot" v-if="showFloor">
          <el-button type="danger" v-if="showDel" @click="deleteHandle">删 除</el-button>
          <el-button @click="closeHandle" v-if="showClose">取 消</el-button>
          <el-button v-if="showSave" type="primary" :disabled="!!disabled" @click="enterHandle">
            {{ enterText }}
          </el-button>
        </div>
      </div>
    </div>
  </teleport>
</template>

<script setup lang="ts">
import { computed, ref, watch, watchEffect, nextTick } from "vue";
import { user } from "@/store/user";
import { operateIsPermissionByPath } from "@/directive/permission";
import { DialogProps } from "./type";

const props = withDefaults(defineProps<DialogProps>(), {
  width: 680,
  show: false,
  hideFloor: false,
  showClose: true,
  showSave: true,
  notSubmit: false,
  enterText: "保 存",
});


const emit = defineEmits<{
  (e: "update:show", show: boolean): void;
  (e: "quit"): void;
  (e: "submit"): void;
  (e: "delete"): void;
}>();

const showDel = ref(props.showDelete);
const showFloor = computed(() => !props.hideFloor);

const closeHandle = () => {
  emit("update:show", false);
  emit("quit");
};
const enterHandle = () => {
  emit("submit");
};
const deleteHandle = () => {
  emit("delete");
};

if (!showFloor.value && !props.notSubmit) {
  nextTick(() => enterHandle());
}

const disabled = computed(() => props.power && !operateIsPermissionByPath(props.power));
</script>

<style lang="scss" scoped>
@import "./style.scss";
</style>
