/**
 * 左边树，右边网格明细查询
 * @param {*} saveDataset 保存数据集，命名规范 ds_[数组下标]_[对话框id]
 * @param {*} conf 组件定义配置项
 * @returns 
 */
import { StringUtil } from "@birt/funclib/StringUtil.js";
import { treeEditFilter } from "../compose/tree-edit-filter"

export function newInstance(queryDataset,saveDataset, conf) {
    debugger
    const dlgId = StringUtil.randomString(6);
    // 设置数据集id
    queryDataset.controlId += "_" + dlgId;
    saveDataset.controlId += "_" + dlgId;
    const hcTabsId = "HcTabs_" + dlgId;
    const hcTableId1 = "HcTable1_" + dlgId
    const hcTableId2 = "HcTable2_" + dlgId
    // 编辑过滤树
    let treeEditFilterInsc = treeEditFilter(dlgId, queryDataset.controlId, conf.treeEditFilter);
    console.log('treeEditFilterInsc', treeEditFilterInsc)
    // 查询面板
    let hcQueryPanel1 = false;
    let hcQueryPanel2 = false;
    if (conf.hcQueryPanel1) {
      hcQueryPanel1 = {
            controlName: "HcQueryPanel",
            controlId: "hcQueryPanel1_" + dlgId,
            dataset: saveDataset.controlId,
            style: 'margin: 10px 0;',
            children: []
        }
        // 应用查询面板个性化配置
        Object.assign(hcQueryPanel1, conf.hcQueryPanel1 || {});
    }
    if (conf.hcQueryPanel2) {
      hcQueryPanel2 = {
            controlName: "HcQueryPanel",
            controlId: "hcQueryPanel2_" + dlgId,
            dataset: saveDataset.controlId,
            style: 'margin: 10px 0;',
            children: []
        }
        // 应用查询面板个性化配置
        Object.assign(hcQueryPanel2, conf.hcQueryPanel2 || {});
    }

    // 操作工具条
    let hcFormToolbar = false;
    if (conf.hcFormToolbar) {
        hcFormToolbar = {
            controlName: "HcFormToolbar",
            controlId: "HcFormToolbar_" + dlgId,
            dataset: saveDataset.controlId,
            children: []
        }
        // 应用表单个性化配置
        Object.assign(hcFormToolbar, conf.hcFormToolbar || {});
    }
    // 网格明细
    var hcTable1 = {
        controlName: "HcTable",
        controlId: hcTableId1,
        dataset: saveDataset.controlId,
        height: function (parentHeight) {
            return parentHeight - 10;
        },
        children: [],
    };
    var hcTable2 = {
        controlName: "HcTable",
        controlId: hcTableId2,
        dataset: saveDataset.controlId,
        height: function (parentHeight) {
            return parentHeight - 40;
        },
        children: [],
    };
    // 应用网格个性化配置
    Object.assign(hcTable1, conf.hcTable1 || {});
    Object.assign(hcTable2, conf.hcTable2 || {});
     // 分页工具条
    var hcPagination = {
        controlName: "HcPagination",
        controlId: "HcPagination_" + dlgId,
        pageSizes: [10, 20, 50, 100],
        layout: "total, prev, pager, next, sizes, jumper",
        dataset: saveDataset.controlId,
    };
    
    var extraBtn = {
        controlName: 'HcRow',
        controlId: 'ElRow_extra',
        style: 'position: absolute;z-index:99;right:30px;height: 40px;line-height: 40px;display:flex',
        children: [
        {
          controlName: "HcButton",
          controlId: "HcButton_set",
          title: "列设置",
          icon: "el-icon-setting",
          size: 'small',
          disabled: function () {
            return false
          },
          events: {
            click: function () {
                debugger
              var tabs = this.getRefCompt(hcTabsId).activeTabName
              if (tabs.indexOf('list1') > -1) {
                this.getRefCompt(hcTableId1).$children[0].showMenu('70%', 0)
              } else {
                this.getRefCompt(hcTableId2).$children[0].showMenu('70%', 0)
              }
            }
          }
        },
        {
          controlName: "HcButton",
          controlId: "HcButton_save",
          title: "保存",
          icon: "el-icon-s-claim",
          disabled: true,
          size: 'small',
          events: {
            click: function () {
              var data = {}, __body = []
              var tabs = this.getRefCompt(hcTabsId).activeTabName
              if (tabs.indexOf('list1') > -1) {
                data = this.getWorkBook().dataset[queryDataset.controlId]
                __body = [{
                  modelFilePath: data.modelFilePath,
                  data: data.getDirtyData()
                }]
                // var addData = data.getData()
                // addData.forEach(tem => {
                //   tem.$state = 'rsInsert';
                // })
                // __body = [{
                //   modelFilePath: data.modelFilePath,
                //   data: addData
                // }]
              } else {
                data = this.getWorkBook().dataset[saveDataset.controlId]
                __body = [{
                  modelFilePath: data.modelFilePath,
                  data: data.getDirtyData()
                }]
              }
              if (__body && __body.length > 0) {
                this.axios.post(data.saveUrl, {
                  __body: JSON.stringify(__body)
                }).then((res) => {
                  if (res.status == 200) {
                    this.$message.success('保存成功')
                  } else {
                    this.$message.error(res.msg)
                  }
                });
              } else {
                this.$message.warning('请添加数据')
              }
            }
          },
          mounted() {
              debugger
            let data = this.getWorkBook().dataset[saveDataset.controlId]
            let me = this;
            data.on("dirtyChange", (isDirty) => {
              me.conf.disabled = !isDirty;
            });
          },
        },
        {
          controlName: "HcButton",
          controlId: "HcButton_cancel",
          title: "取消",
          disabled: true,
          icon: 'el-icon-close',
          size: 'small',
          events: {
            click: function () {
              let data = this.getWorkBook().dataset[saveDataset.controlId]
              data.cancel();
            }
          },
          mounted() {
              debugger
            let data = this.getWorkBook().dataset[saveDataset.controlId]
            let me = this;
            data.on("dirtyChange", (isDirty) => {
              me.conf.disabled = !isDirty;
            });
          },
        },
        {
          controlName: "HcButton",
          controlId: "HcButton_del",
          title: "删除",
          icon: 'el-icon-delete',
          size: 'small',
          events: {
            click: function () {
              let data,removeData
              var tabs = this.getRefCompt(hcTabsId).activeTabName
              if (tabs.indexOf('list1') > -1) {
                data = this.getWorkBook().dataset[queryDataset.controlId]
                removeData = this.getRefCompt(hcTableId1).$children[0].elTable().selection;
              }else{
                data = this.getWorkBook().dataset[saveDataset.controlId]
                removeData = this.getRefCompt(hcTableId2).$children[0].elTable().selection;
              }
              if (removeData.length == 0) {
                this.$message.warning('未勾选要删除的选项')
              } else {
                this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                  confirmButtonText: '确定',
                  cancelButtonText: '取消',
                  type: 'warning'
                }).then(() => {
                  removeData.forEach(tem => {
                    tem.$state = 'rsDelete';
                  })
                  var __body = [{
                    modelFilePath: data.modelFilePath,
                    data: removeData
                  }]
                  this.axios.post(data.saveUrl, {
                    __body: JSON.stringify(__body)
                  }).then((res) => {
                    if (res.status == 200) {
                      this.$message.success('删除成功')
                      data.remove(removeData)
                    } else {
                      this.$message.error(res.msg)
                    }
                  });
                })
              }
            },
          },
        }
        ]
      }

    var hcTabs = {
        controlName: "HcTabs",
        controlId: hcTabsId,
        tabPosition: "top",
        activeTabName: "list1_" + dlgId,
        height: function (parentHeight) {
            debugger
            return parentHeight - 120;
        },
        // type: "border-card",
        children: [
            {
                controlName: "HcTabPane",
                controlId: "HcTabPane_list1_" + dlgId,
                label: "机构列表",
                name: "list1_" + dlgId,
                children: []
                    .concat(hcQueryPanel1 ? hcQueryPanel1 : [])
                    .concat(hcFormToolbar ? hcFormToolbar : [])
                    .concat(hcTable1)
                    .concat(hcPagination)
            },
            {
                controlName: "HcTabPane",
                controlId: "HcTabPane_list2_" + dlgId,
                label: "人员列表",
                name: "list2_" + dlgId,
                children: []
                .concat(hcQueryPanel2 ? hcQueryPanel2 : [])
                    .concat(hcFormToolbar ? hcFormToolbar : [])
                    .concat(hcTable2)
                    .concat(hcPagination)
            }
        ],
        slot: [].concat(extraBtn)
    }
    // 应用报表模型
    var birtWorkBook = Object.assign({
        controlName: "BirtWorkBook",
        controlId: "BirtWorkBook_" + dlgId,
        showToolBar: false,
        totalPage: 1,
        renderType: "pages",
        height: function (parentHeight) {
            debugger
            return parentHeight - 10;
        },
    }, conf.birtWorkBook || {})
    // 添加布局结构
    birtWorkBook.children = [
        {
            controlName: "BirtSheet",
            controlId: "BirtSheet_" + dlgId,
            name: "sheet_" + dlgId,
            pageIndex: 0,
            dataSets: [queryDataset,saveDataset],
            children: [
                {
                    controlName: "BirtFormSheet",
                    controlId: "BirtFormSheet_" + dlgId,
                    children: [
                        {
                            controlName: "HcRow",
                            gutter: 10,
                            children: [
                                treeEditFilterInsc,
                                {
                                    controlName: "HcCol",
                                    controlId: "HcCol_01_right",
                                    span: 18,
                                    children: []
                                    .concat(hcTabs)
                                }
                            ]
                        }
                    ]
                }
            ]
        }
    ]
    return birtWorkBook
}