import { Permission } from "@cqsjjb/jjb-common-decorator/permission";
import { Connect } from "@cqsjjb/jjb-dva-runtime";
import { Button, message, Modal, Space } from "antd";
import AddIcon from "zy-react-library/components/Icon/AddIcon";
import Page from "zy-react-library/components/Page";
import Table from "zy-react-library/components/Table";
import useTable from "zy-react-library/hooks/useTable";
import { getLabelName } from "zy-react-library/utils";
import { NS_VEHICLE_APPLY } from "~/enumerate/namespace";

const VEHICLE_APPROVAL_STATUS_ENUM = [
  { bianma: "1", name: "审批中" },
  { bianma: "2", name: "通过" },
  { bianma: "3", name: "已驳回" },
];

function List(props) {
  const { tableProps, getData } = useTable(props["vehicleApplyList"], {
    params: { vehicleBelongType: props.vehicleBelongType || 1 },
  });

  const onDelete = (id) => {
    Modal.confirm({
      title: "删除确认",
      content: "确定要删除吗？",
      onOk: async () => {
        const { success } = await props["vehicleApplyDelete"]({ id });
        if (success) {
          message.success("删除成功");
          getData();
        }
      },
    });
  };

  return (
    <Page isShowAllAction={false}>
      <Table
        toolBarRender={() => (
          <Space>
            {props.permission(props.addBtn || "jgd-io-personal-vehicle-add") && (
              <Button
                icon={(<AddIcon />)}
                type="primary"
                onClick={() => {
                  if (tableProps.dataSource.length >= 2) {
                    message.warning("最多添加两辆车");
                    return;
                  }
                  props.history.push("./add");
                }}
              >
                新增
              </Button>
            )}
          </Space>
        )}
        columns={[
          { title: "车牌号", dataIndex: "licenceNo" },
          { title: "车牌类型", dataIndex: "licenceTypeName" },
          { title: "车辆类型", dataIndex: "vehicleTypeName" },
          {
            title: "访问港区范围",
            dataIndex: "gateLevelAuthArea",
            render: (_, record) => record.gateLevelAuthArea && JSON.parse(record.gateLevelAuthArea).area.map(item => item.value).join("、"),
          },
          { title: "访问开始时间", dataIndex: "visitStartTime" },
          { title: "访问结束时间", dataIndex: "visitEndTime" },
          {
            title: "审批状态",
            dataIndex: "auditFlag",
            render: (_, record) => getLabelName({ list: VEHICLE_APPROVAL_STATUS_ENUM, status: record.auditFlag }),
          },
          {
            title: "车辆状态",
            dataIndex: "statusFlag",
            render: (_, record) => record.statusFlag === 2 ? "正常" : "已拉黑",
          },
          {
            title: "操作",
            fixed: "right",
            width: 350,
            render: (_, record) => (
              <Space>
                {props.permission(props.vehicleRecordsBtn || "jgd-io-personal-vehicle-inout") && (
                  <Button
                    type="link"
                    onClick={() => {
                    // TODO
                      props.history.push(`./vehicleRecords?licenceNo=${record.licenceNo}`);
                    }}
                  >
                    车辆进出记录
                  </Button>
                )}
                {props.permission(props.approvalRecordsBtn || "jgd-io-personal-vehicle-auditrecord") && (
                  <Button
                    type="link"
                    onClick={() => {
                      props.history.push(`./approvalRecords?id=${record.id}`);
                    }}
                  >
                    审批记录
                  </Button>
                )}
                {props.permission(props.viewBtn || "jgd-io-personal-vehicle-show") && (
                  <Button
                    type="link"
                    onClick={() => {
                      props.history.push(`./view?id=${record.id}`);
                    }}
                  >
                    查看
                  </Button>
                )}
                {(record.auditFlag === 3 && props.permission(props.reapplyBtn || "jgd-io-personal-vehicle-reapply")) && (
                  <Button
                    type="link"
                    onClick={() => {
                      props.history.push(`./add?id=${record.id}`);
                    }}
                  >
                    重新申请
                  </Button>
                )}
                {(props.permission(props.delBtn || "jgd-io-personal-vehicle-del") && record.auditFlag ===2) && (
                  <Button
                    type="link"
                    danger
                    onClick={() => {
                      onDelete(record.id);
                    }}
                  >
                    删除
                  </Button>
                )}
              </Space>
            ),
          },
        ]}
        {...tableProps}
      />
    </Page>
  );
}

export default Connect([NS_VEHICLE_APPLY], true)(Permission(List));
