<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)">
      <img :src="it.select==1?check:defaultPic">
      <!-- <span :class="{'check':it.select==1}">{{it.name}}</span> -->
      <span v-if="(it.name.indexOf('${'))==-1" :class="{'check':it.select==1}">{{it.name}}</span>
      <MultiLineInput v-else 
          @handleInp="inpVal($event,index)"
          :msg="it.name" 
          :part="it" 
          :border="false" 
          :inline="true" 
          :select="it.select==1" 
          />
    </p>
  </div>
</template>
<script type="text/javascript">
import icon from '../images/radio-default.png'
import checkIcon from '../images/radio-check.png'
import {patt} from '@utils/tools.js'
import MultiLineInput from '../common/MultiLineInput.vue';
  export default{
    name:'Radio',
    data(){
      return{
        defaultPic:icon,
        check:checkIcon,
        datas:{}
      }
    },
    props:['item'],
    created(){
      // this.datas = JSON.parse(JSON.stringify(this.item));
      this.datas = this.item;
    },
    methods:{
      handleClick(it,index){
        // let patt = /\$\{[^\]]+\}/g;
        const list = this.datas;
        let data = list.questionDetailList&&list.questionDetailList.slice(0); //数组深拷贝？
        let value = "";
        for(let i=0;i<data.length; i++){
          data[i].select = 0
          if(i==index){
            data[i].select = 1;
            value = data[i].name;
          }
        }
        const newData = Object.assign({},this.datas,{questionDetailList:data},{value:value})
        this.$emit("updata",newData);
      },
      inpVal(val,index){//输入框失焦处理
        let valueStr = this.datas.value;
        if(valueStr){
          // let patt = /\$\{[^\]]+\}/g;
          let newVal =  val;//修改时替换值用
          const str = valueStr.replace(patt,newVal);
          this.datas.value = str;
        }
        
        // 输入框回读
        let detailList = this.datas.questionDetailList;
        let currItem = detailList[index];
        currItem.value = val; 
        this.$emit("updata",this.datas);
      },
    },
    watch:{
      item:{
        handler(newVal,oldVal){
          this.datas = JSON.parse(JSON.stringify(newVal));
        },
        deep:true
      }
    },
    components:{
      MultiLineInput
    }
  }
</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>