#include "DeptTaskForm.h"
#include "ui_DeptTaskForm.h"

DeptTaskForm::DeptTaskForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DeptTaskForm)
{
    ui->setupUi(this);

    initForm();
    initFormTable();

    // 绑定信号与槽
    connect(ui->tableDeptTask, &QPagedTable::reloadTablePagedData, this, &DeptTaskForm::getDeptTaskList);
}

DeptTaskForm::~DeptTaskForm()
{
    qDebug() << "~DeptTaskForm";
    delete ui;
}

void DeptTaskForm::showEvent(QShowEvent * event)
{
    ui->tableDeptTask->adjustTableWidth();
    getDeptTaskList(0);
}

void DeptTaskForm::resizeEvent(QResizeEvent * event)
{
    ui->tableDeptTask->adjustTableWidth();
}

void DeptTaskForm::initForm()
{
    // 加载样式表
    QString qssStyle = QssFileUtils::loadQssFileContent(":/qss/infoTable.css");
    if (qssStyle.isEmpty() == false) {
        this->setStyleSheet(qssStyle); // 仅本窗口生效
    }

    // 查询条件的样式
    QList<QWidget *> children = ui->wdgtQueryBox->findChildren<QWidget *>();
    for (QWidget * wdgt : children) {
        wdgt->setProperty("class", "queryBox");

        if (QString(wdgt->metaObject()->className()).contains("QLineEdit")) {
            // 绑定输入框回车事件 只在密码输入框执行
            connect((QLineEdit *)wdgt, &QLineEdit::returnPressed, this, [=](){
                emit ui->btnQuery->clicked();
            });
        }
    }

    // 查询和重置按钮
    ui->btnQuery->setProperty("class", "btnDefault");
    ui->btnResetQuery->setProperty("class", "btnResetQuery");

    ui->tableDeptTask->setTableProperty("class", "defaultTable");

    // 分页组件
    QStringList btnPageGroupUrls;
    btnPageGroupUrls << ":/image/infomation/btnPageFirst.png" << ":/image/infomation/btnPagePre.png"
                     << ":/image/infomation/btnPageNext.png" << ":/image/infomation/btnPageLast.png";
    ui->tableDeptTask->setPageButtonIcons(btnPageGroupUrls);
    ui->tableDeptTask->setPageButtonProperty("class", "pageBtnGroup");

    ui->tableDeptTask->setDataRowHeight(45);
}

void DeptTaskForm::initFormTable()
{
    // 初始化查询条件
    ui->inputStartDate->setDate(QDate::currentDate().addYears(-1));
    ui->inputEndDate->setDate(QDate::currentDate().addMonths(1));

    SysLocalService localServ;
    QList<QMap<QString, QVariant>> deptTaskColumns = localServ.getInfomationTableColumns("deptTask");
    ui->tableDeptTask->setColumns(deptTaskColumns);

    initMeasureStatusSelect();
    initUrgentSelect();

    ui->tableDeptTask->initTableWidget();
    ui->tableDeptTask->initTableHeader();
}

void DeptTaskForm::initMeasureStatusSelect()
{
    ui->selectStatus->addItem("全部状态", "");

    SysLocalService localServ;
    QMap<QString, QString> statusList = localServ.getTaskMeasureStatusDict();

    for (QString key : statusList.keys()) {
        if (key.mid(14, 1) == "1") {
            continue;
        }
        ui->selectStatus->addItem(statusList.value(key), key.mid(14, 1));
    }
    ui->selectStatus->setView(new QListView()); // 使ComboBox下拉选项 的QSS生效的关键代码
}
void DeptTaskForm::initUrgentSelect()
{
    ui->selectUrgent->addItem("全部", "");
    ui->selectUrgent->addItem("不加急", "0");
    ui->selectUrgent->addItem("加急", "1");

    ui->selectUrgent->setView(new QListView()); // 使ComboBox下拉选项 的QSS生效的关键代码
}

void DeptTaskForm::getDeptTaskList(int currPage)
{
    TaskService taskServ;

    // 获取所有查询条件
    TaskRequest request;
    request.userId = ProMemory::getInstance().getLoginUser().value("id").toString();
    request.sampleName = ui->inputDevName->text();
    request.sampleModel = ui->inputDevModel->text();
    request.customerName = ui->inputCustomer->text();
    request.requireStart = ui->inputStartDate->date().toString("yyyy-MM-dd");
    request.requireEnd = ui->inputEndDate->date().toString("yyyy-MM-dd");
    request.isUrgent = ui->selectUrgent->currentData().toString();
    // 选择第一个选项时 条件为空
    if (ui->selectStatus->currentIndex() != 0) {
        request.measureStatusList << ui->selectStatus->currentData().toString();
    } else {
        request.measureStatusList.clear();
    }

    // 查询页
    page.currentPage = currPage;

    // 查询数据库
    QList<TaskDTO> deptTaskList = taskServ.getDeptTaskListPage(request, page);
    QList<QMap<QString, QVariant>> pageData;
    for (TaskDTO task : deptTaskList) {
        QMap<QString, QVariant> item;

        item.insert("id", task.id);
        item.insert("name", task.sampleName);
        item.insert("model", task.sampleModel);
        item.insert("serialNo", task.manufactureNo);
        item.insert("maker", task.manufacturer);
        item.insert("custom", task.customerName);
        item.insert("requireTime", task.requireOverTime);
        item.insert("isUrgent", task.isUrgentName);
        item.insert("orderNo", task.orderNo);
        item.insert("measureStatus", task.measureStatusName);

        pageData.append(item);
    }

    // 设置分页表格数据
    ui->tableDeptTask->setTotalCount(page.totalCount);
    ui->tableDeptTask->setDatas(pageData);

    // 筛选 加急=是 的单元格标红提示
    QList<int> matchedRows = ui->tableDeptTask->filterCells("isUrgent", "是");
    if (!matchedRows.isEmpty()) {
        for (int row : matchedRows) {
            ui->tableDeptTask->getTableInstance()->item(row, 9)->setForeground(QBrush("#FF0000"));
        }
    }
}

void DeptTaskForm::on_btnQuery_clicked()
{
    getDeptTaskList(0);
}

void DeptTaskForm::on_btnResetQuery_clicked()
{
    ui->inputDevName->setText("");
    ui->inputDevModel->setText("");
    ui->inputCustomer->setText("");

    ui->inputStartDate->setDate(QDate::currentDate().addYears(-1));
    ui->inputEndDate->setDate(QDate::currentDate().addMonths(1));

    ui->selectStatus->setCurrentIndex(0);
    ui->selectUrgent->setCurrentIndex(0);

    getDeptTaskList(0);
}
