<?php
/**
 * 兑换商城
 */
defined('BASEPATH') OR exit('No direct script access allowed');

class Mall extends MY_Controller{


    public $retdata = array('referer' =>'', 'refresh' => 'false', 'state' => 'error', 'message' => '');

    public $postData = array('frombg' => 1, 'mod' => '', 'act' => '', 'lid' => 1, 'sid' => 1);

    public $coupon = array(1 => '礼品券', 2 => '兑换券', 3 => '蚂蚁币');

    public $phonecardStatus = array( 0 => '可使用', 1 => '已使用' );

    protected $limit = 7;//分页
    protected $pageConfig = array(
        'first_tag_open' => '<li>',
        'first_tag_close' => '</li>',
        'last_tag_open' => '<li>',
        'last_tag_close' => '</li>',
        'next_tag_open' => '<li>',
        'next_tag_close' => '</li>',
        'prev_tag_open' => '<li>',
        'prev_tag_close' => '</li>',
        'cur_tag_open' => "<li class='active'><a hef='#'>",
        'cur_tag_close' => "</a></li>",
        'num_tag_open' => "<li>",
        'num_tag_close' => "</li>",
    );

    public function __construct()
    {
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('cookie');
        $this->load->library('curl');
        $this->load->library('pagination');

        //请求地址
        $this->env = get_cookie($this->_wenenv);
        $this->_url = ($this->env == 2) ? $this->config->item('localUrl') : $this->config->item('officialUrl');
    }

    /**
     * 获取兑换商城商品列表
     */
    public function getList($page = 0 ){
        $data = array('list' => [], 'total' => 0, 'pageurl' => '');
        if( $this->env == 2 ){
            $baseUrl = $this->config->item('localBaseUrl');
            $url = $this->config->item('localUrl');
        }else{
            $baseUrl = $this->config->item('officialBaseUrl');
            $url = $this->config->item('officialUrl');
        }
        $this->postData['mod'] = 'Mall';
        $this->postData['act'] = 'getList';
        $this->postData['page'] = $page;
        $this->postData['limit'] = $this->limit;
        ($this->env == 2) && $this->postData['demo'] = 1;
        $retData = $this->curl->req($url, $this->postData);
        $ret = json_decode($retData, TRUE);
        //分页
        $pageConfig = $this->pageConfig;
        $pageConfig['base_url'] = base_url('#mall/getList');
        $pageConfig['total_rows'] = $ret['total'];
        $pageConfig['per_page'] = $this->limit;
        $this->pagination->initialize($pageConfig);
        $pageurl = $this->pagination->create_links();
        $data['pageurl'] = $pageurl;
        $data['list'] = $ret['list'];
        $data['total'] = $ret['total'];
        $data['baseUrl'] = $baseUrl;
        $data['langList'] = $this->_langList;
        $data['couponName'] = $this->coupon;

        $this->load->view('mall/getlist', $data);
    }

    /**
     * 兑换商城 添加商品页面
     */
    public function add(){
        if( $this->env == 1 ){
            $url = $this->config->item('officialBaseUrl').'/api/imgupload.php';
            $baseUrl = $this->config->item('officialBaseUrl');
        }else{
            $url = $this->config->item('localBaseUrl').'/api/imgupload.php';
            $baseUrl = $this->config->item('localBaseUrl');
        }

        //获取兑换券列表
        $this->env == 2 && $this->postData['demo'] = 1;
        $postData = array_merge(array('getall' => 1), $this->postData);
        $postData['mod'] = 'Mall';
        $postData['act'] = 'couponlist';
        $retData = $this->curl->req($this->_url, $postData);
        $couponSelect = array();//礼品券列表
        if ($retData){
            $couponList = json_decode($retData, 1);
            if (is_array($couponList)){
                foreach ($couponList as $k => $v){
                    $couponName = unserialize($v['cname']);
                    $couponSelect[$v['cid']] = $couponName['zh'];
                }
            }
        }
        $data['couponSelect'] = $couponSelect;
        $data['url'] = $url;
        $data['baseUrl'] = $baseUrl;
        $data['coupon'] = $this->coupon;
        $this->load->view('mall/add', $data);
    }

