import React, { useEffect, useState, useCallback } from "react";
import { DataGrid, Menu, Checkbox } from "@ttk/component";
import { useData, useActions, useCommit } from "@ttk/app-loader";
export default function RoleTableArea(props) {
  const commit = useCommit();
  const actions = useActions(props);
  const { loading } = useData([props, "showState"]).toJS();
  const roleData = useData([props, "roleData"]).toJS();
  const [selectAll, setSelectAll] = useState(false);
  const [rowSelection, setRowSelection] = useState(false);
  const tableThead = [
    {
      dataIndex: "selected",
      title: "checkbox",
      width: 30,
    },
    {
      dataIndex: "roleName",
      title: "角色名称",
      width: 120,
    },
    {
      dataIndex: "roleType",
      title: "角色类型",
      width: 150,
    },
    {
      dataIndex: "remark",
      title: "备注",
      width: 180,
    },
  ];
  //选中某行
  const onRowSelection = useCallback(
    (id, value) => {
      roleData[id].selected = value.selected;
      roleData[id].rowIndex = id;
      commit([props, "roleData"], { type: "update-item", data: roleData[id] });
      var newArr = roleData.filter(function (obj) {
        return obj.selected === true;
      });
      var count = newArr.length;
      if (count === roleData.length) {
        setSelectAll(value.selected);
      } else {
        setSelectAll(false);
      }
    },
    [roleData]
  );
  //全选
  const onSelectAll = useCallback(
    (value) => {
      //行的CheckBox，全选或全不选
      value === true ? setRowSelection(value) : setRowSelection(!value);
      setSelectAll(value);
      commit([props, "roleData"], { type: "selectall", data: value });
    },
    [roleData]
  );
  return (
    <React.Fragment>
      <DataGrid
        defaul={true}
        maxHeight={600}
        loading={loading}
        pagination={false}
        rowsCount={roleData.length}
        rowHeight={45}
        headerHeight={50}
        readonly={false}
        columns={tableThead.map((item, index) => {
          return (
            <DataGrid.Column
              isResizable={true}
              width={item.width}
              flexGrow={index === 0 ? 1 : index === 1 ? 2 : 1}
              fixed={index > 1 ? false : true}
              columnKey={item.dataIndex}
              key={index}
              header={
                <DataGrid.Cell key={index}>
                  {item.title === "checkbox" ? (
                    <Checkbox
                      onChange={(e) => onSelectAll(e.target.checked)}
                      checked={selectAll}
                    ></Checkbox>
                  ) : (
                    item.title
                  )}
                </DataGrid.Cell>
              }
              cell={({ rowIndex, ...props }) => {
                switch (item.dataIndex) {
                  case "selected":
                    return (
                      <DataGrid.Cell
                        key={index + rowIndex}
                        style={{ textAlign: "center" }}
                      >
                        <Checkbox
                          onChange={(e) =>
                            onRowSelection(rowIndex, {
                              selected: e.target.checked,
                            })
                          }
                          checked={roleData[rowIndex][item.dataIndex]}
                        />
                      </DataGrid.Cell>
                    );
                  default:
                    return (
                      <DataGrid.TextCell
                        height={40}
                        key={index + rowIndex}
                        value={roleData[rowIndex][item.dataIndex]}
                      />
                    );
                }
              }}
            ></DataGrid.Column>
          );
        })}
      ></DataGrid>
    </React.Fragment>
  );
}
