<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>

      <!-- <el-tree :data="bmDataList" @check="getCheckedKeys()" show-checkbox node-key="id" ref="tree" default-expand-all
        highlight-current :default-expanded-keys="bmmrzkDataList" :default-checked-keys="bmmrKeyDataList"
        :props="defaultProps">
      </el-tree> -->
      <el-tree :data="bmDataList" @check="getCheckedKeys()" show-checkbox node-key="id" ref="tree"
        highlight-current  :default-checked-keys="bmmrKeyDataList"
        :props="defaultProps">
      </el-tree>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">分 发</el-button>
        <!-- <el-button @click="reset">重 置</el-button> -->
        <el-button @click="dialogVisible = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as searchPageApi from '@/api/manager/digsearch';
  import Editor from '@/components/Editor';
      export default {
    name: "DigsearchForm",
    components: {
          Editor,
                    },
    data() {
      return {
        checkList:[],
        selectedBmItems:[],
        // 弹出层标题
        dialogTitle: "分发到",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,
        docid:'',
        sourcename:'',
        checkedNodes: '',
        bmmrzkDataList:[],
        bmmrKeyDataList:[],
        bmDataList: [{
          id: 1,
          label: '一级 1',
          children: [{
            id: 4,
            label: '二级 1-1',
            children: [{
              id: 9,
              label: '三级 1-1-1'
            }, {
              id: 10,
              label: '三级 1-1-2'
            }]
          }]
        }, {
          id: 2,
          label: '一级 2',
          children: [{
            id: 5,
            label: '二级 2-1'
          }, {
            id: 6,
            label: '二级 2-2'
          }]
        }],
        defaultProps: {
          children: 'children',
          label: 'label'
        }
      };
    },
    methods: {
      /** 打开弹窗 */
    async open(result) {
        this.getBmList(result);
        this.dialogVisible = true;
        // this.reset();
        // 修改时，设置数据
        if (this.selectedBmItems) {
          this.formLoading = true;
          try {
            // console.log('result='+result)
            this.docid = result.docid;
            this.sourcename = result.sourcename;
          } finally {
            this.formLoading = false;
          }
        }
    },
 // 获取部门列表
    async getBmList(result){
      this.loading = true;
      // 拼接查询参数
      const searchParams = {
        // deptId: 114,
        deptId: 111,
      };
      const searchParamsa = {
        docid: result.docid,
      };
      // 发送请求到后端
      try {
        const res = await searchPageApi.deptKeyList(searchParams);
        // this.selectedBmItems = JSON.parse(JSON.stringify(res.data)); // 假设返回的数据包含 results
        this.bmDataList = res.data;
        console.log('res=' + this.bmDataList)

        // 获取数据回显
        const res1 = await searchPageApi.docDeptList(searchParamsa);
        // console.log('返回值='+res1.data)
        const res1sz = res1.data;
        this.bmmrKeyDataList = res1sz;
        console.log('bmmrKeyDataList=' + this.bmmrKeyDataList)
        this.loading = false;
      } catch (error) {
        console.error("搜索失败:", error);
        this.loading = false;
      }
    },
      //
      // checkLists(){

      //   console.log(this.checkList);

      // },
      getCheckedKeys() {
        this.checkedNodes = this.$refs.tree.getCheckedKeys();
        console.log('checkedNodes=' + this.checkedNodes);
      },
      /** 提交按钮 */
      async submitForm() {
        this.formLoading = true;
        // 设置参数
        console.log('length' + this.checkedNodes.length);
        if (this.checkedNodes.length == 0){
          alert('请勾选内容！');
          }else{
            const searchParams = {
            userid: this.checkedNodes,
            docid: this.docid,
            sourcename: this.sourcename,
            };
            // 发送请求到后端
            try {
              // 检查是否有“adv/search:ftp2”权限
              searchPageApi.daiBanFp(searchParams);
              console.log('1')
              // const res1 = await searchPageApi.docDeptList(searchParams);
              // console.log('2')
              // // console.log('返回值='+res1.data)
              // const res1sz = res1.data;
              // console.log('3')
              // this.bmmrKeyDataList = res1sz;
              this.loading = false;
              this.dialogVisible = false;
              this.$message({
                  message: '分发成功！',
                  type: 'success'
              });
            } catch (error) {
              console.error("分发失败:", error);
              this.loading = false;
            }
        }


      },
                      /** 表单重置 */
      reset() {
        this.bmmrKeyDataList = [];
        // this.resetForm("formRef");
      },
    }
  };
</script>