    /**
     * 兑换商城 添加兑换商城商品
     */
    public function actionAdd(){
        $postdata['uploadedFile'] = $this->input->post('uploadedFile');
        $postdata['mcoupon'] = $this->input->post('mcoupon');//礼品券类型
        $postdata['mcouponname'] = $this->input->post('mcouponname');//礼品券ID
        $postdata['mnum'] = $this->input->post('mnum');
        $postdata['mstatus'] = $this->input->post('mstatus');
        $postdata['msort'] = $this->input->post('msort');

        $mname = trim($this->input->post('mname'));
        $nameArr = [];
        $nameList = preg_split('/[\r\n]/', $mname);//处理换行
        foreach ($nameList as $v){
            if (!$v){
                continue;
            }
            $info = explode('=', $v);
            $nameArr[$info[0]] = $info[1];
        }
        $postdata['mname'] = json_encode($nameArr);
        $this->env == 2 && $postdata['demo'] = 1;
        $postdata = array_merge($postdata, $this->postData);
        $postdata['mod'] = 'Mall';
        $postdata['act'] = 'add';
        $retData = $this->curl->req($this->_url, $postdata);
        $retData = json_decode($retData, 1);
        if( $retData['num'] > 0 ){
            $this->response(0, '添加成功');
        }else{
            $this->response(1, '添加失败');
        }
        die(json_encode($this->retdata));
    }


    /**
     * 兑换商城 编辑商品页面
     */
    public function edit($id){
        if( $this->env == 1 ){
            $url = $this->config->item('officialBaseUrl').'/api/imgupload.php';
            $baseUrl = $this->config->item('officialBaseUrl');
        }else{
            $url = $this->config->item('localBaseUrl').'/api/imgupload.php';
            $baseUrl = $this->config->item('localBaseUrl');
        }

        $postdata['id'] = $id;
        $postdata = array_merge($postdata, $this->postData);
        $this->env == 2 && $postdata['demo'] = 1;
        $postdata['mod'] = 'Mall';
        $postdata['act'] = 'getOne';
        $retData = $this->curl->req($this->_url, $postdata);
        $ret = json_decode($retData, 1);

        //获取兑换券列表
        $this->env == 2 && $this->postData['demo'] = 1;
        $postData = array_merge(array('getall' => 1), $this->postData);
        $postData['mod'] = 'Mall';
        $postData['act'] = 'couponlist';
        $retData = $this->curl->req($this->_url, $postData);
        $couponSelect = array();//礼品券列表
        if ($retData){
            $couponList = json_decode($retData, 1);
            if (is_array($couponList)){
                foreach ($couponList as $k => $v){
                    $couponName = unserialize($v['cname']);
                    $couponSelect[$v['cid']] = $couponName['zh'];
                }
            }
        }

        if(!empty($ret)) {
            $data = $ret;
            $name = json_decode($ret['name'], 1);
            $nameStr = '';
            if(!empty($name)) {
                foreach ($name as $k => $v) {
                    $nameStr .= $k. '='.$v."\n";
                }
            }
            $data['name'] = $nameStr;
        }else {
            $data = [];
        }

        $data['couponSelect'] = $couponSelect;
        $data['url'] = $url;
        $data['baseUrl'] = $baseUrl;
        $data['couponList'] = $this->coupon;
        $this->load->view('mall/edit', $data);
    }


