<?php

namespace app\admin\controller;
use GatewayWorker\Lib\Db;
use think\facade\View;
use think\facade\Lang;

class Coupon extends AdminControl
{


    /**
     * 卡券消费列表
     */
    public function coupon_expense_list()
    {
        $this->getExpenseList();
        return View::fetch('coupon_expense_list');
    }


    /**
     * 卡券管理列表
     */
    public function coupon_list()
    {

//        $this->get_complain_list(self::STATE_NEW, 'complain_new_list');

        $this->getCouponList();
        return View::fetch('coupon_list');

//        $complain_model = model('complain');
//        //搜索条件
//        $condition = array();
//        $condition[]=array('accuser_name','like', "%" . input('param.input_complain_accuser') . "%");
//        $condition[]=array('accused_name','like', "%" . input('param.input_complain_accused') . "%");
//        $condition[]=array('complain_subject_content','like', "%" . input('param.input_complain_subject_content') . "%");
//        if(input('param.input_complain_datetime_end')) {
//            $stime = strtotime(input('param.input_complain_datetime_start'));
//            $condition[]=array('complain_datetime','>=', $stime);
//        }
//        if(input('param.input_complain_datetime_end')){
//            $etime = strtotime(input('param.input_complain_datetime_end'))+86399;
//            $condition[]=array('complain_datetime','<=', $etime);
//        }
//        if ($action === 'complain_finish_list') {
//            $order= 'complain_id desc';
//        } else {
//            $order = 'complain_id asc';
//        }
//        $condition[]=array('complain_state','=',$complain_state);
//        $complain_list = $complain_model->getComplainList($condition, 10 ,$order);
//        $this->setAdminCurItem($action);
//        View::assign('complain_list', $complain_list);
//        View::assign('show_page', $complain_model->page_info->render());


    }

    /**
     * 卡券详情
     */
    public function coupon_info()
    {

        return View::fetch('coupon_info');
    }


    /**
     * 卡券消费列表
     */
    private function getExpenseList()
    {
        $where = [];
        $model = model('coupon');




        $expenseList = $model->getExpenseList($where,10,'expense_time desc');

//        echo json_encode($expenseList);exit;

        if(!empty($expenseList)){




            //用户数据
            $userIds = array_column($expenseList,'member_id');
            $userIds = array_values(array_unique($userIds));
            $map = [];
            $map[] = ['member_id','in',$userIds];
            $userList =  model('member')->getMemberList($map,'member_id,member_nickname,member_name,member_avatar');

            $memberList = [];
            foreach($userList as $k =>$v){
                $memberList[$v['member_id']] = $v;
            }


            $virOrderIds = [];
            $orderIds = [];
            foreach($expenseList as $v){
                if($v['is_vir'] == 0){
                    $orderIds[] = $v['order_id'];
                }else{
                    $virOrderIds[] = $v['order_id'];
                }
            }

            //常规订单数据
            $orderList = [];
            if(!empty($orderIds)){
                $orderModel = model('order');
                $orderMap = [];
                $orderMap[] = ['order_id','in',$orderIds];
                $orderData = $orderModel->getOrderList($orderMap,'','order_id,order_sn,order_state,order_amount,buyer_name,store_name,add_time');
                if(!empty($orderData)){
                    foreach($orderData as $k=>$v){
                        $orderList[$v['order_id']] = $v;
                    }
                }
            }

            //虚拟订单数据
            $virOrderList = [];
            if(!empty($virOrderIds)){
                $virModel = model('vrorder');
                $virMap = [];
                $virMap[] = ['order_id','in',$virOrderIds];
                $virData = $virModel->getVrorderList($virMap,'','order_id,order_sn,order_state,order_amount,buyer_name,store_name,add_time');
                if(!empty($virData)){
                    foreach($virData as $k=>$v){
                        $virOrderList[$v['order_id']] = $v;
                    }
                }
            }

            

            //卡券数据
            $cardIds = array_values(array_unique(array_column($expenseList,'card_id')));

            $cardMap = [];
            $cardMap[] = ['id','in',$cardIds];
            $cardModel = model('coupon');
            $cardData = $cardModel->getCardList($cardMap,'','id,card_no');
            $cardList = [];
            if(!empty($cardData)){
                foreach($cardData as $k=>$v){
                    $cardList[$v['id']] = $v;
                }
            }
            foreach($expenseList as $k=>$v){
                $expenseList[$k]['expense_time'] = date('Y-m-d H:i:s',$v['expense_time']);
                //卡券的
                if(!empty($cardList[$v['card_id']])){
                    $cardInfo = $cardList[$v['card_id']];
                    $expenseList[$k]['card_no'] = $cardInfo['card_no'];
                }else{
                    $expenseList[$k]['card_no'] = '';
                }



                if($v['is_vir'] == 1){
                    //虚拟订单的
                    if(!empty($virOrderList[$v['order_id']])){
                        $orderInfo = $virOrderList[$v['order_id']];
                        $expenseList[$k]['order_id'] = $v['order_id'];
                        $expenseList[$k]['order_sn'] = $orderInfo['order_sn'];
                        $expenseList[$k]['order_amount'] = $orderInfo['order_amount'];
                        $expenseList[$k]['buyer_name'] = $orderInfo['buyer_name'];
                        $expenseList[$k]['store_name'] = $orderInfo['store_name'];
                        $expenseList[$k]['add_time'] = date('Y-m-d H:i:s',$orderInfo['add_time']);
                    }else{
                        $expenseList[$k]['order_id'] = '';
                        $expenseList[$k]['order_sn'] = '';
                        $expenseList[$k]['order_amount'] = '';
                        $expenseList[$k]['buyer_name'] = '';
                        $expenseList[$k]['store_name'] = '';
                        $expenseList[$k]['add_time'] = '';
                    }
                }else{
                    //实物订单的
                    if(!empty($orderList[$v['order_id']])){
                        $orderInfo = $orderList[$v['order_id']];
                        $expenseList[$k]['order_id'] = $v['order_id'];
                        $expenseList[$k]['order_sn'] = $orderInfo['order_sn'];
                        $expenseList[$k]['order_amount'] = $orderInfo['order_amount'];
                        $expenseList[$k]['buyer_name'] = $orderInfo['buyer_name'];
                        $expenseList[$k]['store_name'] = $orderInfo['store_name'];
                        $expenseList[$k]['add_time'] = date('Y-m-d H:i:s',$orderInfo['add_time']);
                    }else{
                        $expenseList[$k]['order_id'] = '';
                        $expenseList[$k]['order_sn'] = '';
                        $expenseList[$k]['order_amount'] = '';
                        $expenseList[$k]['buyer_name'] = '';
                        $expenseList[$k]['store_name'] = '';
                        $expenseList[$k]['add_time'] = '';
                    }
                }

                //用户的
                if(!empty($memberList[$v['member_id']])){
                    $memberInfo = $memberList[$v['member_id']];
                    $expenseList[$k]['member_nickname'] = $memberInfo['member_nickname'];

                    if(empty($memberInfo['member_avatar']) || $memberInfo['member_avatar'] == null){
                        $expenseList[$k]['member_avatar'] = HOME_SITE_ROOT . '/images/member/default_image.png';
                    }else{
                        $expenseList[$k]['member_avatar'] = $memberInfo['member_avatar'];
                    }

                }else{
                    $expenseList[$k]['member_nickname'] = '';
                    $expenseList[$k]['member_avatar'] = HOME_SITE_ROOT . '/images/member/default_image.png';
                }

                $expenseList[$k]['expense_amount'] = number_format($v['expense_amount'],2);


            }
        }



        $action = 'coupon_expense_list';
        $this->setAdminCurItem($action);
        View::assign('expense_list', $expenseList);
        View::assign('show_page', $model->page_info->render());
    }


