import tableForm from "@base/components/table/tableForm.vue";
import editView from "@base/views/bd/setting/bd_attach_setting/edit.vue";
import {treeScollx} from "@base/utils/global";
import {getJsxBtn} from "@base/views/bd/setting/utils";

let modules = {};
modules = {
  name: 'bd_attach_setting:list',
  components: {
    tableForm,
    editView
  },
  created() {
  },
  async mounted() {
    treeScollx({target: this, type: 'default'});
    this.initTableList();
  },
  data() {
    return {
      activeName: 'second',
      dataId: 0,
      showEdit: false,
      vxeOption: {},
      checkRows: [],
      szTaMbIds: [],
      formData: {},
      defaultProps: {
        label: 'name', //这里是树结构中需显示的数据（即接口返回的需展示在页面上的参数）
        children: [],
        isLeaf: 'leaf'
      },
      showItemView: false,
      showMenuKindDialog: false,
      currentFormType: {},
      menuKindAuth: {},
      showTree: false
    };
  },
  computed: {
    currentMenuKindId() {
      return this.currentFormType?.id || null
    },
    currentMenuKindName() {
      return this.currentFormType?.name || null
    }
  },
  methods: {
    searchEvent() {
      this.$refs['table-m1'].commitProxy('reload');
    },
    resetEvent() {
      this.formData = {};
      this.checkTags = [];
      this.$refs['table-m1'].commitProxy('reload');
    },
    openEditDialog(row) {
      /*this.dataId = !id || typeof id == 'object' ? 0 : id;
      this.activeName = 'first';
      this.$openEditView('showEdit');*/

      let dataId = row?.id || 0;
      if (!dataId) {
        this.dataId = dataId;
        this.activeName = 'first';
        this.$openEditView('showEdit');
      } else {
        this.$refs.xTabs.openEditTab(row);
      }
    },
    getSearchParam() {
      let currentFormType = this.currentFormType || {}
      return {
        ...this.formData,
        type: "importTemplate",
        menuKindCode: currentFormType.menuKindCode || null
      }
    },
    initTableList() {
      let that = this;
      let tableOption = {
        vue: this,
        tableRef: 'table-m1',
        tableName: 'bd_attach_setting_list-m1',
        path: USER_PREFIX + '/bd_attach_setting/listPage',
        param: () => {
          return this.getSearchParam()
        },
        columns: [
          {type: 'checkbox', width: 48, resizable: false, fixed: 'left'},
          {
            title: this.$t1('模板文件名称'),
            field: 'name',
            width: 180,
            fixed: 'left'
          },
          {
            title: this.$t1('模板文件编码'),
            field: 'code',
            width: 180
          },
          // {title: this.$t1('表单分类'), field: 'menuKindName', width: 150},
          {
            title: this.$t1('备注'),
            field: 'remark',
            width: 250
          },
          {
            field: 'createBy',
            title: this.$t1('创建人'),
            width: 150
          },
          {
            field: 'createDate',
            title: this.$t1('创建时间'),
            width: 150
          },
          {
            field: 'modifyBy',
            title: this.$t1('更新人'),
            width: 150
          },
          {
            field: 'modifyDate',
            title: this.$t1('更新时间'),
            width: 150
          },
          {
            width: 47,
            fixed: 'right',
            title: '',
            sortable: false,
            slots: {
              default: ({row}) => {
                return getJsxBtn({
                  iconName: "el-icon-edit",
                  content: this.$t1('查看'),
                  onclick: () => {
                    this.openEditDialog(row);
                  }
                });
              }
            }
          }
        ],
        searchColumns: [
          /*{
            title: this.$t1('表单分类'),
            field: "menuKindName",
            type: "input",
            common: true,
            disabled: true,
            defaultValueEnabled: false,
            slot: "menuKindName"
          },*/
          {title: this.$t1('模板文件名称'), field: "name", type: "input", common: true},
          {title: this.$t1('模板文件编码'), field: "code", type: "input", common: true},
          {title: this.$t1('备注'), field: "remark", type: "input", common: true},
          {title: this.$t1('创建人'), field: "createBy", type: "input", common: false},
          {title: this.$t1('更新人'), field: "modifyBy", type: "input", common: false},
          {
            title: this.$t1('创建时间'),
            field: "startCreateDate",
            field2: "endCreateDate",
            type: "dateRange",
            common: false,
            widgetType: "datetime",
            valueFormat: "yyyy-MM-dd HH:mm:ss"
          },
          {
            title: this.$t1('更新时间'),
            field: "startModifyDate",
            field2: "endModifyDate",
            type: "dateRange",
            common: false,
            widgetType: "datetime",
            valueFormat: "yyyy-MM-dd HH:mm:ss"
          }
        ],
        /*config: {
          proxyConfig: {
            autoLoad: false
          }
        },*/
      };
      this.$vxeTableUtil.initVxeTable(tableOption).then(opts => {
        this.vxeOption = opts;
        this.showTree = true;
      });
    },
    handleNodeClick(data, node, v) {
      this.handleNodeData(node)
      this.$forceUpdate();
      this.searchEvent();
    },
    // 异步树叶子节点懒加载逻辑
    loadNode(node, resolve) {
      let id = node && node.data && node.data.id ? node.data.id || '' : 0;
      let param = {enabled: true};
      let url = !id ? USER_PREFIX + '/menuKind/getAllList' : USER_PREFIX + '/menuKind/getChildren';
      this.$http({
        aes: true,
        url: url,
        method: 'post',
        data: {
          parent: id,
          enabled: true
        },
        success: res => {
          let rows = res.objx || [];
          rows.forEach(value => {
            value.leaf = !value.hasChild;
          });
          if (node.level === 0) {
            if (rows.length > 0) {
              this.$nextTick(() => {
                let firstNode = node.childNodes[0];
                if (rows[0].hasChild) {
                  firstNode.expand();
                }
                this.$refs.tree.setCurrentKey(firstNode.data);
                this.handleNodeClick(firstNode.data, firstNode, this)
              });
            }
          }
          resolve(rows);
        }
      });
    },
    handleNodeData(node) {
      this.currentFormType = node ? node.data : null;
      this.currentNode = node;
    },
    confirmInsertMenuKind(rows) {
      this.$baseReload();
    },
    moveClick() {
      this.szTaMbIds = [];
      this.$refs["table-m1"].getCheckboxRecords(true).map((item) => {
        this.szTaMbIds.push(item.id);
      });
      this.showMenuKindDialog = true;
    }
  }
};
export default modules
