<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 支付商品
 * Created by PhpStorm.
 * User: lqx
 * Date: 2017/12/15 0015
 * Time: 12:18
 */


class Goods extends MY_Controller
{
    public $postdata = array('frombg' => 1, 'mod' => '', 'act' => '', 'lid' => 1, 'sid' => 1);
    protected $typeArr = [1 => '筹码', 2 => '绿钻', 3 => '互动道具', 4 => '礼包', 5 => '喇叭'];
    protected $rewardKey = ['money' => '筹码' , 'diamond' => '绿钻', 'props' => '互动道具', 'gifts' => '礼包', 'speaker' => '喇叭'];
    protected $gchannelName = [
        1 => 'Google',
        2 => 'SMS',
        3 => 'IOS',
        4 => 'Bluecoins',
        5 => 'Truemoney',
        6 => '12call',
        7 => 'dtac',
        8 => 'Line',
        9 => 'VinaPhone',
        10 => 'MobiFone',
        11 => 'Viettel'
    ];


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

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


    /**
     * 加载商品的查询页面
     */
    public function index()
    {
        //图片的baseUrl
        if( $this->env == 1 ){
            $baseUrl = $this->config->item('officialBaseUrl');
        }else{
            $baseUrl = $this->config->item('localBaseUrl');
        }
        $data['baseUrl'] = $baseUrl;
        $this->load->view('goods/index', $data);
    }

    /**
     * 加载商品的数据
     */
    public function indexData()
    {
        $gpid = $this->input->post('gpid');
        $gname = $this->input->post('gname');
        $page = $this->input->post('start');
        $limit = $this->input->post('length');

        $postdata = $this->postdata;
        $postdata['gpid'] = $gpid;
        $postdata['gname'] = $gname;
        $postdata['page'] = $page;
        $postdata['limit'] = $limit;
        $postdata['mod'] = 'Paymentgoods';
        $postdata['act'] = 'getGoodsList';
        $this->env == 2 && $postdata['demo'] = 1;

        $retData = $this->curl->req($this->_url, $postdata);
        $ret = json_decode($retData, TRUE);
        if(empty($ret)) {
           die(json_encode($this->withNodata()));
        }

        $list = $ret['list'];
        foreach ($list as $k => &$_v) {
            $_v['typeName'] = $this->typeArr[$_v['gtype']] ?? '';
            $_v['productContent'] = '';
            $baseReward = json_decode($_v['gbaseReward'], true);
            if(!empty($baseReward) && is_array($baseReward)) {
                foreach ($baseReward as $rkey => $reward) {//处理商品内容
                    if(empty($reward)) {
                        continue;
                    }
                    switch ($rkey) {
                        case "money":
                            $_v['productContent'] .= "筹码:".$reward." <br/>";
                            continue;
                        case "diamond":
                            $_v['productContent'] .= "钻石:".$reward."  <br/>";
                            continue;
                        case "props":
                            $_v['productContent'] .= "道具:".$reward."  <br/>";
                            continue;
                        case "gifts":
                            $_v['productContent'] .= "礼包:".$reward." <br/>";
                            continue;
                        case "speaker":
                            $_v['productContent'] .= "喇叭:".$reward."  ";
                        default:
                            break;
                    }
                }
            }
            //处理商品渠道名称
            $_v['gchannelName'] = $this->gchannelName[$_v['gchannel']] ?? '';
            //上下架状态
            $_v['gstatusName'] = $_v['gstatus'] == 1 ? '上架' : '下架';
            //热卖属性
            $_v['gattributeName'] = $_v['gattribute'] == 1 ? '新品' : '热卖';
            //平台
            $_v['gplatformName'] = $_v['gplatform'] == 1 ? 'Android' : 'IOS';
        }

        $data['data'] = $list;
        $data['recordsTotal'] = $ret['total'];
        $data['recordsFiltered'] = $ret['total'];
        die(json_encode($data));
    }

    /**
     * 商品增加界面
     */
    public function add()
    {
//        if(!$this->isAdmin()) {//如果不是管理员没有权限查看用户列表
//            redirect('permission/forbidpage');
//            exit;
//        }
        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');
        }
        $data['url'] = $url;
        $data['baseUrl'] = $baseUrl;
        $data['goodsType'] = $this->typeArr;
        $data['rewardType'] = $this->rewardKey;
        $data['gchannelName'] = $this->gchannelName;
        $data['goodsStatusArr'] = [ 1 => '上架', 2 => '下架'];
        $data['goodsAttributeArr'] = [1 => '新品', 2 => '热卖'];
        $data['goodsPlatformArr'] = [ 1 => 'Android', 2 => 'IOS'];

