   <!--  -->
   <template>
        <el-dialog
      title="关联试验划分树"
      class="excelBox"
      :visible.sync="testGLExcel"
      width="500px"
      modal-append-to-body
      append-to-body
      @close="testGLExcelMD"
    
    >
   <div class="mg-b-10">
    <el-alert
        :closable="false"
        title="勾选试验划分节点，被勾选的节点数据将会汇总到当前台账分类中"
        type="warning">
      </el-alert>
   </div>
      <div>
        <el-select
          style="width: 100%"
          v-model="testGLExcelFrom.name"
          placeholder="请选择"
          @change="changetherr"
        >
          <el-option
            v-for="(item, key) in testGLExcelData"
            :key="key"
            :label="item.projectName"
            :value="item.id"
          >
          </el-option>
        </el-select>
        <el-scrollbar style="margin-top: 20px; height: 50vh">
          <el-input
          style="width: 100%"
            v-model.trim="testGLExcelFrom.search"
            placeholder="请输入需要选择的内容"
          ></el-input>
          <el-tree
            :filter-node-method="filterNode222"
            ref="tree"
            class="filter-tree"
            style="margin-top: 10px"
            :props="testGLExcelProps"
            :data="testexceldata"
            node-key="primaryKeyId"
            accordion
            show-checkbox
            @check-change="checkchange"
            v-loading="testGLExcelLoading"
            :default-checked-keys="defaultKeys"
          
          >
          </el-tree>
        </el-scrollbar>
      </div>
      <span
        slot="footer"
        class="dialog-footer"
        style="display: flex; justify-content: center; align-items: center"
      >
        <el-button @click="testGLExcelMD()">取 消</el-button>
        <el-button
          style="margin-left: 30px"
          type="primary"
          v-throttle="2000"
          @click="saveLink()"
          >确 定</el-button
        >
      </span>
    </el-dialog>
   </template>
   
   <script>

import { getProjectList,findProjectTree } from "@/api/manager/projectinfo";
import {matching} from "@/api/tentative/testcollect";
    export default{
        props: {
            testGLExcel: {
                type: Boolean,
                default:false
            },
            setId:{
              type:[String,Number],
              default:''
            },
            trialTreeIds:{
              type:[String,Number],
              default:''//已勾选的数据
            }
        
        },
        data(){
            return{
                testGLExcelLoading:false,
                testGLExcelFrom:{
                id: "",
                name: "",
                search: "", //搜素框舒服的值
                ids: "",
                excelId: "",
                initTableName: "",
                initTableId:""
                },
                testGLExcelData:[],
                testexceldata:[],
                testGLExcelProps: {
                label: "title",
                children: "children",
                // disabled: "hasChildren",
                isLeaf: function (data) {
                let tag = false;
                if (!data.hasChildren) {
                    tag = true;
                }
                if (data.isExistForm == 1) {
                    tag = true;
                }
                return tag;
                },
            },
            defaultKeys:[]

            }
        },
        methods:{
            testGLExcelMD(){
                //弹框关闭事件
                this.testGLExcelFrom.name = "";
                this.testGLExcelFrom.search = "";
                this.testGLExcelFrom.id = "";
                this.testexceldata = [];

                this.testGLExcel = false;
                this.$emit('changemodal');
            },
            async tabLazytreeAll(val,pid) {
                //清表树信息
                this.GLExcelLoading = true;
                const { data: res } = await findProjectTree(val,pid);
                this.testGLExcelLoading = false;
                console.log(res);
                if (res.code === 200 && res.msg === "操作成功") {
                    this.testexceldata = res.data;
                }
             },
            async getList(da) {
                    getProjectList(1, 999).then((res) => {
                      this.testGLExcelData = res.data.data.records;
                    });
                  
             },
            changetherr(val) {
                if (this.testGLExcelFrom.name != "") {
                    this.testGLExcelFrom.search = "";
                    let pid=''
                    this.testGLExcelData.forEach((ele)=>{
                      if(ele.id===val){
                        pid=ele.referenceWbsTemplateIdTrial
                      }
                    })
                    this.tabLazytreeAll(val,pid);
                }
            },
            filterNode222(value,data,node) {
                if(!value){
                    return true;
                    }
                    let level = node.level;
                    let _array = [];//这里使用数组存储 只是为了存储值。
                    this.getReturnNode2(node,_array,value);
                    let result = false;
                    _array.forEach((item)=>{
                    result = result || item;
                    });
                    return result;
             },
            getReturnNode2(node,_array,value){
                let isPass = node.data &&  node.data.title && node.data.title.indexOf(value) !== -1;
                isPass?_array.push(isPass):'';
                if(!isPass && node.level!=1 && node.parent){
                    this.getReturnNode2(node.parent,_array,value);
                }
            },
            checkchange(data1,data2,data3) {
          
            },
            saveLink(){
              let checkIds=this.$refs.tree.getCheckedKeys()
              if(checkIds.length==0){
                this.$message.warning('请选择关联的内容')
                return
              }
              matching({
                classId:this.setId,
                ids:checkIds.join(',')
              }).then(() => {
                  this.$message({
                    type: "success",
                    message: "操作成功!"
                  });
                  this.testGLExcelMD()
                }, error => {
               
                  console.log(error);
                });

            },
        },
        watch:{
            testGLExcel(val){
                if(val){
                    this.getList({
                        current: 1,
                        size: 100000,
                        parentId: 0,
                    });
                }
            },
            "testGLExcelFrom.search"(val) {
                console.log(val);
                if (this.testexceldata) {
                    this.$refs.tree.filter(val);
                }
            },
            setId(val){
                this.setId=val
            },
            trialTreeIds(val){
              this.trialTreeIds=val
              this.defaultKeys=this.trialTreeIds.split(',')
            }
        }
    }
   </script>
   <style lang='scss' scoped>
   </style>