    /**
     * 兑换商城 修改兑换商城商品
     */
    public function actionEdit(){
        $postdata['mid'] = $this->input->post('mid');
        $postdata['uploadedFile'] = $this->input->post('uploadedFile');
        $postdata['mcoupon'] = $this->input->post('mcoupon');
        $postdata['mnum'] = $this->input->post('mnum');
        $postdata['mstatus'] = $this->input->post('mstatus');
        $postdata['msort'] = $this->input->post('msort');
        $postdata['mcouponname'] = $this->input->post('mcouponname');//礼品券ID

        $mname = trim($this->input->post('mname'));
        $nameArr = [];
        $nameList = preg_split("/[\r\n]/", $mname);//处理换行
        foreach ($nameList as $v){
            if (!$v){
                continue;
            }
            $info = explode('=', $v);
            $nameArr[$info[0]] = $info[1];
        }
        $postdata['mname'] = json_encode($nameArr);
        $this->env == 2 && $postdata['demo'] = 1;
        $postdata = array_merge($postdata, $this->postData);
        $postdata['mod'] = 'Mall';
        $postdata['act'] = 'edit';
        $retData = $this->curl->req($this->_url, $postdata);
        $retData = json_decode($retData, 1);
        if( $retData['num'] > 0 ){
            $this->response(0, '更新成功');
        }else{
           $this->response(1, '更新失败');
        }
    }

    /**
     * 兑换商城 删除兑换商城商品
     */
    public function actionDelete(){
        $postdata['mid'] = $this->input->post('mid');

        $this->env == 2 && $postdata['demo'] = 1;
        $postdata = array_merge($postdata, $this->postData);
        $postdata['mod'] = 'Mall';
        $postdata['act'] = 'delete';
        $retData = $this->curl->req($this->_url, $postdata);
        $retData = json_decode($retData, 1);

        die(json_encode($retData));
    }

    /**
     * 兑换商城 兑换电话卡
     */
    public function phonecard($p = 0){
        $data = array('list' => [], 'total' => 0);
        ($this->env == 2) && $this->postData['demo'] = 1;
        $postData = array_merge(array('p' => $p, 'limit' => 20), $this->postData);
        $postData['mod'] = 'Mall';
        $postData['act'] = 'phoneCard';
        $retData = $this->curl->req($this->_url, $postData);
        if ($retData){
            $data['list'] = json_decode($retData, 1);
            $data['total'] = count($data['list']);
        }
        //分页
        $pageConfig = $this->pageConfig;
        $pageConfig['base_url'] = base_url('#mall/phonecard');
        $pageConfig['total_rows'] = $data['total'];
        $pageConfig['per_page'] = 20;
        $this->pagination->initialize($pageConfig);
        $pageurl = $this->pagination->create_links();
        $data['pageurl'] = $pageurl;
        $data['phonecardStatus'] = $this->phonecardStatus;
        $this->load->view('mall/phonecard', $data);
    }





    /**
     * 兑换商城 添加电话卡
     */
    public function actionaddphonecard(){
        $phone_card = $this->input->post('phone_card'); //卡号
        $phone_pwd = $this->input->post('phone_pwd'); //密码
        $phone_operator = $this->input->post('phone_operator');//运营商

        ($this->env == 2) && $this->postData['demo'] = 1;
        $postData = array_merge(array('phone_card' => $phone_card, 'phone_pwd' => $phone_pwd, 'phone_operator' => $phone_operator), $this->postData);
        $postData['mod'] = 'Mall';
        $postData['act'] = 'actionaddphonecard';
        $retData = $this->curl->req($this->_url, $postData);
        $data = $this->retdata;
        $data['refresh'] = true;
        $data['state'] = 'success';
        $data['message'] = '添加成功';
        die(json_encode($data));
    }

    /**
     * 兑换商城 删除电话卡
     */
    public function actionDelphonecard(){
        $id = $this->input->post('mid');
        ($this->env == 2) && $this->postData['demo'] = 1;
        $postData = array_merge(array('id' => $id), $this->postData);
        $postData['mod'] = 'Mall';
        $postData['act'] = 'actionDelphonecard';
        $retData = $this->curl->req($this->_url, $postData);
        die(json_encode(array('num' => 1)));
    }


