<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <!-- <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px"> -->
          <el-checkbox-group v-model="checkList" @change="checkLists()" >
            <el-checkbox v-for="(result, index) in selectedBmItems" :key="index" :label="result.id" >
              {{ result.name }}
            </el-checkbox>
            <!-- <el-checkbox label="一科" ></el-checkbox> -->
          </el-checkbox-group>
      <!-- </el-form> -->
      <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:'',

      };
    },
    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 = {
        userid: 114,
      };
      const searchParamsa = {
        docid: result.docid,
      };
      // 发送请求到后端
      try {
        const res = await searchPageApi.deptList(searchParams);
        this.selectedBmItems = JSON.parse(JSON.stringify(res.data)); // 假设返回的数据包含 results
        // 获取数据回显
        const res1 = await searchPageApi.docDeptList(searchParamsa);
        console.log('返回值='+res1.data)
        const res1sz = res1.data;
        this.checkList = res1sz;
        console.log('checkList='+this.checkList)
        this.loading = false;
      } catch (error) {
        console.error("搜索失败:", error);
        this.loading = false;
      }
    },
      // 
      checkLists(){

        console.log(this.checkList);

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

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