import _Tooltip from "antd/es/tooltip";
import _Popover from "antd/es/popover";
import _Table from "antd/es/table";
import _Divider from "antd/es/divider";
import _Button from "antd/es/button";
import _extends from "@babel/runtime/helpers/extends";
import _Checkbox from "antd/es/checkbox";
import _objectWithoutPropertiesLoose from "@babel/runtime/helpers/objectWithoutPropertiesLoose";
var _excluded = ["actionColumnProps", "actionColumnButtons", "wholeStyle", "dataSource", "columns", "listShow", "rowSelection", "style", "rowkey", "totalPage", "pageSize", "tableType", "fixedHeader", "useVirtual", "rowExpandable", "maxBodyHeight", "expandedRowRender", "cellPropsList", "hanshuOnclick"];
/* eslint-disable @typescript-eslint/no-shadow */
import * as React from 'react';
import { useState, useEffect } from 'react';
import { SettingOutlined } from '@ant-design/icons/es/index';
import "./index.css";
import "./index.less";
var antTableReal = function antTableReal(_ref) {
  var actionColumnProps = _ref.actionColumnProps,
    actionColumnButtons = _ref.actionColumnButtons,
    wholeStyle = _ref.wholeStyle,
    dataSource = _ref.dataSource,
    columns = _ref.columns,
    listShow = _ref.listShow,
    rowSelection = _ref.rowSelection,
    style = _ref.style,
    rowkey = _ref.rowkey,
    totalPage = _ref.totalPage,
    pageSize = _ref.pageSize,
    tableType = _ref.tableType,
    fixedHeader = _ref.fixedHeader,
    useVirtual = _ref.useVirtual,
    rowExpandable = _ref.rowExpandable,
    maxBodyHeight = _ref.maxBodyHeight,
    expandedRowRender = _ref.expandedRowRender,
    cellPropsList = _ref.cellPropsList,
    hanshuOnclick = _ref.hanshuOnclick,
    otherProps = _objectWithoutPropertiesLoose(_ref, _excluded);
  var _style = style || {};
  var _otherProps = otherProps || {};
  _otherProps.style = _style;
  var _useState = useState(''),
    ID = _useState[0],
    setID = _useState[1];
  useEffect(function () {
    setID("checkBox_" + uuid());
  }, []);
  var CheckboxGroup = _Checkbox.Group;
  var plainOptions = []; // 渲染的所有多选框
  var defaultCheckedList = []; //默认选择的
  var _useState2 = useState(defaultCheckedList),
    checkedList = _useState2[0],
    setCheckedList = _useState2[1];
  var _useState3 = useState(true),
    indeterminate = _useState3[0],
    setIndeterminate = _useState3[1];
  var _useState4 = useState(false),
    checkAll = _useState4[0],
    setCheckAll = _useState4[1];
  var _loop = function _loop() {
    var _element$filters;
    var element = columns[index];
    plainOptions.push(element.title);
    defaultCheckedList.push(element.title);
    if (element.filters && ((_element$filters = element.filters) === null || _element$filters === void 0 ? void 0 : _element$filters.length) === 0) {
      element.filters = null;
    }
    if (element.sortable && element.sorter.length === 0) {
      element.sorter = function (a, b) {
        if (typeof a[element.dataIndex] === 'number') {
          return a[element.dataIndex] - b[element.dataIndex];
        } else {
          // (a, b) => a.violationTypeStr.slice(0, 1).charCodeAt(0) - b.violationTypeStr.slice(0, 1).charCodeAt(0)
          return a[element.dataIndex].slice(0, 1).charCodeAt(0) - b[element.dataIndex].slice(0, 1).charCodeAt(0);
        }
      };
    }
    if (element.sorter && element.sorter.length === 0) {
      element.sorter = null;
    }
    if (element.render && element.render.length === 0) {
      element.render = null;
    }
    if (element.showTitle) {
      element.ellipsis = {
        showTitle: false
      };
      element.render = function (address) {
        return /*#__PURE__*/React.createElement(_Tooltip, {
          placement: wholeStyle.reminder.placement,
          title: address,
          color: wholeStyle.reminder.background,
          overlayInnerStyle: {
            color: wholeStyle.reminder.color,
            fontWeight: wholeStyle.reminder.weight,
            fontSize: wholeStyle.reminder.size
          }
        }, /*#__PURE__*/React.createElement("span", null, " ", address));
      };
    }
  };
  for (var index = 0; index < (columns === null || columns === void 0 ? void 0 : columns.length); index++) {
    _loop();
  }
  useEffect(function () {
    setCheckedList(defaultCheckedList);
  }, [columns]);
  useEffect(function () {
    if (ID) {
      thead();
      tbody();
      document.getElementById(ID).style.setProperty('--bgeven-color', wholeStyle.tableBody.backgroundTwo);
      document.getElementById(ID).style.setProperty('--bgodd-color', wholeStyle.tableBody.backgroundOne);
      document.getElementById(ID).style.setProperty('--bghover-color', wholeStyle.tableBody.backgroundHover);
    }
  });
  var thead = function thead() {
    var _ref2;
    var elems = (_ref2 = document.getElementById(ID) || document) === null || _ref2 === void 0 ? void 0 : _ref2.querySelectorAll('.ant-table-thead')[0].querySelectorAll('tr')[0].querySelectorAll('th');
    var length = elems.length;
    for (var _index = 0; _index < length; _index++) {
      if (wholeStyle.tableHeader.ifborder) {
        elems[_index].style.borderTop = "1px solid  " + wholeStyle.tableHeader.borderColor;
        if (_index == 0) {
          elems[_index].style.borderLeft = "1px solid  " + wholeStyle.tableHeader.borderColor;
        }
        if (_index == length - 1) {
          elems[_index].style.borderRight = "1px solid  " + wholeStyle.tableHeader.borderColor;
        }
      }
      elems[_index].style.fontSize = wholeStyle.tableHeader.size + 'px'; // 字体大小
      elems[_index].style.fontWeight = wholeStyle.tableHeader.weight; //字体粗细
      elems[_index].style.color = wholeStyle.tableHeader.color; // 字体颜色
      elems[_index].style.borderColor = wholeStyle.tableHeader.borderColor; //边框颜色
      elems[_index].style.backgroundColor = wholeStyle.tableHeader.background; //背景色
      elems[_index].style.height = wholeStyle.tableHeader.height + 'px'; // 行高
      elems[_index].style.paddingLeft = wholeStyle.tableHeader.paddingLeft + 'px'; // 左边距
      elems[_index].style.paddingRight = wholeStyle.tableHeader.paddingRight + 'px'; // 右边距
      elems[_index].style.paddingTop = wholeStyle.tableHeader.paddingTop + 'px'; // 上边距
      elems[_index].style.paddingBottom = wholeStyle.tableHeader.paddingBottom + 'px'; // 下边距
    }
  };

  var tbody = function tbody() {
    var _ref3;
    var elems = (_ref3 = document.getElementById(ID) || document) === null || _ref3 === void 0 ? void 0 : _ref3.querySelectorAll('.ant-table-tbody')[0].querySelectorAll('tr');
    for (var _index2 = 0; _index2 < elems.length; _index2++) {
      var item = elems[_index2].querySelectorAll('td');
      for (var j = 0; j < item.length; j++) {
        if (wholeStyle.tableBody.ifborder) {
          if (j == 0) {
            item[j].style.borderLeft = "1px solid  " + wholeStyle.tableBody.borderColor;
          }
          if (j == item.length - 1) {
            item[j].style.borderRight = "1px solid  " + wholeStyle.tableBody.borderColor;
          }
        }
        item[j].style.fontSize = wholeStyle.tableBody.size + 'px'; // 字体大小
        item[j].style.fontWeight = wholeStyle.tableBody.weight; //字体粗细
        item[j].style.color = wholeStyle.tableBody.color; // 字体颜色
        item[j].style.borderColor = wholeStyle.tableBody.borderColor; //边框颜色
        item[j].style.height = wholeStyle.tableBody.height + 'px'; // 行高
        item[j].style.paddingLeft = wholeStyle.tableBody.paddingLeft + 'px'; // 左边距
        item[j].style.paddingRight = wholeStyle.tableBody.paddingRight + 'px'; // 右边距
        item[j].style.paddingTop = wholeStyle.tableBody.paddingTop + 'px'; // 上边距
        item[j].style.paddingBottom = wholeStyle.tableBody.paddingBottom + 'px'; // 下边距
      }
    }
  };

  var tableProps = {
    bordered: wholeStyle.halvingLine ? wholeStyle.halvingLine : false
  };
  var nameDate = [];
  if (actionColumnButtons && actionColumnButtons.dataSource && actionColumnButtons.dataSource.length !== 0) {
    nameDate.push(_extends({}, actionColumnProps, {
      key: 'action',
      render: function render(_, record) {
        return /*#__PURE__*/React.createElement("div", null, actionColumnButtons.dataSource.map(function (item, index) {
          if (item.verify) {
            if (record[item.verify]) {
              if (!actionColumnButtons.text) {
                return /*#__PURE__*/React.createElement(_Button, _extends({
                  size: actionColumnButtons.size,
                  style: {
                    margin: "0px " + item.margin + "px 0px  0px ",
                    color: item.color
                  },
                  key: index
                }, actionColumnButtons.dataSource[index], {
                  onClick: function onClick() {
                    return actionColumnButtons.dataSource[index].onClick ? actionColumnButtons.dataSource[index].onClick(record) : null;
                  }
                }), item.children);
              } else {
                return /*#__PURE__*/React.createElement(_Button, _extends({
                  size: actionColumnButtons.size,
                  style: {
                    margin: "0px " + item.margin + "px 0px  0px",
                    padding: '0px',
                    color: item.color
                  },
                  key: index
                }, actionColumnButtons.dataSource[index], {
                  onClick: function onClick() {
                    return actionColumnButtons.dataSource[index].onClick ? actionColumnButtons.dataSource[index].onClick(record) : null;
                  },
                  type: item.type == 'primary' ? "link" : "text"
                }), item.children);
              }
            }
          } else {
            if (!actionColumnButtons.text) {
              return /*#__PURE__*/React.createElement(_Button, _extends({
                size: actionColumnButtons.size,
                style: {
                  margin: "0px " + item.margin + "px 0px  0px",
                  color: item.color
                },
                key: index
              }, actionColumnButtons.dataSource[index], {
                onClick: function onClick() {
                  return actionColumnButtons.dataSource[index].onClick ? actionColumnButtons.dataSource[index].onClick(record) : null;
                }
              }), item.children);
            } else {
              return /*#__PURE__*/React.createElement(_Button, _extends({
                size: actionColumnButtons.size,
                style: {
                  margin: "0px " + item.margin + "px 0px  0px",
                  padding: '0px',
                  color: item.color
                },
                key: index
              }, actionColumnButtons.dataSource[index], {
                onClick: function onClick() {
                  return actionColumnButtons.dataSource[index].onClick ? actionColumnButtons.dataSource[index].onClick(record) : null;
                },
                type: item.type == 'primary' ? "link" : "text"
              }), item.children);
            }
          }
        }));
      }
    }));
  }
  var _onChange = function onChange(list) {
    setCheckedList(list);
    setIndeterminate(!!list.length && list.length < plainOptions.length);
    setCheckAll(list.length === plainOptions.length);
  };
  var onCheckAllChange = function onCheckAllChange(e) {
    setCheckedList(e.target.checked ? plainOptions : []);
    setIndeterminate(false);
    setCheckAll(e.target.checked);
  };
  var columnsData = [];
  for (var i = 0; i < checkedList.length; i++) {
    var item1 = checkedList[i];
    for (var j = 0; j < columns.length; j++) {
      var item2 = columns[j];
      if (item1 === item2.title) {
        columnsData.push(item2);
      }
    }
  }
  var content = /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(_Checkbox, {
    indeterminate: indeterminate,
    onChange: function onChange(e) {
      onCheckAllChange(e);
    },
    checked: checkAll
  }, "\u5168\u9009"), /*#__PURE__*/React.createElement(_Divider, null), /*#__PURE__*/React.createElement(CheckboxGroup, {
    options: plainOptions,
    value: checkedList,
    onChange: function onChange(e) {
      _onChange(e);
    }
  }));

  // 行样式设置
  var setRowClassName = function setRowClassName(record, index) {
    var rowColor = index % 2 === 0 ? "oddRowStyl" : "evenRowStyl"; //判断单双行，赋予不同样式
    return rowColor; //赋予行样式
  };

  useEffect(function () {
    if (ID) {
      document.getElementById(ID).style.setProperty('--bgeven-color', wholeStyle.tableBody.backgroundTwo);
      document.getElementById(ID).style.setProperty('--bgodd-color', wholeStyle.tableBody.backgroundOne);
      document.getElementById(ID).style.setProperty('--bghover-color', wholeStyle.tableBody.backgroundHover);
    }
  }, [wholeStyle]);
  return !listShow ? /*#__PURE__*/React.createElement("div", {
    id: ID,
    style: _extends({}, _otherProps.style)
  }, /*#__PURE__*/React.createElement(_Table, _extends({
    className: "antTable"
  }, tableProps, {
    columns: columns.concat(nameDate),
    dataSource: dataSource,
    rowClassName: setRowClassName //设置每行样式
    ,
    pagination: false
  }, _otherProps))) : /*#__PURE__*/React.createElement("div", {
    id: ID,
    style: _extends({}, _otherProps.style)
  }, /*#__PURE__*/React.createElement("div", {
    style: {
      width: '100%',
      display: 'flex',
      justifyContent: 'flex-end'
    }
  }, /*#__PURE__*/React.createElement(_Popover, {
    id: "popover",
    content: content,
    title: "\u8BBE\u7F6E",
    trigger: "focus"
  }, /*#__PURE__*/React.createElement(_Button, {
    icon: /*#__PURE__*/React.createElement(SettingOutlined, null)
  }, "\u64CD\u4F5C"))), /*#__PURE__*/React.createElement(_Table, _extends({
    className: "antTable"
  }, tableProps, {
    columns: columnsData.concat(nameDate),
    dataSource: dataSource,
    rowClassName: setRowClassName //设置每行样式
    ,
    pagination: false
  }, _otherProps)));
};
antTableReal.displayName = 'antTableReal';
export default antTableReal;
export function uuid() {
  return (Math.random() * 1e6 >> 0).toString(36);
}