    /*
     * 兑换商城 礼品券, 礼券列表
     */
    public function couponlist($p = 0){
        $data = array('list' => [], 'total' => 0);

        $this->env == 2 && $this->postData['demo'] = 1;
        $postData = array_merge(array('p' => $p, 'limit' => 20), $this->postData);
        $postData['mod'] = 'Mall';
        $postData['act'] = 'couponlist';
        $retData = $this->curl->req($this->_url, $postData);
        if ($retData){
            $list = json_decode($retData, 1);
            foreach ($list as $k => $v){
                $cnameArr = unserialize($v['cname']);
                $list[$k]['cnameArr'] = $cnameArr;
            }
            $data['list'] = $list;
            $data['total'] = count($data['list']);
            //分页
            $pageConfig = $this->pageConfig;
            $pageConfig['base_url'] = base_url('#mall/couponlist');
            $pageConfig['total_rows'] = $data['total'];
            $pageConfig['per_page'] = 20;
            $this->pagination->initialize($pageConfig);
            $pageurl = $this->pagination->create_links();
            $data['pageurl'] = $pageurl;
        }
        $this->load->view('mall/couponlist', $data);
    }

    /**
     * 兑换商城 添加礼品券
     */
    public function actionAddCoupon(){
        $name = $this->input->post('cname');
        $cexpire = intval($this->input->post('cexpire'));
        $nameArr = [];
        $nameList = preg_split('/[\r\n]/', $name);//处理换行
        foreach ($nameList as $v){
            $v = trim($v);
            if (empty($v)){
                continue;
            }
            $info = explode('=', $v);
            $nameArr[$info[0]] = $info[1];
        }
        $nameJson = serialize($nameArr);
        $this->env == 2 && $this->postData['demo'] = 1;
        $postData = array_merge(array('nameJson' => $nameJson, 'expire' => $cexpire), $this->postData);
        $postData['mod'] = 'Mall';
        $postData['act'] = 'actionAddCoupon';
        $retData = $this->curl->req($this->_url, $postData);
        $data = $this->retdata;
        $data['refresh'] = true;
        $data['state'] = 'success';
        $data['message'] = '添加成功';
        die(json_encode($data));
    }


    /**
     * 兑换商城 --- 编辑礼品券
     */
    public function actionEditCoupon(){
        $cid = $this->input->post('cid');
        $field = $this->input->post('field');
        $val = $this->input->post('val');
        if ($field == 'cname'){
            $nameList = preg_split('/[\r\n]/', $val);//处理换行
            foreach ($nameList as $v){
                $v = trim($v);
                if (empty($v)){
                    continue;
                }
                $info = explode('=', $v);
                $nameArr[$info[0]] = $info[1];
            }
            $val = serialize($nameArr);
        }
        $this->env == 2 && $this->postData['demo'] = 1;
        $postData = array_merge(array('cid' => $cid, 'field' => $field, 'val' => $val), $this->postData);
        $postData['mod'] = 'Mall';
        $postData['act'] = 'actionEditCoupon';
        $retData = $this->curl->req($this->_url, $postData);
    }

    /**
     * 兑换商城 --- 删除礼品券
     */
    public function actionDelCoupon(){
        $cid = $this->input->post('cid');
        $this->env == 2 && $this->postData['demo'] = 1;
        $postData = array_merge(array('cid' => $cid), $this->postData);
        $postData['mod'] = 'Mall';
        $postData['act'] = 'actionDelCoupon';
        $retData = $this->curl->req($this->_url, $postData);
        exit(json_encode(array('num' => 1)));
    }

