<?php 
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/1/001
 * Time: 15:23
 */

namespace backend\models\searches;


use backend\models\MailApi;
use yii\data\ArrayDataProvider;
use yii\data\Pagination;
use yii\helpers\Url;

class MailSearch extends DataTable
{
    public function search($params)
    {
        $this->setAttributes($params);
        $this->initSearchParams();
        
        $pagination = new Pagination();
        $pagination->pageSize = $this->pageSize;
        $data['page'] = $this->page;
        $data['pageSize'] = $pagination->getPageSize();

        if ($this->orderBy) {
            $data['orderBy'] = $this->orderBy;
        }
        if (!empty($this->search['value']) && trim($this->search['value']) !== '') {
            $data['search'] = trim($this->search['value']);
        }

        if ($this->pageSize > 1000) {
            if (\Yii::$app->getRequest()->isAjax) {
                \Yii::$app->getResponse()->redirect(Url::current());
            } else {
                $this->exportXls($data);
            }
            \Yii::$app->end();
        }
        
        $mailApi = new MailApi();
        $result = $mailApi->getMailList($data);
        $models = [];
        if ($result['code'] == 1) {
            $models = (array)$result['data']['dataList'];
            $pagination->totalCount = (int)$result['data']['totalCount'];
        }
        $dataProvider = new ArrayDataProvider([
            'models' => $models,
            'pagination' => $pagination,
            'totalCount' => $pagination->totalCount,
        ]);
        return $dataProvider;
    }

    /**
     * @param $data
     */
    private function exportXls($data)
    {
        \Yii::$app->getResponse()->setDownloadHeaders("邮件记录_" . date('Y-m-d') . '.xls')->send();
        if (!ob_get_level()) {
            ob_start();
        }
        echo '<style>.num{mso-number-format:General;} .text{mso-number-format:"\@";/*force text*/}</style>';
        echo '<table border="1" cellpadding="0" cellspacing="0">';
        echo '<tr><th>ID</th><th>类型</th><th>状态</th><th>标题</th><th>发布时间</th></tr>';
        $api = new MailApi();
        $leftSize = $this->pageSize;
        while ($leftSize > 0) {
            // 重新计算页数和分页大小 start
            $pageSize = $leftSize > 1000 ? 1000 : $leftSize;
            $data['pageSize'] = $pageSize;
            $data['page'] = ceil($this->start / $pageSize) + 1;
            // 重新计算页数和分页大小 end
            $result = $api->getMailList($data);
            if ($result['code'] == 1 && $result['data']['dataList']) {
                foreach ((array)$result['data']['dataList'] as $key => $row) {
                    $str = '<tr>';
                    $str .= "<td class='text'>{$row['id']}</td>";
                    if ((int)$row['type'] == 0) {
                        $str .= "<td class='text'>会员</td>";
                    } elseif ((int)$row['type'] == 1) {
                        $str .= "<td class='text'>代理商</td>";
                    } elseif ((int)$row['type'] == 2) {
                        $str .= "<td class='text'>会员+代理商</td>";
                    } else {
                        $str .= "<td class='text'>自定义</td>";
                    }

                    if ((int)$row['state'] == 1) {
                        $str .= "<td class='text'>已发送</td>";
                    } else {
                        $str .= "<td class='text'>待发送</td>";
                    }

                    $str .= "<td class='text'>{$row['subject']}</td>";
                    $str .= "<td class='text'>".date('Y-m-d H:i:s', intval($row['in_time'] / 1000))."</td>";
                    $str .= "</tr>";
                    echo $str;
                }
                ob_flush();
                flush();
            } else {
                break;
            }
            $this->start += $pageSize;
            $leftSize -= $pageSize;
            if ($this->start >= $result['data']['totalCount']) {
                break;
            }
        }
        echo "</table>";
        if (ob_get_level()) {
            ob_end_flush();
        }
    }
}
