#include "stable.h"
#include "cmwcalicleancoatfluid.h"
#include "cctrlcombo.h"

CMWCaliCleanCoatFluid::CMWCaliCleanCoatFluid(QWidget *parent)
	: CMWidget(parent),
	 ui(new Ui::CMWCaliCleanCoatFluid)
{
	ui->setupUi(this);
	m_iSelectRow=-1;
}

CMWCaliCleanCoatFluid::~CMWCaliCleanCoatFluid()
{
	 delete ui;
}

void CMWCaliCleanCoatFluid::MInit()
{

	CMWidget::MInit();
}

void CMWCaliCleanCoatFluid::MFresh()
{
	CMWidget::MFresh();
	CProcess_Coat *pCoatClean=0;
	QMMutexVector<COATCLEANSTEP> coatcleansteps;
	pCoatClean=GET_PROCESS(PROCESS_COAT,SM_COAT,
		CProcess_Coat);


	coatcleansteps=pCoatClean->m_vCleanTableRing;
	int isize=coatcleansteps.size();
	ui->tableWidget->clearContents();
	ui->tableWidget->setRowCount(isize);
	for(int i=0;i<isize;i++)
	{
		const COATCLEANSTEP& step=coatcleansteps.at(i);
		QTableWidgetItem* pItem=0;
		pItem=new QTableWidgetItem(CoatName[step.iType]);
		pItem->setData(Qt::UserRole,step.iType);
		ui->tableWidget->setItem(i,0,pItem);
		pItem=new QTableWidgetItem(QString::number(step.dTime,'f',0));
		ui->tableWidget->setItem(i,1,pItem);
		pItem=new QTableWidgetItem(QString::number(step.iRPM));
		ui->tableWidget->setItem(i,2,pItem);
	}
}

void CMWCaliCleanCoatFluid::MApply()
{
	CMWidget::MApply();
	QMMutexVector<COATCLEANSTEP> coatcleansteps;
	int isize=ui->tableWidget->rowCount();
	coatcleansteps.resize(isize);
	on_tableWidget_currentItemChanged(0,ui->tableWidget->currentItem());
	for(int i=0;i<isize;i++)
	{
		COATCLEANSTEP& step=coatcleansteps[i];
		step.iType=ui->tableWidget->item(i,0)->data(Qt::UserRole).toInt();
		step.dTime=ui->tableWidget->item(i,1)->text().toDouble();
		step.iRPM=ui->tableWidget->item(i,2)->text().toInt();
	}
	CProcess_Coat* pCoatClean=0;
	QString filePath;
	pCoatClean=GET_PROCESS(PROCESS_COAT,SM_COAT,
		CProcess_Coat);


	filePath=CMakePath::make_config_path(PATH_CLEANTABLE_COATFLUID);

	pCoatClean->m_vCleanTableRing=coatcleansteps;
	try{
		pCoatClean->ReadWriteCleanCoatLiquid(true);
	}catch(QString& ex){
		m_gSystem.m_SysMsg.SetMsg(ex,NOTIFY_MSG_TYPE);
	}

}

void CMWCaliCleanCoatFluid::MTimeFresh()
{

}

void CMWCaliCleanCoatFluid::on_tableWidget_currentItemChanged(QTableWidgetItem *current, QTableWidgetItem *previous)
{
	if(previous)
	{
		if(previous->column()!=0)
		{
			CCtrlLineEdit* pLineEdit=
				(CCtrlLineEdit*)ui->tableWidget->cellWidget(previous->row(),
				previous->column());
			if(pLineEdit)
			{
				previous->setText(pLineEdit->text());
			}
		}
		else
		{
			CCtrlCombo* pCombo=
				(CCtrlCombo*)ui->tableWidget->cellWidget(previous->row(),
				previous->column());
			if(pCombo)
			{
				previous->setText(pCombo->currentText());
				previous->setData(Qt::UserRole,pCombo->currentData());
			}
		}
		ui->tableWidget->removeCellWidget(previous->row(),
			previous->column());
	}

	if(current&&!m_bReadOnly)
	{
		m_iSelectRow=current->row();

		if(current->column()!=0)
		{
			CCtrlLineEdit* pLineEdit=new CCtrlLineEdit(current->text(),this);
			pLineEdit->SetRange(0,1500,false);
			ui->tableWidget->setCellWidget(current->row(),
				current->column(),
				pLineEdit);
		}
		else
		{
			CCtrlCombo* pCombo=new CCtrlCombo(this);
			for(int i=1;i<MAXCOATTYPE;i++)
			{
				QString s=CoatName[i];
				if(s!= "WATER(SOAK)"&& s!= "WASH" && s!="AIR"&&s!="SPIN")
				{
					continue;
				}
				pCombo->addItem(CoatName[i],i);
			}
			
			pCombo->setCurrentText(current->text());
			ui->tableWidget->setCellWidget(current->row(),
				current->column(),
				pCombo);
		}
	}
}

void CMWCaliCleanCoatFluid::on_pushButton_Insert_clicked()
{
	if(!m_gSystem.IsAllSMStopped())
	{
		return;
	}
	MApply();
	CProcess_Coat* pCoatClean=0;
	COATCLEANSTEP  Step;
	pCoatClean=GET_PROCESS(PROCESS_COAT,SM_COAT,
		CProcess_Coat);
	Step.iType=WATER_SOAK;

	QMMutexVector<COATCLEANSTEP> coatcleansteps=pCoatClean->m_vCleanTableRing;

	int iSize=coatcleansteps.size();
	if (!OUTOFRANGE(m_iSelectRow,0,iSize))
	{
		coatcleansteps.insert(coatcleansteps.begin()+m_iSelectRow+1,Step);
	}
	else
	{
		coatcleansteps.push_back(Step);
	}
	pCoatClean->m_vCleanTableRing=coatcleansteps;
	MFresh();
}

void CMWCaliCleanCoatFluid::on_pushButton_del_clicked()
{
	if(!m_gSystem.IsAllSMStopped())
	{
		return;
	}
	CProcess_Coat *pCoatClean=0;
	pCoatClean=GET_PROCESS(PROCESS_COAT,SM_COAT,
		CProcess_Coat);

	QMMutexVector<COATCLEANSTEP> coatcleansteps=pCoatClean->m_vCleanTableRing;

	int iSize=coatcleansteps.size();
	int iIndex=m_iSelectRow;

	if (!OUTOFRANGE(iIndex,0,iSize))
	{
		coatcleansteps.erase(coatcleansteps.begin()+iIndex);
		pCoatClean->m_vCleanTableRing=coatcleansteps;
		MFresh();
	}
}

void CMWCaliCleanCoatFluid::on_pushButton_Clean_clicked()
{
	if(!m_gSystem.IsAllSMStopped())
	{
        m_gSystem.m_SysMsg.SetMsg
                (QObject::trUtf8("Stop action before test move"));
		return;
	}

	int iStart=ui->lineEdit_start->text().toInt();
	int iEnd=ui->lineEdit_end->text().toInt();
    QString str=QObject::trUtf8("Clean coattable coat fulid sig");
	int iSMID=SM_COAT;
	V_MsgQueue msgqueue;
	msgqueue.pushback_Msg(VarMsg_StartSemi_ID, str,
		QVariantList()<<CLEANCOATFLUID_ACTION<<iStart-1
		<<iEnd-1,0);
    m_gSystem.start_test(iSMID, msgqueue);
}
