<template>
  <div class="treat-wrap treatper btscroll">
    <div class="content">
      <div v-for="(it,i) in dtoList" 
          v-if="dtoList"
          :key="it.id"
          class="label">
        <p class="quest">{{i + 1 +'.' + (it.description||it.name)}}</p>
        <img class="questionImg" :src="it.url.replace('{imageUrlPrefix}',imgUrl)" v-if="it.url">
        <Label v-if="it.controlType==0" 
              :item="it" 
              :ppId="it.id"
              :order="i"
              :moduleType="datas.type"/>
        <!-- 上传图片 -->
        <UploadImg v-if="it.controlType==4" 
            :item="it" 
            :moduleType="datas.type"
            :imgList="imgs"/>
        <!-- 输入框 -->
        <Input v-if="it.controlType==6 || it.controlType==7"
            :item="it"
            :key="it.id"
            @updata="updataData($event,it.id,i)"/>
        <!-- 文本域 -->
        <ComTextArea v-if="it.controlType == 5"
            :item="it"
            @updata="updataData($event,it.id,i)"/>
        <Radio v-if="it.controlType==1" 
            :item="it"
            :key="it.id"
            @updata="updataData($event,it.id,i)"/>
        <CheckBox v-if="it.controlType==2" 
            :item="it"
            :key="it.id"
            @updata="updataData($event,it.id,i)"/>
        <!-- 多行输入-->
        <template
            v-if="it.controlType == 3"
            v-for="(part,index) in it.questionDetailList"
          >
            <MultiLineInput
              :msg="part.name"
              :part="part"
              @updata="updataData($event,it.id,i,index)"
            ></MultiLineInput>
        </template>
        <!-- 时间控件 -->
        <PickTime v-if="it.controlType==9"
            :item="it"
            :key="it.id"
            @updata="updataData($event,it.id,i)"/>
      </div>
      <div class="result" v-if="getText()">
        <p class="title">{{datas.name}}</p>
        <p>{{getText()}}</p>
      </div>
    </div>

    <div class="foot" v-if="modluesLen==2">
      <!-- <span class="back" @click="beBack">{{'返回'+ preName}}</span> -->
      <span class="back" @click="beBack">上一步</span>
      <span class="next" @click="toNext">预览并提交病历</span>
    </div>
    <div class="foot" v-else>
      <!-- <span class="back" @click="beBack">{{'返回'+ preName}}</span> -->
      <!-- <span class="next" @click="toNext">{{'进入'+ nextName}}</span> -->
      <span class="back" @click="beBack">上一步</span>
      <span class="next" @click="toNext">下一步</span>
    </div>
  </div>
</template>
<script type="text/javascript">
  import UploadImg from '../common/UploadImg.vue';
  import Label from '../common/Label.vue';
  import DetailBox from './DetailBox.vue';
  import Input from '../common/Input.vue';
  import ComTextArea from '../common/ComTextArea.vue';
  import {moduleCP,patt,imageUrlPrefix,setScroll,trimDots} from '@utils/tools';
  import Radio from '../common/Radio.vue';
  import CheckBox from '../common/CheckBox.vue';
  import BScroll from 'better-scroll';
  import MultiLineInput from '../common/MultiLineInput.vue';
  import PickTime from '../common/PickTime.vue';
  export default {
    name:'DiagTreat',
    data(){
      let {origin,text,datas} = this.$store.state.diagnose;
      return{
        msg:"诊疗情况",
        imgs:this.$store.state.diagnose.imgSrc,
        // dtoList:origin, //模板数据
        dtoList:datas, //模板数据
        labelDetail:{}, //标签明细
        checkText:text, //选中的文字-Arr
        show:false,
        ppId:null,
        imgUrl:imageUrlPrefix,
        scroll:null
      }
    },
    props:['datas','preName','nextName','modluesLen'],
    mounted(){
      this.$nextTick(()=>{
        let scroll = setScroll(BScroll,true,'.treatper')
        this.scroll = scroll
        scroll.on('scroll', this.onScroll)
      })
    },
    methods:{
      onScroll(data) {
        this.$store.commit('setScroll', data);
        document.activeElement.scrollIntoViewIfNeeded(true);
      },
      beBack(){
        this.$emit('back');
      },
      toNext(){
        if(this.modluesLen==2){
          this.$emit('next','preview')
        }else{
          this.$emit('next');
        }
      },
      getText(){
        let textArr = this.checkText;
        let msg = "";
        for(let k in textArr){
          if(textArr[k] && Array.isArray(textArr[k])){
            let temp = textArr[k];
            for(let j in temp){
              if(temp[j] && temp[j].textP){
                msg += temp[j].textP + '，'
              }
            }
          }else if(textArr[k]&&textArr[k].textP){
            msg += textArr[k].textP + '，'
          }
        }    
        return trimDots(msg.substring(0,msg.length-1));
      },
      updataData(data,id,order,index){//输入框存值
        let list = this.dtoList;
        let value = "";
        let valueP = "";
        for(let i in list){
          if(list[i].id==id){
            if(list[i].controlType == 3){//多行输入
              let detailList = list[i].questionDetailList;
              detailList.splice(index,1,data);
              let temVal = "";
              for(let k in detailList){
                if(detailList[k].value){
                  temVal += detailList[k].value + '，'
                }
              }
              value = valueP = temVal?temVal.substring(0,temVal.length-1):'';
            }else{
              list.splice(i,1,data);
              value = data.value;
              valueP = data.valueP;
            }
          }
        }
        this.$store.commit('setDatas',{type:moduleCP['diagT'],data:data,pId:data.id,ppId:id});
        this.$store.commit('setText',{type:moduleCP['diagT'],text:value.replace(patt,'').replace(/\#\{/g,'').replace(/\}/g,''),textP:valueP.replace(patt,'').replace(/\#\{/g,'').replace(/\}/g,''),pId:data.id,flag:true,order:order}); 
        this.$nextTick(()=>{
          this.scroll.refresh()
        })
      }
    },
    components:{
      UploadImg,
      Label,
      DetailBox,
      Input,
      ComTextArea,
      Radio,
      CheckBox,
      MultiLineInput,
      PickTime
    },
    computed:{
      getItem(){
        return this.$store.state.diagnose.text;
      }
    },
    watch:{
      getItem:{
        handler(value){
          this.checkText = value;
        },
        deep:true
      }
    }
  }
</script>
<style lang="less" scoped>
@import '../less/base.less';
  .treat-wrap{
    font-size: .3rem;
    .quest{
      color: #colors[quest];
      margin-bottom: .36rem;
      font-weight: 700;
    }
    .label{
      .label;
    }
    .result{
      .result;
    }
  }
  .foot{
    .dbfooter;
  }
.footer{
  .footer;
}
.questionImg {
  width: 96%;
}
</style>
