<template>
  <div class="tab-wrap">
    <div class="tab"
      v-if="modlues&&modlues.length>0 && moduleShow">
      <p v-for="(it,index) in modlues"
        :style="{'width':1/modlues.length*100+'%'}"
        :key="it.id"
        @click="clickTab(it,index)">
        <span :class="{current:index==number,comp:finish[it.type]}" v-if="type[it.type]==1">
          <i>{{index+1}}</i>
          {{it.name}}
        </span>
      </p>
    </div>
    <!-- 内容 -->
    <div class="content">
      <template v-for="(item,idx) in modlues">
        <Symptom
          v-if="flag==moduleCP['symp']&&item.type==moduleCP['symp']&&moduleShow"
          @next="toNext"
          :nameStr="item.name"
          :modluesLen="modluesLen"
        />
      
        <DiagTreat
          v-if="flag==moduleCP['diagT']&&item.type==moduleCP['diagT']&&moduleShow"
          :datas="item"
          :preName="modlues[0]&&modlues[0].name"
          :modluesLen="modluesLen"
          :nextName="modlues[2]&&modlues[2].name"
          @next="toNext"
          @back="beBack"
        />

        <Others
          v-if="flag==moduleCP['other']&&item.type==moduleCP['other']&&moduleShow"
          :modluesLen="modluesLen"
          :datas="item"
          :preName="modlues[idx-1]&&modlues[idx-1].name"
          :nextName="modlues[idx+1]&&modlues[idx+1].name"
          @next="toNext"
          @back="beBack"
        />
        <AddContent
          v-if="flag == moduleCP['suplement']&&item.type==moduleCP['suplement']&&moduleShow"
          :modluesLen="modluesLen"
          :allMoudles="item"
          :preName="modlues[idx-1]&&modlues[idx-1].name"
          @toggleModule="toggleModule"
          @next="toNext"
          @back="beBack"
        />
      </template>
      <Preview v-if="!moduleShow" 
        :preName="getPreviewName()"
        :loadingShow="this.$store.state.loadingShow"
        :showLis="type"
        @back="beBack" 
      />
    </div>
    <!-- 详情页 -->
    <div class="detail" v-if="this.$store.state.detailShow">
    <DetailBox />
    </div>
  </div>
</template>
<script type="text/javascript">
import Symptom from './Symptom.vue';
import AddContent from './AddContent.vue';
import DiagTreat from './DiagTreat.vue';
import Others from './Others.vue';
import Preview from './Preview.vue';
import {moduleConfig,moduleCP} from '../utils/tools.js';
import $ from 'jquery';
import DetailBox from './DetailBox.vue';
export default {
  name: 'TabPage',
  data() {
    let sysConfig = this.$store.state.sysConfig;
    let tmpAllMoudles = this.$store.state.allMoudles;
    let allMoudles = moduleConfig(sysConfig,tmpAllMoudles);
    const { choose} = this.$store.state.symptom;
    return {
      config: sysConfig,
      modlues: allMoudles.data,
      modluesLen: allMoudles.len,
      type: {
        "1": sysConfig.length > 0 && sysConfig.filter(item => item.code == "symptoms_show")[0].value,
        "51": sysConfig.length > 0 && sysConfig.filter(item => item.code == "diagnosis_show")[0].value,
        "3": sysConfig.length > 0 && sysConfig.filter(item => item.code == "omhistory_show")[0].value,
        "52": sysConfig.length > 0 && sysConfig.filter(item => item.code == "replenish_show")[0].value,
      },
      flag: allMoudles.order,
      moduleShow:true,
      finish:{ //标识已填
        '1':true,
        '51':false,
        '3':false,
        '52':false
      },
      number:0,
      moduleCP:moduleCP,
      symChoose:choose
    }
  },
  created() {
    if (this.config.length == 0) {//登录进去返回到列表页，扫码返回到开始页
      let scan = localStorage.getItem('scan');
      let params = JSON.parse(localStorage.getItem('infoParam'));
      let param = JSON.parse(localStorage.getItem('loginParam'));
      let data = JSON.parse(localStorage.getItem('loginData'));
      if(!scan){
        if(data.length>1){
          this.$router.replace({path:'/department'});
        }else{
          this.$router.replace({path:'/home',query:param});
        }
      }else{
        this.$router.replace({path:'/home',query:Object.assign({}, params,{scan:true})});
      }
    }
  },
  methods: {
    // 症状情况有内容后才可以点击tab栏
    clickTab(item,index){
      if(this.symChoose.length>0){
        this.number = index;
        this.flag = item.type;
        this.finish[parseInt(item.type)] = true;
      }
    },
    getPreviewName(){
      let len = this.modluesLen,tmpModule = this.modlues;
      let preName = '';
      if(len == 1){
        preName=tmpModule[0].name
      }else if(len == 2){
        preName=tmpModule[1].name
      }else if(len == 3){
        preName=tmpModule[2].name
      }else if(len == 4){
        preName=tmpModule[3].name
      }
      return preName
    },
    getNames(type){
      let len = this.modluesLen,tmpModule = this.modlues;
      let nextName = '';
      if(len == 1){
        preName=tmpModule[0].name
      }else if(len == 2){
        preName=tmpModule[1].name
      }else if(len == 3){
        preName=tmpModule[2].name
      }else if(len == 4){
        preName=tmpModule[3].name
      }
      return preName
    },
    toNext(preview) {
      if(preview === 'preview'){
        this.moduleShow=false
        return
      }
      let flag = parseInt(this.flag),num=this.number;
      this.number = ++num;

      for(let i = 0;i < this.modlues.length;i++){
        let part = this.modlues[i];
        if(part.type == flag){
          if(i < (this.modlues.length-1)){
            this.flag = this.modlues[i+1].type
          }
        }
      }
      
      for(let j in this.finish){
        if(j == this.flag){
          this.finish[j] = true
        }
      }
    },
    beBack(preview) {
      if(preview === 'preview'){
        this.moduleShow=true
        return
      }
      let flag = parseInt(this.flag),num=this.number;
      this.number = --num;

      for(let i = 0;i < this.modlues.length;i++){
        let part = this.modlues[i];
        if(part.type == flag){
          this.flag = this.modlues[i-1].type
        }
      }
      for(let j in this.finish){
        if(j == this.flag){
          this.finish[j] = true
        }
      }
    },
    toggleModule(flg){
      this.moduleShow = flg
    },
  },
  components: {
    Symptom,
    DiagTreat,
    Others,
    AddContent,
    Preview,
    DetailBox
  }
}
</script>
<style lang="less" scoped>
@import '../less/base.less';
.tab-wrap {
  width: 100%;
  height: 100%;
  padding-bottom: 0.88rem;
  padding-top: 0.82rem;
  .tab {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    background: #fff;
    z-index: 101;
    height: 0.82rem;
    line-height: 0.82rem;
    border-bottom: 1px solid #ededed;
    box-sizing: border-box;
    padding-top: 0.16rem;
    p {
      display: inline-block;
      padding: 0 0.2rem;
      box-sizing: border-box;
    }
    span {
      font-size: 0.24rem;
      display: inline-block;
      vertical-align: top;
      width: 100%;
      height: 0.5rem;
      line-height: 0.5rem;
      color: #7c828e;
      text-align: center;
    }
    .current {
      color: #0043e8;
      background: #e5ecfc;
      border-radius: 25px;
    }
    .comp{
      color: #0043e8;
    }
}
  .content {
    font-size: 0.3rem;
    padding: 0.45rem 0 0.45rem 0.3rem;
  }
}
</style>