<template>
  <div class="page_drugsTemplate">
    <van-sticky>
      <div class="hisOrderTitle">
        <i class="iconfont icon-zuojiantou jiantou" @click="close"></i>
        <span class="titleName">项目组套</span>
        <span @click="save">保存</span>
      </div>
    </van-sticky>
    <div>
      <van-tree-select
        @click-nav="clickNav"
        :items="projects"
        :active-id.sync="activeIds"
        :main-active-index.sync="activeIndex"
      />
    </div>
  </div>
</template>


<script>
  export default {
    props: {
      projects: Array,
      src_projects: Array,
      is_show: Boolean,
    },
    data() {
      return {
        activeIds: [],
        activeIndex: 0,
      }
    },
    methods: {
      clickNav(index){
        this.activeIds = []
        for (let i = 0; i < this.projects[index].children.length; i++){
          this.activeIds.push( this.projects[index].children[i].id)
        }
      },
      setData() {
        if (this.projects.length > 0) {
          for (let i =0; i < this.projects[0].children.length; i++){
            this.activeIds.push(this.projects[0].children[i].id)
          }
        }
      },
      save() {

        let all_project = []
        let tempProject = []
        let targetProject = []


        for (let i = 0; i < this.src_projects.length; i++) {
          for (let b = 0; b < this.src_projects[i].project.length; b++) {
            all_project.push(this.src_projects[i].project[b])
          }
        }


        for (let i = 0; i < all_project.length; i++) {
          for (let b = 0; b < this.activeIds.length; b++) {
            if (all_project[i].new_id == this.activeIds[b]) {
              tempProject.push(all_project[i])
            }
          }
        }


        var obj = {}
        tempProject = tempProject.reduce((cur, next) => {
          obj[next.id] ? '' : obj[next.id] = true && cur.push(next)
          return cur
        }, [])


        for (let b = 0; b < tempProject.length; b++) {
          let obj = {
            id: 0,
            project_id: tempProject[b].id,
            project_name: tempProject[b].project_name,
            statistical_classification: tempProject[b].statistical_classification,
            single_dose: tempProject[b].single_dose,
            delivery_way: tempProject[b].delivery_way,
            execution_frequency: tempProject[b].execution_frequency,
            number_days: 1,
            total: 1,
            price: tempProject[b].price,
            remark: "",
            medical_code: tempProject[b].medical_code,
            unit: tempProject[b].unit,
            type:tempProject[b].type,
          }
          targetProject.push(obj)
        }
        this.$emit('add', targetProject)
        this.activeIds = []
        this.close()


      },
      close() {
        this.activeIds = []
        this.activeIndex = 0
        this.$parent.close(4);
      },

    }
  }
</script>
