
#include "postSearchListModel.h"
#include <QPixmap>
#include <QImageReader>
#include <QDebug>
#include <QUrl>

postSearchListModel::postSearchListModel(const QStringList &nameString, const QStringList &areaString, const QStringList &sizeString, const QStringList &roomString, const QStringList &priceString, const QStringList &pricePerSizeString, const QStringList &strings, const QStringList &suiteString,const QPixmap p[],QObject *parent) :
        QAbstractListModel(parent), stringList(strings), nameString(nameString),areaString(areaString), roomString(roomString),priceString(priceString),pricePerSizeString(pricePerSizeString),sizeString(sizeString),suiteString(suiteString)
{
    qDebug()<<"1";
    for(int i=0;i<nameString.size();i++){_p[i]=p[i];qDebug()<<"1.5";}
    qDebug()<<"1.5";

}

int postSearchListModel::rowCount(const QModelIndex &parent) const
{
    qDebug()<<"1.6";
    return nameString.count();
}

QVariant postSearchListModel::data(const QModelIndex &index, int role) const
{
    qDebug()<<"1.7";
    if ( (!index.isValid()) || (index.row() >= stringList.size()))
        return QVariant();

    qDebug()<<"1.8";
    if(role == Qt::UserRole)
    {
        qDebug()<<"2";
        QString string = nameString.at(index.row());
        string.replace("‧",".");
        return string;
    }
    else if( role == Qt::UserRole+1)
    {

        return _p[index.row()];
    }
    else if( role == Qt::UserRole+2)
    {
        qDebug()<<"3";
        QImageReader reader;
        reader.setFileName(":/new/prefix1/Image/arrow_right.png");
        QSize imageSize = reader.size();
        imageSize.scale(QSize(20,20), Qt::KeepAspectRatio);
        reader.setScaledSize(imageSize);
        QImage image = reader.read();
        QPixmap pixmap = QPixmap::fromImage(image);

        return pixmap;
    }
    else if( role == Qt::UserRole+3)
    {
        qDebug()<<"4";
        return areaString.at(index.row());
    }
    else if( role == Qt::UserRole+4)
    {
        QString tmp = sizeString.at(index.row());
//        tmp.remove(tmp.length()-3,tmp.length());
//        tmp.append("呎");
        return tmp;
    }
    else if( role == Qt::UserRole+5)
    {
        qDebug()<<"5";
        QString tmp = roomString.at(index.row());
//        tmp.append("房");
//        QString tmp2 = suiteString.at(index.row());
//        tmp2 = tmp2.trimmed();
//        int tmp3 = tmp2.toInt();

//        if(tmp3 > 0);
//        {
//            qDebug()<<"I am ok! :"<<tmp3;
////        tmp.append("(");
////        tmp.append(tmp2);
////        tmp.append("套房)");
//        }
       return tmp;
    }
    else if( role == Qt::UserRole+6)
    {
        qDebug()<<"6";
        QString tmp = priceString.at(index.row());
//        tmp.remove(tmp.length()-3,tmp.length());
//        int tmp2 = tmp.toInt();
//        tmp.prepend("$");
//        if(tmp2 > 100000)
//        {
//        tmp2 = tmp2/10000;
//        tmp = tmp.setNum(tmp2);
//        if(tmp2>999)
//        {

//        tmp.insert(1,",");
//        }

//        tmp.append("萬");
//        tmp.prepend("售");
//        }
//        else
//        {
//            tmp.prepend("租");
//            if(tmp2 > 9999)
//                tmp.insert(3,",");
//            else if(tmp2 > 999)
//                tmp.insert(2,",");
//        }

        return tmp;
    }
    else if( role == Qt::UserRole+7)
    {
        qDebug()<<"7";
        QString tmp = pricePerSizeString.at(index.row());
        tmp.remove(tmp.length()-5,tmp.length());
        int tmp2 = tmp.toInt();
        if(tmp2 > 9999)
            tmp.insert(2,",");
        else if(tmp2 > 999)
            tmp.insert(1,",");
        tmp.prepend("($");
        tmp.append("/呎)");
        return tmp;
    }
    return QVariant();
}



void postSearchListModel::setStrings(const QStringList & strings)
{
    qDebug()<<"bulala";
    stringList = strings;
    reset();
}
