
<template>
  <div class="xtx-confirm">
    <div class="wrapper" ref="target">
      <div class="header" v-if="options.title">
        <h3>{{ options.title }}</h3>
        <a href="JavaScript:;" class="iconfont icon-close-new" @click="cancelCallback"></a>
      </div>
      <div class="body">
        <div class="typeImg">
          <img
            style="height: 48px; width: 48px"
            v-if="options.type == 'success'"
            src="@/assets/images/icon/success.png"
            alt=""
          />
          <img
            style="height: 48px; width: 48px"
            v-else-if="options.type == 'warn'"
            src="@/assets/images/icon/warn.png"
            alt=""
          />
          <img
            style="height: 48px; width: 48px"
            v-else
            src="@/assets/images/icon/error.png"
            alt=""
          />
        </div>
        <span>{{ options.text }}</span>
      </div>
      <div class="footer">
        <!-- <button @click="cancelCallback" size="mini" type="gray">取消</button> -->
        <button @click="confirmCallback" size="mini" type="primary">{{$t('confirm.text')}}</button>
      </div>
    </div>
  </div>
</template>
  

  <script lang="ts">
// 注意：当前组件不是在 #app 下进行渲染，无法使用 #app 下的环境（全局组件，全局指令，原型属性函数）
import { ref } from 'vue'
import { useI18n } from 'vue-i18n'
// import { onClickOutside } from '@vueuse/core'
export default {
  name: 'showConfirm',
  props: {
    type: {
      type: String,
      default: 'success'
    },
    options: {
      type: Object,
      default: () => {
        return {
          title: '',
          text: '',
          type: 'success',
          callback: () => {}
        }
      }
    },
    // 关闭方法
    close: {
      type: Function,
      default: () => {}
    },
    // 取消按钮
    // cancelCallback: {
    //   type: Function,
    //   default: () => {}
    // }
  },
  setup(props) {
    // 点击 target 目标元素外部相当于点击了取消
    const target = ref(null)
    const { locale: language, t } = useI18n()
    let current = language
    if (!current) {
      current = window.navigator.language || window.navigator.userLanguage || null
        if (current && !/^zh/.test(current)) {
            console.log('自动获取浏览器语言:' + current)
            current = 'en'
        }else{
          current = 'zh'
        }
    }
    // onClickOutside(target, () => {
    //   props.cancelCallback()
    // })
    function cancelCallback() {
      props.close()
    }
    function confirmCallback() {
      props.options.callback()
      props.close()
    }
    return { options:props.options,target,confirmCallback,cancelCallback }
  }
}
</script>

<style scoped lang="less">
.xtx-confirm {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  z-index: 8888;
  background: rgba(0, 0, 0, 0.5);
  .wrapper {
    width: calc(100% - 40px);
    max-width: 400px;
    border-radius: 10px 10px 10px 10px;
    background: #fff;
    border-radius: 4px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    .header,
    .footer {
      height: 50px;
      line-height: 50px;
      padding: 0 20px;
      text-align: center;
    }
    .body {
      padding: 30px 40px;
      font-size: 16px;
      font-family: PingFang SC-Regular, PingFang SC;
      font-weight: 400;
      color: #202020;
      line-height: 19px;
      text-align: center;
      .icon-warning {
        color: red;
        margin-right: 3px;
        font-size: 16px;
      }
      .typeImg {
        margin-bottom: 20px;
      }
    }
    .footer {
      //   text-align: right;
      border-top: 1px solid #ebebeb;
      padding: 20px;
      .xtx-button {
        margin-left: 20px;
      }
      button {
        height: 40px;
        background: #29b2ff;
        border-radius: 4px 4px 4px 4px;
        opacity: 1;
        font-size: 14px;
        font-family: PingFang SC-Regular, PingFang SC;
        font-weight: 400;
        color: #ffffff;
        border: none;
        padding: 10px 50px;
        text-align: center;
        margin: 0 15px;
      }
    }
    .header {
      position: relative;
      h3 {
        font-weight: normal;
        font-size: 18px;
      }
      a {
        position: absolute;
        right: 15px;
        top: 15px;
        font-size: 20px;
        width: 20px;
        height: 20px;
        line-height: 20px;
        text-align: center;
        color: #999;
        &:hover {
          color: #666;
        }
      }
    }
  }
}
</style>
  
  