    /**
     * 玩家兑换记录
     */
    public function exchangerecord($p = 0, $uid = 0){
        $data = array('list' => [], 'mallname' => [], 'total' => 0);
        $this->env == 2 && $this->postData['demo'] = 1;
        $postData = array_merge(array('p' => $p, 'limit' => 20), $this->postData);
        $postData['uid'] = $uid;
        $postData['mod'] = 'Mall';
        $postData['act'] = 'exchangerecord';
        $retData = $this->curl->req($this->_url, $postData);
        if ($retData){
            $info = json_decode($retData, 1);
            $list = $info['list'];
            $mallList = $info['malllist'];
            foreach ($mallList as $mid => $val){
                $tempArr = json_decode($val, 1);
                $mallname[$mid] = $tempArr['zh'];
            }
            $data['list'] = $list;
            $data['total'] = $info['total'] ?? 0;
            $data['mallname'] = $mallname;

            //分页
            $pageConfig = $this->pageConfig;
            $pageConfig['base_url'] = base_url('#mall/exchangerecord');
            $pageConfig['total_rows'] = $data['total'];
            $pageConfig['per_page'] = 20;
            $this->pagination->initialize($pageConfig);
            $pageurl = $this->pagination->create_links();
            $data['pageurl'] = $pageurl;
        }
        $this->load->view('mall/exchangerecord', $data);
    }

    /**
     * 添加电话卡券
     */
    public function actionAddPhonecardCoupon(){
        $name = $this->input->post('cname');
        $operation = $this->input->post('operation');
        $amount = $this->input->post('amount');
        $cexpire = intval($this->input->post('cexpire'));
        $nameArr = [];
        $nameList = preg_split('/[\r\n]/', $name);//处理换行
        foreach ($nameList as $v){
            $v = trim($v);
            if (empty($v)){
                continue;
            }
            $info = explode('=', $v);
            $nameArr[$info[0]] = $info[1];
        }
        $nameJson = serialize($nameArr);
        $this->env == 2 && $this->postData['demo'] = 1;
        $postData = array_merge(array('nameJson' => $nameJson, 'expire' => $cexpire, 'operation' => $operation, 'amoutn' => $amount), $this->postData);
        $postData['mod'] = 'Mall';
        $postData['act'] = 'actionAddPhonecardCoupon';
        $retData = $this->curl->req($this->_url, $postData);
        $data = $this->retdata;
        $data['refresh'] = true;
        $data['state'] = 'success';
        $data['message'] = '添加成功';
        die(json_encode($data));
    }


    /**
     *	订单列表
     */
    public function userCouponList( )
    {
        $data['stime'] = '';
        $data['etime'] = '';
        $this->load->view('mall/usercouponlist', $data);
    }
    /**
     *	订单列表
     */
    public function userCouponData()
    {
        if( $this->env == 1 ){
            $url = $this->config->item('officialUrl');
        }else{
            $url = $this->config->item('localUrl');
            $this->postData['demo'] = 1;
        }

        $stime = !empty($this->input->post('coupon_stime')) ? $this->input->post('coupon_stime') : '';
        $etime = !empty($this->input->post('coupon_etime')) ? $this->input->post('coupon_etime') : '';
//        $payment_type = !empty($this->input->post('payment_type')) ? $this->input->post('payment_type') : 2;//1 所有 2 支付成功
        $start =  (int)$this->input->post('start') ? $this->input->post('start') : 0;
        $length =  (int)$this->input->post('length') ? $this->input->post('length') : 10;

        //时间查询
        $dateArr = array();

        $dateArr['stime'] = empty($stime) ? '' : strtotime($stime.' 0:0:1');
        $dateArr['etime'] = empty($etime) ? '' : strtotime($etime.' 23:59:59');


        $this->postData['mod'] = 'Mall';
        $this->postData['act'] = 'getUserCouponList';
        $this->postData['limit'] = $length;
        $this->postData['page'] = $start;
        $postData = array_merge($dateArr, $this->postData);
        $res = $this->curl->req($url, $postData);
        $retData = json_decode($res, TRUE);

        if( empty($retData) || !is_array($retData['list'])) {
            die(json_encode($this->withNodata())) ;
        }
        $list = $retData['list'];
        foreach ($list as &$v){
            $v['expiretime'] = date('Y-m-d H:i:s', $v['expiretime']);
        }
        $data['data'] = $list;
        $data['recordsTotal'] = $retData['total'];
        $data['recordsFiltered'] = $retData['total'];

        die(json_encode($data));
    }


}