<template>
  <div class="radio-wrap" v-if="item">
    <p class="quest">{{indx + '.' + item.name}}</p>
    <img :src="item.url.replace('{imageUrlPrefix}',imgUrl)" v-if="item.url">
    <p v-for="(it,index) in item.questionDetailList" :key="it.id" class="list">
      <img :src="index==current?check:defaultPic" @click="handleClick(it,index)">
      <!-- <img :src="it.select?check:defaultPic" @click="handleClick(it,index)"> -->
      <span :class="{'check':index==current}">{{it.name}}</span>
    </p>
  </div>
</template>
<script type="text/javascript">
import icon from '../images/radio-default.png'
import checkIcon from '../images/radio-check.png'
import {deepClone} from '@utils/tools.js'
  export default{
    name:'Radio',
    data(){
      return{
        defaultPic:icon,
        check:checkIcon,
        current:null,
        datas:{}
      }
    },
    props:['item','indx','pId'],
    methods:{
      handleClick(it,index){
        this.current = index;
        const list = JSON.parse(JSON.stringify(this.item));
        let data = list.questionDetailList.slice(0); //数组深拷贝？
        // let data = deepClone(list);
        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.item,{questionDetailList:data},{value:value})
        this.$store.commit('setDatas',{data:newData,pId:this.pId});
        this.$store.commit('getUpdate'); //更新数据
      }
    }
  }
</script>
<style lang="less" scoped>
  .radio-wrap{
    font-size: .3rem;
    .quest{
      color:#000;
      margin-bottom: .2rem;
    }
    img{
      width:100%;
    }
    .list{
      color: #7C828E;
      margin:0 .1rem .1rem 0;
      padding: .12rem .1rem;
      display: inline-block;
      img{
        width: .38rem;
        vertical-align: middle;
      }
      .check{
        color: #4F50FF;
      }
    }
  }
</style>