<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/3/003
 * Time: 19:49
 */

namespace backend\models\searches;


use backend\models\RemitApi;
use common\helpers\ExcelHelper;
use common\helpers\UserAgentHelper;
use yii\data\ArrayDataProvider;
use yii\data\Pagination;
use yii\helpers\Url;

class RemitDetailSearch extends DataTable
{
    public $remit_no;

    /**
     * @return array
     */
    public function rules()
    {
        $rules = parent::rules();
        $rules[] = [['remit_no'], 'safe'];
        return $rules;
    }

    public function search($params)
    {
        $this->setAttributes($params);
        $this->initSearchParams();

        $pagination = new Pagination();
        $pagination->setPageSize($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']);
        }

        $data['remit_no'] = $this->remit_no;
        $data['batch_no'] = $params['batch_no'];

        if ($this->pageSize > 1000) {
            if (\Yii::$app->getRequest()->isAjax) {
                \Yii::$app->getResponse()->redirect(Url::current(), UserAgentHelper::isIE() ? 200 : 302);
            } else {
                $this->exportXls($data);
            }
            \Yii::$app->end();
        }

        $api = new RemitApi();
        $result = $api->financeRemitDetail($data);
        // $result = $this->multiQuery($api, 'financeRemitDetail', $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 array $params
     */
    protected function exportXls($params)
    {
        ExcelHelper::init();
        $attachmentName = "财务打款审核记录_" . date('Y-m-d') . '.xls';
        $header = ['打款批次号', '打款单号', '打款方式', '出金单号', '收款人名字', '打款金额', '银行名称', '银行支行', '银行卡号', '开户行所在省', '开户行所在市', '银行代码', '备注', '打款信息'];
        $source = [];

        $api = new RemitApi();
        $leftSize = $this->pageSize;
        while ($leftSize > 0) {
            // 重新计算页数和分页大小 start
            $pageSize = $leftSize > 1000 ? 1000 : $leftSize;
            $params['pageSize'] = $pageSize;
            $params['page'] = ceil($this->start / $pageSize) + 1;

            $result = $api->financeRemitDetail($params);
            if ($result['code'] == 1 && !empty($result['data']['dataList'])) {
                foreach ((array)$result['data']['dataList'] as $key => $row) {
                    $arr = [];
                    $arr[] = ' '.$row['batch_no'];
                    $arr[] = ' '.$row['remit_no'];
                    $pay_type = '';
                    if ((int)$row['pay_type'] == 1) {
                        $pay_type = '爱农';
                    } elseif ((int)$row['pay_type'] == 2) {
                        $pay_type = '汇道';
                    } elseif ((int)$row['pay_type'] == 3) {
                        $pay_type = '';
                    } elseif ((int)$row['pay_type'] == 4) {
                        $pay_type = '华银';
                    } elseif ((int)$row['pay_type'] == 5) {
                        $pay_type = '奥凸';
                    }
                    $arr[] = $pay_type;
                    $arr[] = ' '.$row['biz_no'];
                    $arr[] = $row['payee_name'];
                    $arr[] = $row['amount'];
                    $arr[] = $row['bank_name'];
                    $arr[] = $row['bank_branch'];
                    $arr[] = ' '.$row['bank_card_no'];
                    $arr[] = $row['bank_province'];
                    $arr[] = $row['bank_city'];
                    $arr[] = $row['bank_code'];
                    $arr[] = $row['memo'];
                    $arr[] = $row['ret_msg'];
                    $source[] = $arr;
                }
            } else {
                break;
            }
            $this->start += $pageSize;
            $leftSize -= $pageSize;
            if ($this->start >= $result['data']['totalCount']) {
                break;
            }
        }
        ExcelHelper::output($source, $header, $attachmentName);
    }

    private function exportXlsx($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>打款批次号</th><th>打款单号</th><th>打款方式</th><th>出金单号</th><th>收款人名字</th><th>打款金额</th><th>银行名称</th><th>银行支行</th><th>银行卡号</th><th>开户行所在省</th><th>开户行所在市</th><th>银行代码</th><th>备注</th><th>打款信息</th></tr>';
        $api = new RemitApi();
        $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->financeRemitDetail($data);
            if ($result['code'] == 1 && $result['data']['dataList']) {
                foreach ((array)$result['data']['dataList'] as $key => $row) {
                    $str = '<tr>';
                    $str .= "<td class='text'>{$row['batch_no']}</td>";
                    $str .= "<td class='text'>{$row['remit_no']}</td>";
                    if ((int)$row['pay_type'] == 1) {
                        $str .= "<td class='text'>爱农</td>";
                    } elseif ((int)$row['pay_type'] == 2) {
                        $str .= "<td class='text'>汇道</td>";
                    } elseif ((int)$row['pay_type'] == 3) {
                        $str .= "<td class='text'></td>";
                    } elseif ((int)$row['pay_type'] == 4) {
                        $str .= "<td class='text'>华银</td>";
                    } elseif ((int)$row['pay_type'] == 5) {
                        $str .= "<td class='text'>奥凸</td>";
                    }
                    $str .= "<td class='text'>{$row['biz_no']}</td>";
                    $str .= "<td class='text'>{$row['payee_name']}</td>";
                    $str .= "<td class='text'>{$row['amount']}</td>";
                    $str .= "<td class='text'>{$row['bank_name']}</td>";
                    $str .= "<td class='text'>{$row['bank_branch']}</td>";
                    $str .= "<td class='text'>{$row['bank_card_no']}</td>";
                    $str .= "<td class='text'>{$row['bank_province']}</td>";
                    $str .= "<td class='text'>{$row['bank_city']}</td>";
                    $str .= "<td class='text'>{$row['bank_code']}</td>";
                    $str .= "<td class='text'>{$row['memo']}</td>";
                    $str .= "<td class='text'>{$row['ret_msg']}</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();
        }
    }
}
