<template>
  <div class="tem-selector">
    <el-dialog :title="'选择区块版式'" :visible.sync="isShow" @open="loadBlockTemplateList" :width="dialogWidth">
      <el-radio-group v-model="radio" @change="onChange">
        <div class="div-row">
          <el-row :gutter="20">
            <el-col :span="6" v-for="item in templateData" :key="item.ID">
              <div class="image">
                <img :src='item.logoFile'>
              </div>
              <el-radio :label="item.name"></el-radio>
            </el-col>
          </el-row>
        </div>
      </el-radio-group>
      <div slot="footer">
        <el-button @click="isShow = false">取 消</el-button>
        <el-button type="primary" :loading="cSubmitLoading" @click="okHandler">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style>
.tem-selector .image {
  margin-bottom: 10px;
}

.tem-selector .el-radio__label {
  font-size: 12px;
  padding-left: 5px;
}

.tem-selector .el-col-6 {
  text-align: center;
  margin: 10px 0;
}
</style>

<script>
import util from '@src/common/util.js'
export default {
  computed: {
    isShow: {
      get() {
        return this.show
      },
      set(val) {
        this.$emit('update:show', val)
      }
    },
    cSubmitLoading: {
      get() {
        return this.submitLoading
      },
      set(val) {
        this.$emit('update:submitLoading', val)
      }
    }
  },
  data() {
    return {
      dialogWidth: '',
      templateData: [],
      radio: '',
      selected: {}
    }
  },
  methods: {
    async loadBlockTemplateList() {
      let data = await axios.get('/ui/blocktemplateselector')
      this.templateData = data.data.data
      if (document.documentElement.clientWidth >= 1920) {
        this.dialogWidth = '35%'
      } else {
        this.dialogWidth = '50%'
      }
    },
    okHandler() {
      if (!this.selected.ID) {
        util.showErrorMessageBox('请先选择区块版式')
        return
      }
      this.cSubmitLoading = true
      this.$emit('clickOK', this.selected)
    },
    onChange(val) {
      this.templateData.forEach(function(value) {
        if (value.name === val) {
          this.selected = value
        }
      }, this)
    }
  },
  props: { show: [Boolean], submitLoading: [Boolean] }
}
</script>
