<?php

namespace Admin\Controller;

use User\Api\UserApi as UserApi;

/**
 * 推广查询控制器
 * @author 王贺
 */
class UserPayWhiteListController extends ThinkController
{
    public function index($p = 1) {

        $page = intval($p);
        $page = $page ? $page : 1; //默认显示第一页数据
        $row=10;
        if(isset($_REQUEST['row'])) {$row = $_REQUEST['row'];}else{$row = 10;}

        $map = [];

        if ($_SESSION['onethink_admin']['user_group_id']) {

            $group_id = $_SESSION['onethink_admin']['user_group_id'];

        }

        $empower_type = M('AuthGroup')->field('show_data')->where(array('id'=>$group_id))->find();

        if (($empower_type['show_data']==1||$group_id==1)) {
            $this->assign('show',1);
        }

        if ($_REQUEST['show']&&($empower_type['show_data']==1||$group_id==1)) {

            $data = M('user_pay_whitelist','tab_')->where($map)
                ->page($page,$row)
                ->order('update_time DESC')
                ->select();

            foreach ($data as $key => $value) {
                if ($value['status']==2) {
                    $data[$key]['time_status'] = '永久';
                } else {
                    $data[$key]['time_status'] = '有效期至'.date('Y-m-d H:i:s',$value['update_time']+$value['time']);
                }

                $data[$key]['update_time'] = date('Y-m-d H:i:s',$value['update_time']);

            }

            $count = M('user_pay_whitelist','tab_')->where($map)->count();

            $this->assign('data',$data);

            $page = set_pagination($count,$row);
            if($page) {$this->assign('_page', $page);}
        }

        $this->checkListOrCountAuthRestMap($map,[]);

        $this->assign('data',$data);
        $this->meta_title = '玩家支付限制白名单';

        $this->display();

    }

    public function export() {

        $xlsName = '支付限制白名单账号导出';

        $xlsCell = array(
            "序号", "账号", '状态', '有效日期',
            '时间'
        );

        $map = [];

        $csvFileName = $xlsName.'.csv';
        //设置好告诉浏览器要下载excel文件的headers
        header('Content-Description: File Transfer');
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment; filename="'. $csvFileName .'"');
        header('Expires: 0');
        header('Cache-Control: must-revalidate');
        header('Pragma: public');
        $fp = fopen('php://output', 'a');//打开output流
        mb_convert_variables('GBK', 'UTF-8', $xlsCell);
        fputcsv($fp, $xlsCell);//将数据格式化为CSV格式并写入到output流中


        $accessNum = M('user_pay_whitelist','tab_')->where($map)->count();

        $perSize = 2000;//每次查询的条数
        $pages   = ceil($accessNum / $perSize);

        for($i = 1; $i <= $pages; $i++) {


            $xlsData = M('user_pay_whitelist','tab_')
                ->field("id,user_account,status,'' as time_status,update_time,time")
                ->where($map)
                ->limit(($i-1)*$perSize  ,$perSize)
                ->order('update_time DESC')
                ->select();

            foreach ($xlsData as $key => $value) {
                if ($value['status']==2) {
                    $value['time_status'] = '永久';
                    $value['status'] = '永久';
                } else if($value['status']==1){
                    $value['time_status'] = '有效期至'.date('Y-m-d H:i:s',$value['update_time']+$value['time']);
                    $value['status'] = '30天允许支付';
                } else {
                    $value['time_status'] = '有效期至'.date('Y-m-d H:i:s',$value['update_time']+$value['time']);
                    $value['status'] = '7天允许支付';
                }
                unset($value['time']);

                $value['update_time'] = date('Y-m-d H:i:s',$value['update_time']);

                mb_convert_variables('GBK', 'UTF-8', $value);
                fputcsv($fp, $value);

            }

            unset($xlsData);//释放变量的内存
            //刷新输出缓冲到浏览器
            ob_flush();
            flush();//必须同时使用 ob_flush() 和flush() 函数来刷新输出缓冲。
        }

        fclose($fp);

        $getData = $_GET;
        unset($getData['id']);
        unset($getData['xlsname']);

    }

    public function add() {

        if (IS_POST) {

            $post = $_POST;

            if($post['account']!=$post['account_check']) {
                $this->ajaxReturn(['status'=>0,'msg'=>'两次输入不一致']);
                exit();
            }

            $hav_user = M('user','tab_')->where(['account'=>$post['account']])->find();

            if(!$hav_user) {
                $this->ajaxReturn(['status'=>0,'msg'=>'账号不存在']);
                exit();
            }

            $whiteList = M('user_pay_whitelist','tab_')->where(['user_id'=>$hav_user['id']])->find();

            $insert = [];

            $insert['user_id'] = $hav_user['id'];
            $insert['user_account'] = $hav_user['account'];

            if(!$whiteList) {

                $insert['status'] = 0;
                $insert['time'] = 7*86400;
                $insert['create_time'] = time();
                $insert['update_time'] = time();

                $is_success = M('user_pay_whitelist','tab_')->add($insert);

            } else {
                $insert['update_time'] = time();
                if($whiteList['status']==0) {
                    $insert['status'] = 1;
                    $insert['time'] = 30*86400;
                } elseif($whiteList['status']==1) {
                    $insert['status'] = 2;
                    $insert['time'] = 1;

                }

                if($whiteList['status']==2) {
                    $this->ajaxReturn(['status'=>0,'msg'=>'该账号已经是永久无需再进行操作']);
                    exit();
                }

                $is_success = M('user_pay_whitelist','tab_')->where(['user_id'=>$hav_user['id']])->save($insert);
            }

            if($is_success) {
                $this->ajaxReturn(['status'=>1,'msg'=>'操作成功']);
                exit();
            } else {
                $this->ajaxReturn(['status'=>0,'msg'=>'操作失败']);
                exit();
            }

        }

        $this->meta_title = '新增支付限制白名单账号';

        $this->display();

    }

    public function del() {

        if (!$_REQUEST['id']) {
            $this->ajaxReturn(['status'=>0,'msg'=>'id不能为空']);
            exit();
        }

        $is_del = M('user_pay_whitelist','tab_')->where(['id'=>$_REQUEST['id']])->delete();

        if($is_del) {
            $this->ajaxReturn(['status'=>1,'msg'=>'删除成功']);
            exit();
        } else {
            $this->ajaxReturn(['status'=>0,'msg'=>'删除失败']);
            exit();
        }

    }



}