<template>
  <div class="radio-wrap" v-if="item">
    <!-- <img :src="datas.url.replace('{imageUrlPrefix}',imgUrl)" v-if="datas.url"> -->
    <p v-for="(it,index) in datas.questionDetailList" :key="it.id" class="list" @click="handleClick(it,index,true)">
      <img :src="it.select==1?check:defaultPic">
      <!-- <span :class="{'check':it.select==1}">{{it.name}}</span> -->
      <span v-if="((it.description||it.name).indexOf('${'))==-1" :class="{'check':it.select==1}">{{it.description||it.name}}</span>
      <!-- <MultiLineInput v-else 
          @handleInp="inpVal($event,index)"
          :msg="it.name" 
          :part="it" 
          :border="false" 
          :inline="true" 
          :select="it.select==1" 
          /> -->
      <OptionInp v-else :item="it" @handleInp="inpVal($event,index)" @handleSelec="handleClick(it,index)"/>
    </p>
  </div>
</template>
<script type="text/javascript">
import icon from '../images/radio-default.png'
import checkIcon from '../images/radio-check.png'
import {patt,imageUrlPrefix,concatVal} from '@utils/tools.js'
import MultiLineInput from '../common/MultiLineInput.vue';
import OptionInp from '../common/OptionInp.vue';
  export default{
    name:'Radio',
    data(){
      return{
        defaultPic:icon,
        check:checkIcon,
        datas:{},
        imgUrl:imageUrlPrefix,
      }
    },
    props:['item'],
    created(){
      // this.datas = JSON.parse(JSON.stringify(this.item));
      this.datas = this.item;
    },
    methods:{
      handleClick(it,index,flg){
        const list = this.datas;
        let data = list.questionDetailList&&list.questionDetailList.slice(0); //数组深拷贝？
        for(let i=0;i<data.length; i++){
          data[i].select = 0
          if(i==index){
            data[i].select = 1;
          }
        }
        let temp = concatVal(data);
        const newData = Object.assign({},this.datas,{questionDetailList:data,value:temp.value,valueP:temp.valueP})
        this.$emit("updata",newData);
        if(flg){
          document.activeElement.blur();
        }
      },
      inpVal(val,index){//输入框失焦处理  
        // 输入框回读
        let detailList = this.datas.questionDetailList;
        let currItem = detailList[index];
        currItem.value = val; 
        // 输入框失焦重新拼接父级的value
        let temp = concatVal(detailList);
        this.datas.value = temp.value;
        this.datas.valueP = temp.valueP;
        this.$emit("updata",this.datas);
      },
      /*inpSele(index){//输入框反选
        let detailList = this.datas.questionDetailList;console.log("单选：",index,detailList[index])
        detailList[index].select = 1;
      }*/
    },
    watch:{
      item:{
        handler(newVal,oldVal){
          this.datas = JSON.parse(JSON.stringify(newVal));
        },
        deep:true
      }
    },
    components:{
      MultiLineInput,
      OptionInp
    }
  }
</script>
<style lang="less" scoped>
  .radio-wrap{
    img{
      width:100%;
    }
    .list{
      color: #7C828E;
      margin:0 .1rem .1rem 0;
      padding: .12rem .1rem;
      display: inline-block;
      white-space: nowrap;
      -webkit-white-space: nowrap;
      // -webkit-box-orient: vertical;
      img{
        width: .38rem;
        vertical-align: middle;
      }
      .check{
        color: #4F50FF;
      }
    }
  }
</style>