import {Button, Table} from "antd";
import {useEffect, useState} from "react";
import ModalHooks from "./components/modal.jsx";
import "./rankingOfPassengerVolume.scss";
import axios from "@/utils/axios.js";
import DeleteButton from "@/components/delete.jsx";

const {Column} = Table;

export default function RankingOfPassengerVolume() {
    // 页面数据
    const [data, setData] = useState([]);
    // 是否展示弹框
    const [isModalOpen, setIsModalOpen] = useState(false);
    // 一行的数据
// 线路下拉框
    const [option, setOption] = useState([])
    const [row, setRow] = useState({});
    // 设置下拉框绑定的值
    const [val, setVal] = useState('')


    // 新增用户页面
    function addUser() {
        setRow([]);
        setIsModalOpen(true);
    }

    // 关闭页面
    function closeModal() {
        setIsModalOpen(false);
        getData()
    }

    // 点击修改的回调
    function modify(data) {
        setRow(data);
        setIsModalOpen(true);

    }


    // 获取数据
    async function getData() {

        let {data} = await axios.get("/trafficAnalysis/getTrafficFlowRank")

        setData(data);

    }

    // 设置类型文字
    function setType(text) {
        switch (text) {
            case 1:
                return "行驶中";
            case 2:
                return "维保";
            case 3:
                return "固定包车";
            case 4:
                return "临时包车";
            case 5:
                return "充电";
            case 6:
                return "公务";

        }
    }

    /**
     * 线路数据请求
     * @returns {Promise<void>}
     */
    async function getLineData() {

        let {data} = await await axios.get("/busLine/getList", {params: {hasModel: 1}})
        let option = data.map(item => {
            return {
                label: item.lineName,
                value: item.lineId,
            }

        })

        setOption(option)

    }

    useEffect(() => {
        getData()
        getLineData()
    }, [])
    return (
        <div className="passenger">
            <div className="top">

                <div className="button">
                    <Button type="primary" onClick={addUser}>
                        新增客运量排名
                    </Button>
                </div>
            </div>
            <Table
                dataSource={data}
                bordered={true}
                rowKey="id"
                pagination={false}
                scroll={{y: 700}}
            >
                <Column title="线路" dataIndex="lineName" key="id"></Column>
                <Column title="客运量（万人)" dataIndex="trafficVolume" key="id"></Column>


                <Column
                    title="操作"
                    width="170px"
                    dataIndex="id"
                    key="id"
                    render={(text, record) => (
                        <div className="btn">
                            <Button type="primary" onClick={() => modify(record)}>
                                修改
                            </Button>
                            <DeleteButton url='/trafficAnalysisEdit/delTrafficFlowRank' id={text}
                                          getData={getData}></DeleteButton>
                        </div>
                    )}
                />
            </Table>
            <ModalHooks
                isModalOpen={isModalOpen}
                closeModal={closeModal}
                row={row}
                option={option}
                val={val}
            ></ModalHooks>
        </div>
    );
}
