#include "JobTicketsModel.h"

#include <QQmlEngine>

JobTicketModel* JobTicketModel::pInstance = nullptr;

JobTicketModel::JobTicketModel(QObject* parent)
    : QAbstractListModel(parent)
{

}

JobTicketModel::~JobTicketModel()
{
    m_dataList.clear();
}

JobTicketModel *JobTicketModel::instance()
{
    if (!pInstance) {
        pInstance = new JobTicketModel(nullptr);
    }

    return pInstance;
}

JobTicketModel *JobTicketModel::create(QQmlEngine *engine, QJSEngine *)
{
    engine->setObjectOwnership(instance(), QQmlEngine::CppOwnership);
    return instance();
}

void JobTicketModel::append(const JobTicket &data)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    m_dataList.append(data);
    endInsertRows();
}

void JobTicketModel::clear()
{
    beginResetModel();
    m_dataList.clear();
    endResetModel();
}

int JobTicketModel::rowCount(const QModelIndex &parent) const
{
    if (parent.isValid()) {
        return 0;
    }
    return m_dataList.count();
}

QHash<int, QByteArray> JobTicketModel::roleNames() const
{
    QHash<int, QByteArray> roles;

    roles[MachineryIconCharacter] = "machineryIconCharacter";
    roles[SopIconCharacter] = "sopIconCharacter";
    roles[WorkStationIcon] = "workstationIcon";
    roles[TimeIcon] = "timeIcon";
    roles[WorkingStatusIcon] = "workingStatusIcon";
    roles[UserIconCharacter] = "userIconCharacter";

    roles[WorkId] = "workId";
    roles[NodeId] = "nodeId";
    roles[OrderNo] = "orderNo";
    roles[Name] = "name";
    roles[UrgencyLevel] = "urgencyLevel";
    roles[CompletionTime] = "completionTime";
    roles[CancellationTime] = "cancellationTime";
    roles[CancellationReason] = "cancellationReason";
    roles[InitiatorName] = "initiatorName";
    roles[WorkerUserName] = "workerUserName";
    roles[WorkTime] = "workTime";
    roles[CurrentNodeId] = "currentNodeId";
    roles[CurrentNodeName] = "currentNodeName";
    roles[ApprovalStatus] = "approvalStatus";
    roles[ApprovalStatusColor] = "approvalStatusColor";
    return roles;
}

QVariant JobTicketModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() > m_dataList.count()) {
        return QVariant();
    }

    const JobTicket& data = m_dataList[index.row()];

    if (role == MachineryIconCharacter) {
        return data.m_machineryIconCharacter;
    }
    else if (role == SopIconCharacter) {
        return data.m_sopIconCharacter;
    }
    else if (role == WorkStationIcon) {
        return data.m_workstationIcon;
    }
    else if (role == TimeIcon) {
        return data.m_timeIcon;
    }
    else if (role == WorkingStatusIcon) {
        return data.m_workingStatusIcon;
    }
    else if (role == UserIconCharacter) {
        return data.m_userIconCharacter;
    }
    else if (role == WorkId) {
        return data.m_workId;
    }
    else if (role == NodeId) {
        return data.m_nodeId;
    }
    else if (role == OrderNo) {
        return data.m_orderNo;
    }
    else if (role == Name) {
        return data.m_name;
    }
    else if (role == UrgencyLevel) {
        return data.m_urgencyLevel;
    }
    else if (role == CompletionTime) {
        return data.m_completionTime;
    }
    else if (role == CancellationTime) {
        return data.m_cancellationTime;
    }
    else if (role == CancellationReason) {
        return data.m_cancellationReason;
    }
    else if (role == InitiatorName) {
        return data.m_initiatorName;
    }
    else if (role == WorkerUserName) {
        return data.m_workerUserName;
    }
    else if (role == WorkTime) {
        return data.m_workTime;
    }
    else if (role == CurrentNodeId) {
        return data.m_currentNodeId;
    }
    else if (role == CurrentNodeName) {
        return data.m_currentNodeName;
    }
    else if (role == ApprovalStatus) {
        return data.m_approvalStatus;
    }
    else if (role == ApprovalStatusColor) {
        return data.m_approvalStatusColor;
    }

    return QVariant();
}

int JobTicketModel::runningCount() const
{
    int count = 0;
    for (const JobTicket& ticket : m_dataList) {
        if (ticket.m_approvalStatus == "进行中") {
            count++;
        }
    }
    return count;
}

int JobTicketModel::getFirstRunningNodeId() const
{
    for (const JobTicket& ticket : m_dataList) {
        if (ticket.m_approvalStatus == "进行中") {
            return ticket.m_nodeId;
        }
    }
    return -1;
}

int JobTicketModel::getFirstRunningIndex() const
{
    for (int i = 0; i < m_dataList.count(); i++) {
        if (m_dataList[i].m_approvalStatus == "进行中") {
            return i;
        }
    }
    return -1;
}

QVariantMap JobTicketModel::getSingleJobTicket() const
{
    QVariantMap jobTicket;

    for (int i = 0; i < m_dataList.count(); i++) {
        if (m_dataList[i].m_approvalStatus == "进行中") {
            JobTicket ticket = m_dataList[i];
            jobTicket.insert("nodeId", QString::number(ticket.m_nodeId));
            jobTicket.insert("name", ticket.m_name);
            jobTicket.insert("orderNo", ticket.m_orderNo);
            jobTicket.insert("approvalStatus", ticket.m_approvalStatus);
            jobTicket.insert("workTime", ticket.m_workTime);
            jobTicket.insert("workerUserName", ticket.m_workerUserName);
            jobTicket.insert("currentNodeName", ticket.m_currentNodeName);
            return jobTicket;
        }
    }

    return jobTicket;
}
