<template>
  <el-dialog title="选择模板" :visible.sync="isShow" @open="dialogOpen" append-to-body class="style-template-selector-dialog">
    <el-radio-group v-model="tmpTemplate" v-loading="dialogLoading" class="templates-group-wrap">
      <div v-for="item in templates" class="template-item" :key="item.ID" @click="tmpTemplate = item.ID">
        <em>
          <img :src="item.logoFile" :alt="item.name">
        </em>
        <el-radio :label="item.ID">{{item.name}}</el-radio>
      </div>
    </el-radio-group>
    <div slot="footer">
      <el-button @click="isShow = false">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </div>
  </el-dialog>
</template>
<script>
export default {
  props: {
    show: Boolean

  },
  data() {
    return {
      dialogLoading: false,
      templates: [],
      tmpTemplate: ''
    }
  },
  computed: {
    isShow: {
      get() {
        return this.show
      },
      set(val) {
        this.$emit('update:show', val)
      }
    }
  },
  methods: {
    async dialogOpen() {
      this.tmpTemplate = ''
      this.dialogLoading = true
      try {
        let res = await axios.get('/ui/stylenodes/stylenodetemplates')
        this.templates = res.data.data
      } catch (e) {}
      this.dialogLoading = false
    },
    confirm() {
      if (this.tmpTemplate === '') {
        this.$alert('请选择一个模板！', '提示', {
          type: 'warning'
        })
        return
      }
      let index = this.templates.findIndex(val => val.ID === this.tmpTemplate)
      this.$emit('callback', this.templates[index].template)
      this.isShow = false
    }
  }
}
</script>
<style>
  .style-template-selector-dialog .el-dialog {
    width: 720px;
  }

  @media (max-width: 480px) {
    .style-template-selector-dialog .el-dialog {
      width: 96%;
    }
  }
</style>
<style scoped>
  .templates-group-wrap {
    min-height: 300px;
    border: 1px solid #f5f5f5;
    padding: 10px;
  }

  .template-item {
    width: 125px;
    height: 145px;
    text-align: center;
    margin: 5px 0 0 5px;
    float: left;
  }

  .template-item em {
    width: 126px;
    height: 126px;
    display: table-cell;
    vertical-align: middle;
    text-align: center;
    overflow: hidden;
  }

  .template-item img {
    display: block;
    max-width: 100%;
    max-height: 100%;
    margin: 2px 0 2px 2px;
    border: #f7f7f7 1px solid;
  }
</style>
