<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
 * 礼物配置.
 * Date: 2017/7/11 0011
 * Time: 16:06
 */
class Gift extends MY_Controller{

    public $postData = array('frombg' => 1, 'mod' => '', 'act' => '', 'lid' => 1, 'sid' => 1);
    public $data = array('referer' =>'', 'refresh' => 'false', 'state' => 'error', 'message' => '');

    protected $limit = 10;//分页

    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>",
    );

    private $_giftType = array(1 => '精品', 2 => '食物', 3 => '跑车', 4 => '鲜花');
    private $_vipgiftType = array(51 => 'VIP精品', 52 => 'VIP食物', 53 => 'VIP跑车', 54 => 'VIP鲜花');
    private $_giftProperty = array(1 => '新品', 2 => '热卖');

    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);
    }


    /**
     * 礼物管理
     */
    public function index( $page = 0 )
    {
        $data = array('list' => [], 'total' => '', 'pageurl' => '', 'giftType' => $this->_giftType, 'giftProperty' => $this->_giftProperty);
        if($this->env == 1){
            $url = $this->config->item('officialUrl');
            $baseUrl = $this->config->item('officialBaseUrl');
        }else{
            $url = $this->config->item('localUrl');
            $baseUrl = $this->config->item('localBaseUrl');
        }
        $this->postData['mod'] = 'Gift';
        $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);
        $list = json_decode($retData, TRUE);
        //获取后的处理
        if( !empty($list) ){
            $data['total'] = $list['total'];

            //处理数据
            foreach ($list['list'] as &$val){
                $tempType = $val['type'];
                $tempProperty = $val['property'];
                $val['type'] = $this->_giftType[$tempType] ? $this->_giftType[$tempType] : $this->_vipgiftType[$tempType];
                $val['property'] = $this->_giftProperty[$tempProperty];
                $zipPos = strpos($val['img'], 'zip');
                if ($zipPos === false){
                    $tempimg = $baseUrl.'/gift/'.$val['img'];
                    $val['img'] = '<img class="img_'.$val['id'].'" src="'.$tempimg.'" width="90px;" height="90px;">';
                }
                //$val['img'] = ($zipPos === false) ? $baseUrl.'/gift/'.$val['img'] : $val['img'];
            }
            $data['list'] = $list['list'];

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

    /**
     * 编辑礼物
     */
    public function edit($gid = 0){
        $data = array('giftType' => $this->_giftType, 'giftProperty' => $this->_giftProperty, 'vipgiftType' => $this->_vipgiftType, 'info' => []);
        if($this->env == 1){
            $url = $this->config->item('officialUrl');
            $baseUrl = $this->config->item('officialBaseUrl');
            $imguploadurl = $this->config->item('officialBaseUrl').'/api/imgupload.php';
        }else{
            $url = $this->config->item('localUrl');
            $baseUrl = $this->config->item('localBaseUrl');
            $imguploadurl = $this->config->item('localBaseUrl').'/api/imgupload.php';
        }

        $data['baseUrl'] = $baseUrl;
        $data['url'] = $imguploadurl;

        $this->postData['mod'] = 'Gift';
        $this->postData['act'] = 'getOne';
        $this->postData['gid'] = $gid;
        ($this->env == 2) && $this->postData['demo'] = 1;
        $retData = $this->curl->req($url, $this->postData);
        $giftInfo = json_decode($retData, TRUE);
        //检查图片是否为zip
        $zipfile = strpos($giftInfo['img'], 'zip');
        $zipArr = explode('zip', $giftInfo['img']);
        $giftInfo['zipfile'] = ($zipfile !== false) ? 1 : 0;
        $giftInfo['urldesc'] = $zipArr[1];
        $giftInfo['isvip'] = $giftInfo['type'] >= 50 ? 1 : 0;
        $data['info'] = $giftInfo;
        $data['gid'] = $gid;
        $this->load->view('gift/edit2', $data);
    }

    /**
    *   操作编辑礼物
    */
    public function actionEdit()
    {
        $this->postData['img'] = $this->input->post('uploadedFile');//图片路径
        $this->postData['gid'] = $this->input->post('gid');
        $this->postData['cname'] = $this->input->post('cname');
        $this->postData['ename'] = $this->input->post('ename');
        $this->postData['type'] = $this->input->post('type');
        $this->postData['price'] = $this->input->post('price');
        $this->postData['prop'] = $this->input->post('prop');
        $this->postData['expire'] = $this->input->post('expire');
        $this->postData['gsort'] = $this->input->post('gsort');
        $this->postData['isShow'] = $this->input->post('isShow');
        $this->postData['urldesc'] = $this->input->post('urldesc');//参数描述

        if ($this->postData['urldesc']){
            $this->postData['img'] .= '?'.$this->postData['urldesc'];
        }

        $this->postData['mod'] = 'Gift';
        $this->postData['act'] = 'editGift';
        $this->env == 2 && $this->postData['demo'] = 1;
        $url = ($this->env == 2) ? $this->config->item('localUrl') : $this->config->item('officialUrl');
        $res = $this->curl->req($url, $this->postData);
        $retData = json_decode($res, TRUE);

        if( $retData['num'] > 0 ){
            $this->response(0, '新增成功');
        }else{
            $this->response(3, '新增失败');
        }
        die(json_encode($this->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');
        }
        $data['url'] = $url;
        $data['baseUrl'] = $baseUrl;
        $data['giftType'] = $this->_giftType;
        $data['vipgiftType'] = $this->_vipgiftType;
        $data['giftProperty'] = $this->_giftProperty;
        $this->load->view('gift/add', $data);
    }

    /**
     * 进行新增操作
     */
    public function actionAdd()
    {

        $this->postData['img'] = $this->input->post('uploadedFile');//图片路径
        $this->postData['cname'] = $this->input->post('cname');
        $this->postData['ename'] = $this->input->post('ename');
        $this->postData['type'] = $this->input->post('type');
        $this->postData['price'] = $this->input->post('price');
        $this->postData['prop'] = $this->input->post('prop');
        $this->postData['expire'] = $this->input->post('expire');
        $this->postData['gsort'] = $this->input->post('gsort');
        $this->postData['isShow'] = $this->input->post('isShow');
        $this->postData['urldesc'] = $this->input->post('urldesc');//参数描述

        if ($this->postData['urldesc']){
            $this->postData['img'] .= '?'.$this->postData['urldesc'];
        }

        $this->postData['mod'] = 'Gift';
        $this->postData['act'] = 'addGift';
        $this->env == 2 && $this->postData['demo'] = 1;
        $url = ($this->env == 2) ? $this->config->item('localUrl') : $this->config->item('officialUrl');
        $res = $this->curl->req($url, $this->postData);
        $retData = json_decode($res, TRUE);

        if( $retData['num'] > 0 ){
            $this->response(0, '新增成功');
        }else{
            $this->response(3, '新增失败');
        }
    }

    /**
     * 上传礼物图片
     */
    public function do_upload()
    {
        //上传配置
        $config = array(
            'savePath' => 'images/gift',             //存储文件夹
            'maxSize'  => 5000,                   //允许的文件最大尺寸，单位KB
            'allowFiles' => array('.gif', '.png', '.jpg', '.jpeg', '.bmp')  //允许的文件格式
        );
        $this->load->library('uploader', $config);

        $file = $this->uploader->getFileInfo();

        return $file;
    }


//    /**
//     * 上传礼物图片
//     */
//     function uploadFile(){
//        $uploadConfig = array(
//            'upload_path' => './images/gift',
//            'allowed_types' => 'jpeg|jpg|png',
//            'encrypt_name' => TRUE,
//        );
//        $this->load->library('uploade', $uploadConfig);
//
//        if ( ! $this->upload->do_upload('giftfile'))
//        {
//            return -1;
//        }
//        else
//        {
//            $data = array('upload_data' => $this->upload->data());
//            return $data['full_path'];
//        }
//    }


    /**
    *   删除礼物
    */
    public function actionDelgift()
    {
        $this->postData['gid'] = $this->input->post('gid');
        $this->postData['mod'] = 'Gift';
        $this->postData['act'] = 'delGift';
        if( $this->env == 2 ){
            $url = $this->config->item('localUrl');
            $this->postData['demo'] = 1;
        }else{
            $url = $this->config->item('officialUrl');
        }
        $res = $this->curl->req($url, $this->postData);
        $retData = json_decode($res, TRUE);
        if( $retData['num'] > 0 ){
            $data['num'] = 1;
        }else{
            $data['num'] = 0;
        }
        die(json_encode($data));
    }

    /**
     * 生成配置文件
     */
    public function createConfigFile(){
        $this->postData['mod'] = 'Gift';
        $this->postData['act'] = 'createConfigFile';
        if( $this->env == 2 ){
            $url = $this->config->item('localUrl');
            $this->postData['demo'] = 1;
        }else{
            $url = $this->config->item('officialUrl');
        }
        $res = $this->curl->req($url, $this->postData);
    }

}