<?php

namespace app\agent\controller;

use fastphp\base\Controller;
use app\agent\model\AgentUser;
use app\agent\model\User as UserModel;
use app\agent\model\ProductAccount as ProductAccountModel;

class Account extends Controller
{
    //账号列表
    public function getList ()
    {
        $get_data = $_GET;
        $agent_id = $this->userinfo['OperaterID'];
        $agent_phone = $this->userinfo['phone'];
        $page = 0;
        if (isset($_GET['PageIndex'])) {
            $page = ($_GET['PageIndex'] - 1) * 50;
        }
        $where['DeleteTag'] = 0;
        $where['agent_id']=$agent_id;

        $where_str = '';

        //处理筛选
        if($get_data['UserId'] != 0){
            $where['UserId'] = $get_data['UserId'];
        }
        if(!empty($get_data['ProductIds'])){
            $where['ProductId'] = $get_data['ProductIds'];
        }
        if(!empty($get_data['PackageNames'])){
            $where['PackageName'] = $get_data['PackageNames'];
        }
        if(!empty($get_data['accountTypes'])){
            $where['AccountType'] = $get_data['accountTypes'];
        }
        if (!empty($_GET['Btime'])){
            $date1 = date('Y-m-d', strtotime($_GET['Btime']));
            $date2 = date('Y-m-d', strtotime($_GET['Etime']));
            
            if (mb_strlen($where_str)>0){
                $where_str .= ' AND ';
            }
            $where_str .= " EndTime>='" . $date1 . "' and EndTime<='" . $date2 . "' ";
        }
        if (!empty($_GET['Bktime'])){
            if (mb_strlen($where_str)>0){
                $where_str .= ' AND ';
            }
            $date1 = date('Y-m-d', strtotime($_GET['Bktime']));
            $date2 = date('Y-m-d', strtotime($_GET['Ektime']));
            $where_str .= " StartTime>='" . $date1 . "' and StartTime<='" . $date2 . "' ";
        }
        if (!empty($get_data['keyWord'])){
            $where['Account'] = ['like','%'.$get_data['keyWord'].'%'];
        }
        //根据账号过期时间
        if ($get_data['ExpirdDay'] > -100)
        {
            
            if (mb_strlen($where_str)>0){
                $where_str .= ' AND ';
            }
            if ($get_data['ExpirdDay'] == 0)
            {
                $where_str .= ' EndTime<now() ';
            }elseif($get_data['ExpirdDay'] == -2){
                $where_str .= ' EndTime>now() ';
            } elseif ($get_data['ExpirdDay'] < 0 && $get_data['ExpirdDay']>-4) {
                $where_str .= ' TO_DAYS(EndTime) - (TO_DAYS(now()))>='.$get_data['ExpirdDay'].' AND EndTime < now() ';
            } elseif ($get_data['ExpirdDay']==-4) {
                $where_str .= 'TO_DAYS(EndTime) - (TO_DAYS(now()))<='.$get_data['ExpirdDay'].' ';
            } elseif ($get_data['ExpirdDay'] > 0){
                $where_str .= ' TO_DAYS(EndTime) - (TO_DAYS(now()))<='.$get_data['ExpirdDay'].' AND EndTime>now() ';
                // $where2 .= ' AND EndTime<now() ';
            }      
        }


        
        $account_model = new ProductAccountModel;
        $list = $account_model->getAgentListPage($where,$where_str, '*', 'id desc', "$page,50");
     
        foreach ($list as &$info){
            $endtime = strtotime($info['EndTime']);
            if($endtime <= time()){
                $info['RestTime'] = '已过期';
            } else {
                $day = ceil(($endtime - time())/86400);
                $info['RestTime'] = $day.'天';
            }
            $info['UserCode'] = $info['UserPhone']?$info['UserPhone']:$info['UserCode'];
        }

        $data = [
            'Code' => 10000,
            'Data' => $list,
            'Message' => '',
            'TotalCount' => (int)$account_model->getCount($where)['count'],
        ];
        echo json_encode($data);
    }

}