    /**
     * 卡券列表
     */
    private function getCouponList()
    {

        $where = [];
        $card_model = model('coupon');

        $card_list = $card_model->getCardList($where,10,'create_time desc');


        if(!empty($card_list)){

            $userIds = array_column($card_list,'member_id');
            $userIds = array_values(array_unique($userIds));

            $map = [];
            $map[] = ['member_id','in',$userIds];
            $userList =  model('member')->getMemberList($map,'member_id,member_nickname,member_name,member_avatar');

            $rel = [];
            foreach($userList as $k =>$v){
                $rel[$v['member_id']] = $v;
            }


            foreach($card_list as $k =>$v){
                if(!empty($rel[$v['member_id']])){
                    $userInfo = $rel[$v['member_id']];
                    $card_list[$k]['member_nickname'] = $userInfo['member_nickname'];
                    $card_list[$k]['member_avatar'] = $userInfo['member_avatar'];
                }else{
                    $card_list[$k]['member_nickname'] = '';
                    $card_list[$k]['member_avatar'] = '';
                }


                $card_list[$k]['balance'] = number_format($v['balance'],2);
                $card_list[$k]['worth'] = number_format($v['worth'],2);



            }





        }

        $action = 'coupon_list';

        $this->setAdminCurItem($action);

        View::assign('card_list', $card_list);
        View::assign('show_page', $card_model->page_info->render());
    }



    protected function getAdminItemList()
    {
        $menu_array = array(
            array(
                'name' => 'coupon_list',
                'text' => '卡券列表',
                'url' => (string)url('coupon/coupon_list')
            ),
            array(
                'name' => 'coupon_expense_list',
                'text' => '消费记录',
                'url' => (string)url('coupon/coupon_expense_list')
            )
        );

        if (request()->action() == 'complain_progress') {
            $menu_array[] = array(
                'name' => 'complain_progress',
                'text' => lang('complain_progress'),
                'url' => '###'
            );
        }
        return $menu_array;
    }





}
