<template>
  <div :style="{'width':width,'line-height':height}" style="font-size: 0;">

    <al-label
      :label="label"
      :labelWidth="labelWidth"
      :labelOption="labelOption"
      :value="labelActiveIndex"
      @input="labelChange"
      :height="height">{{label}}</al-label>

    <al-form-item :class="{'autoWidth':label}" :prop="prop"
                  :style="{width:label||labelOption.length?'calc( 100% - '+labelWidth+')':'100%','line-height':height}" style="font-size: 0;">
      <el-radio-group
        v-if="type!='button'"
        :value="value"
        @input="onInput"
        :style="{width:'100%'}"
        :disabled="disabled">
        <div v-for="(item,index) in options" :key="index" :style="{'width':radioWidth,'height':height,'line-height':height,'margin-bottom':border?'15px':''}" style="display: inline-block;margin-right: 30px;">
          <el-radio :border="border" :ref="'radio'" :style="{'width':border?'':radioWidth,'height':height,'line-height':height}" :label="item.value" @click.native="radioClick(item)">{{item.label}}</el-radio>
        </div>
      </el-radio-group>
      <el-radio-group
        v-else
        :size="size"
        :value="value"
        @input="onInput"
        :style="{width:'100%'}"
        :disabled="disabled">
        <el-radio-button v-for="(item,index) in options" :label="item.value" :key="index" @click.native="radioClick(item)" :disabled="!!item.disabled">
          <span v-if="!item.tooltip">{{item.label}}</span>
          <el-tooltip>
            <div slot="content">
              <p v-html="item.tooltip"></p>
            </div>
            <span v-if="item.tooltip">{{item.label}}</span>
          </el-tooltip>

        </el-radio-button>
      </el-radio-group>
    </al-form-item>
  </div>
</template>

<script>
    import AlFormItem from '../AlFormItem'

    export default {
        name: "index",
        data() {
            return {
                radioWidth:''
            }
        },
        props: {
            //labelOption 激活值
            labelActiveIndex:{
                type:Number,
                default:0
            },
            //label下拉框的选项
            labelOption: {
                type: Array,
                default() {
                    return []
                }
            },
            type:{
                type:String,
                default:''
            },
            //配置项
            options:{
                type:Array,
                default(){
                    return []
                }
            },
            prop: {
                type: String,
                default: ''
            },
            //输入值
            value: {
                default: ''
            },
            //标签
            label: {
                type: String,
                default: ""
            },
            //宽度
            width: {
                type: String,
                default: '320px'
            },
            //尺寸
            size: {
                type: String,
                default: 'medium'
            },
            //标签宽度
            labelWidth: {
                type: String,
                default: "100px"
            },
            //组件高度
            height: {
                type: String,
                default: '36px'
            },
            //组件禁用
            disabled: {
                type: Boolean,
                default: false
            },
            //边框
            border: {
                type: Boolean,
                default: false
            }
        },
        computed:{
            refList(){
                return this.$refs
            }
        },
        watch:{
            refList:{
                immediate:true,
                deep:true,
                handler(){
                    var width=0;
                    setTimeout(()=>{
                        for(var i in this.refList['radio']){
                            if(this.refList['radio'][i].$el.offsetWidth&&this.refList['radio'][i].$el.offsetWidth>width){
                                width=this.refList['radio'][i].$el.offsetWidth
                            }
                        }
                        if(width){
                            this.radioWidth=width+'px'
                        }
                    },0)
                }
            }
        },
        methods: {
            radioClick(item){
                this.$emit("radioClick",item)
            },
            change(item) {
                this.$emit("change", item)
            },
            onInput(newValue) {
                this.$emit("input", newValue);
                for(var i=0;i<this.options.length;i++){
                    if(this.options[i].value==newValue){
                        this.$emit("change", this.options[i]);
                    }
                }
            },
            labelChange(v){
                this.$emit('labelChange',v)
            },
        },
        components:{
            AlFormItem
        }
    }
</script>

<style scoped lang="scss">
  /deep/{
    .el-radio.is-bordered{
      padding: 0 20px 0 20px;
    }
    .el-radio__input {
      cursor: pointer;
      vertical-align: -2px;
    }
    /*.el-radio-button--medium .el-radio-button__inner{*/
    /*  padding: 7px 20px;*/
    /*}*/
  }
  /deep/ {
    .el-switch__label--left {
      background: transparent !important;
    }
  }

  .label {
    display: inline-block;
    font-size: 14px;
    text-align: right;
    padding-right: 12px;
    box-sizing: border-box;
    vertical-align: top;

    /deep/ {
      .el-icon--right {
        margin-left: 2px;
      }
    }
  }
</style>
