<template>
  <cr-popup
    v-model="show"
    :style="{ backgroundColor: 'transparent' }"
    :closeable="closeable"
    get-container="body"
    close-icon="close"
    class="panel-wrap"
    @close="onClose"
  >
    <div class="panel">
      <div class="panel-head">
        <slot name="title">
          <h4 v-if="title">{{ title }}</h4>
        </slot>
      </div>
      <div class="panel-body">
        <slot>
          {{ content }}
        </slot>
      </div>
      <div class="panel-foot" :class="{ split: confirmBtn && cancelBtn }">
        <cr-button size="large" block @click="onConcel" v-if="cancelBtn">
          {{ cancelBtn }}
        </cr-button>
        <cr-button type="warning" size="large" block @click="onConfirm" v-if="confirmBtn">
          {{ confirmBtn }}
        </cr-button>
      </div>
    </div>
  </cr-popup>
</template>

<script>
const DIALOG_SHOW_EVENT = "input";
const DIALOG_CLOSE_EVENT = "close";
const DIALOG_CONFIRM_EVENT = "confirm";
const DIALOG_CANCEL_EVENT = "cancel";
export default {
  name: "Modal",
  props: {
    value: null,
    confirmBtn: {
      type: String,
      default: "确定"
    },
    cancelBtn: {
      type: String,
      default: "取消"
    },
    closeable: {
      type: Boolean,
      default: true
    },
    title: {
      type: String,
      default: ""
    },
    content: {
      type: String,
      default: ""
    }
  },
  data() {
    return {
      show: false
    };
  },
  watch: {
    value: {
      immediate: true,
      handler(val) {
        this.show = val;
      }
    },
    show(val) {
      this.$emit(DIALOG_SHOW_EVENT, val);
    }
  },
  methods: {
    onClose() {
      this.$emit(DIALOG_CLOSE_EVENT, {});
    },
    onConfirm() {
      this.$emit(DIALOG_CONFIRM_EVENT, {});
    },
    onConcel() {
      this.$emit(DIALOG_CANCEL_EVENT, {});
    }
  }
};
</script>

<style lang="less" scoped>
@import "../style/var.less";
.panel {
  background-color: @white;
  border-radius: @border-radius-md;
  width: 300px;
  &-wrap {
    overflow: initial;
    @{deep} .cr-popup--close {
      top: -36px;
      right: 0;
      color: @white;
      font-size: 24px;
    }
  }
  &-head {
    border-top-left-radius: @border-radius-md;
    border-top-right-radius: @border-radius-md;
    padding: @padding-lg @padding-lg 5px @padding-lg;
    background: url("../assets/dialog-header-bg.png") right no-repeat;
    background-size: 34px 25px;
    background-position: right center;
    h4 {
      color: @black;
      font-size: @font-size-18;
      font-weight: @font-weight-bold;
      line-height: @line-height-lg + 1;
    }
  }
  &-body {
    padding: @padding-lg;
    font-size: @font-size-12;
    color: @gray-4;
  }
  &-foot {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: @padding-lg;
    &.split {
      .cr-button {
        width: 48%;
      }
    }
  }
}
</style>
