<template>
  <div class="check-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},{'exclu':exclusion !==999 && it.exclusion !== exclusion}]">{{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" 
          ref="inp"
          @handleInp="inpVal($event,index)" 
          :exclu="exclusion !==999 && it.exclusion !== exclusion" 
          />
    </p>
  </div>
</template>
<script type="text/javascript">
import {imageUrlPrefix,patt} from '@utils/tools.js';
import icon from '../images/check-default.png';
import checkIcon from '../images/check.png';
import MultiLineInput from '../common/MultiLineInput.vue';
import OptionInp from '../common/OptionInp.vue';
  export default{
    name:'CheckBox',
    data(){
      return{
        imgUrl:imageUrlPrefix,
        defaultPic:icon,
        check:checkIcon,
        datas:{},
        exclusion:999 //互斥
      }
    },
    props:['item'],
    created(){
      // this.datas = JSON.parse(JSON.stringify(this.item));
      this.datas = this.item;
      this.resetExc();
    },
    methods:{
      handleClick(it,index){
        const that = this;
        const list = this.datas;
        let data = list.questionDetailList&&list.questionDetailList.slice(0);
        // 处理互斥
        const excluArr = data.filter(it=>it.exclusion==1);
        const filArr = data.filter(it=>it.select==1);
        if(excluArr.length>0){//有互斥
          if(filArr.length>0){//有选中
            if(it.exclusion !== filArr[0].exclusion){
              return
            }
          }
          this.exclusion = it.exclusion;  
        }
        // 处理选中状态
        if(data[index].select){
          data[index].select = 0;
        }else{
          data[index].select = 1;
          this.exclusion = it.exclusion;
        }
        // 处理取消-互斥
        const filArr1 = data.filter(it=>it.select==1);
        if(excluArr.length>0){//有互斥
          if(filArr1.length==0){//无选中
            this.exclusion = 999;
          }else{
            this.exclusion = filArr1[0].exclusion;
          }
        }
        let value = "";
        for(let k in data){
          if(data[k].select==1){
            // 选中该项时，带的输入框内有值则加上
            if(data[k].value){
              let newVal = '#{' + data[k].value + '}';
              let str = data[k].name.replace(patt,newVal);
              value += str + '，'
            }else{
              value += data[k].name + '，'
            }
            
          }
        }
        const newData = Object.assign({},this.datas,{questionDetailList:data},{value:value?value.substring(0,value.length-1):''});
        this.$emit("updata",newData);
      },
      inpVal(val,index){//输入框失焦处理
        let valueStr = this.datas.value;
        if(valueStr){
          // let patt = /\$\{[^\]]+\}/g;
          let rePatt = /\#\{[^\]]+\}/g;
          let newVal = '#{' +val+'}';
          let str = "";
          // 区分第一次输入还是修改
          if(valueStr.indexOf("${") !== -1){
            str = valueStr.replace(patt,newVal);
          }else{//修改
            str = valueStr.replace(rePatt,newVal);
          }
          this.datas.value = str;
        }
        
        // 输入框回读
        let detailList = this.datas.questionDetailList;
        let currItem = detailList[index];
        currItem.value = val;
        this.$emit("updata",this.datas);
      },
      resetExc(){
        // 回读互斥项标识
        const arr = this.datas.questionDetailList;
        const filArr = arr.filter(it=>it.select==1);
        if(filArr.length > 0){
          this.exclusion = filArr[0].exclusion;
        }else{
          this.exclusion = 999;
        }
      }
    },
    watch:{
      item:{
        handler(newVal,oldVal){
          this.datas = JSON.parse(JSON.stringify(newVal));
          this.resetExc();
        },
        deep:true
      }
    },
    components:{
      MultiLineInput,
      OptionInp
    }
  }
</script>
<style lang="less" scoped>
  .check-wrap{
    img{
      width:100%;
    }
    .list{
      color: #7C828E;
      margin:0 .1rem .1rem 0;
      padding: .12rem .1rem;
      display: inline-block;
      white-space: nowrap;
      img{
        width: .38rem;
        vertical-align: middle;
      }
    }
    .check{
      color: #4F50FF;
    }
    .exclu{
      color:#e6e7e9;
    }
  }
</style>