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 RolePostList = useData([props, "RolePostList"]).toJS();
  const [selectAll, setSelectAll] = useState(false);
  const [rowSelection, setRowSelection] = useState(false);
  const tableThead = [
    {
      dataIndex: "selected",
      title: "checkbox",
      width: 30,
    },
    {
      dataIndex: "postId",
      title: "岗位编码",
      width: 100,
    },
    {
      dataIndex: "postName",
      title: "岗位名称",
      width: 100,
    },
    {
      dataIndex: "entitleInsLevelName",
      title: "有权部门层级",
      width: 100,
    },
    {
      dataIndex: "busTypeId",
      title: "业务类型",
      width: 100,
    },
    {
      dataIndex: "isValid",
      title: "状态",
      width: 100,
    },
    {
      dataIndex: "remark",
      title: "备注",
      width: 180,
    },
  ];
  //选中某行
  const onRowSelection = useCallback(
    (id, value) => {
      RolePostList[id].selected = value.selected;
      RolePostList[id].rowIndex = id;
      commit([props, "RolePostList"], {
        type: "update-item",
        data: RolePostList[id],
      });
      var newArr = RolePostList.filter(function (obj) {
        return obj.selected === true;
      });
      var count = newArr.length;
      if (count === RolePostList.length) {
        setSelectAll(value.selected);
      } else {
        setSelectAll(false);
      }
    },
    [RolePostList]
  );
  //全选
  const onSelectAll = useCallback(
    (value) => {
      //行的CheckBox，全选或全不选
      value === true ? setRowSelection(value) : setRowSelection(!value);
      setSelectAll(value);
      commit([props, "RolePostList"], { type: "selectall", data: value });
    },
    [RolePostList]
  );
  return (
    <React.Fragment>
      <DataGrid
        defaul={true}
        maxHeight={600}
        loading={loading}
        pagination={false}
        rowsCount={RolePostList.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={RolePostList[rowIndex][item.dataIndex]}
                        />
                      </DataGrid.Cell>
                    );
                  default:
                    return (
                      <DataGrid.TextCell
                        height={40}
                        key={index + rowIndex}
                        value={RolePostList[rowIndex][item.dataIndex]}
                      />
                    );
                }
              }}
            ></DataGrid.Column>
          );
        })}
      ></DataGrid>
    </React.Fragment>
  );
}