        $this->load->view('goods/add', $data);
    }

    /**
     * 商品添加操作
     * @return json
     */
    public function doAdd()
    {
        $data['gimg'] = $this->input->post('uploadedFile');
        $data['gpid'] = $this->input->post('gpid');
        $data['gprice'] = $this->input->post('gprice');
        $data['gpriceThb'] = $this->input->post('gpriceThb');
        $data['gname'] = $this->input->post('gname');
        $data['gdesc'] = $this->input->post('gdesc');
        $data['gtype'] = $this->input->post('gtype');
        $data['gaddPro'] = $this->input->post('gaddPro');
        $data['gactivityReward'] = $this->input->post('gactivityReward');
        $data['gdiscount'] = $this->input->post('gdiscount');
        $data['gchannel'] = $this->input->post('gchannel');
        $data['gsort'] = $this->input->post('gsort');
        $data['gchannel_sort'] = $this->input->post('gchannel_sort');
        $data['gstatus'] = $this->input->post('gstatus');
        $data['gattribute'] = $this->input->post('gattribute');
        $data['ggameid'] = $this->input->post('ggameid');
        $data['gplatform'] = $this->input->post('gplatform');

        //reward
        $rewardList = array_filter($this->input->post('rewardList'));
        if(!empty($rewardList)) {
            $reward = $rewardList;
        }else {
            $reward = [];
        }
        $data['gbaseReward'] = json_encode($reward);
        $postdata =array_merge($this->postdata, $data);
        $postdata['mod'] = 'Paymentgoods';
        $postdata['act'] = 'addGoods';
        $this->env == 2 && $postdata['demo'] = 1;

        $retData = $this->curl->req($this->_url, $postdata);
        $ret = json_decode($retData, TRUE);
        if(!empty($ret) || $ret['code'] == 0) {
            return $this->response(0, '商品添加成功');
        }
        return $this->response(-1, '商品添加失败');
    }

    /**
     * 商品修改界面
     */
    public function edit()
    {
//        if(!$this->isAdmin()) {//如果不是管理员没有权限查看用户列表
//            redirect('permission/forbidpage');
//            exit;
//        }
        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 = $this->postdata;
        $postdata['gid'] = $this->input->get('gid');
        $postdata['mod'] = 'Paymentgoods';
        $postdata['act'] = 'getGoodsByID';
        $this->env == 2 && $postdata['demo'] = 1;

        $retData = $this->curl->req($this->_url, $postdata);
        $ret = json_decode($retData, TRUE);
        //处理baseReward
        $baseReward = $ret['data']['gbaseReward'];
        $baseReward = json_decode($baseReward, true);

        $data = $ret['data'] ?? [];
        $data['baseRewardArr'] = $baseReward;
        $data['rewardType'] = $this->rewardKey;
        $data['url'] = $url;
        $data['baseUrl'] = $baseUrl;
        $data['goodsType'] = $this->typeArr;
        $data['gchannelName'] = $this->gchannelName;
        $data['goodsStatusArr'] = [ 1 => '上架', 2 => '下架'];
        $data['goodsAttributeArr'] = [1 => '新品', 2 => '热卖'];
        $data['goodsPlatformArr'] = [ 1 => 'Android', 2 => 'IOS'];

        $this->load->view('goods/edit', $data);
    }

    /**
     * 进行商品修改
     * @return json
     */
    public function doEdit()
    {
        $data['gid'] = $this->input->post('gid');
        if(empty($data['gid'])) {
            return $this->response(-1, '商品修改失败');
        }
        $data['gimg'] = $this->input->post('uploadedFile');
        $data['gpid'] = $this->input->post('gpid');
        $data['gprice'] = $this->input->post('gprice');
        $data['gpriceThb'] = $this->input->post('gpriceThb');
        $data['gname'] = $this->input->post('gname');
        $data['gdesc'] = $this->input->post('gdesc');
        $data['gtype'] = $this->input->post('gtype');
        $data['gaddPro'] = $this->input->post('gaddPro');
        $data['gactivityReward'] = $this->input->post('gactivityReward');
        $data['gdiscount'] = $this->input->post('gdiscount');
        $data['gchannel'] = $this->input->post('gchannel');
        $data['gsort'] = $this->input->post('gsort');
        $data['gchannel_sort'] = $this->input->post('gchannel_sort');
        $data['gstatus'] = $this->input->post('gstatus');
        $data['gattribute'] = $this->input->post('gattribute');
        $data['ggameid'] = $this->input->post('ggameid');
        $data['gplatform'] = $this->input->post('gplatform');

        //reward
        $rewardList = array_filter($this->input->post('rewardList'));
        if(!empty($rewardList)) {
            $reward = $rewardList;
        }else {
            $reward = [];
        }
        $data['gbaseReward'] = json_encode($reward);
        $postdata =array_merge($this->postdata, $data);
        $postdata['mod'] = 'Paymentgoods';
        $postdata['act'] = 'editGoods';
        $this->env == 2 && $postdata['demo'] = 1;

        $retData = $this->curl->req($this->_url, $postdata);
        $ret = json_decode($retData, TRUE);
        if(!empty($ret) || $ret['code'] == 0) {
            return $this->response(0, '商品修改成功');
        }
        return $this->response(-1, '商品修改失败');
    }

}

