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

class Exchange extends Score_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');        
        $this->load->model('score/Exchange_model', 'exchange');
        $this->load->model("User_model",'user');
        $this->data['now'] = 'goods';
        $this->status = array(1=>'未处理',4=>'已处理');
    } 
    /**
     * [index 兑换列表]
     * @return [type] [description]
     */
    public function index($gid='',$offset ='')
    {
        #配置分页信息
        $config['base_url'] = site_url('score/exchange/index/'.$gid);
        $config['total_rows'] = $this->exchange->count($gid);
        $config['per_page'] = 5;
        $config['uri_segment'] = 5;

        $config['first_link'] = '首页';
        $config['first_tag_open'] = '<li id="first">';
        $config['first_tag_close'] = '</li>';

        $config['last_link'] = '末页';
        $config['last_tag_open'] = '<li id="last">';
        $config['last_tag_close'] = '</li>';

        $config['next_link'] = '下一页';
        $config['next_tag_open'] = '<li id="down">';
        $config['next_tag_close'] = '</li>';

        $config['prev_link'] = '上一页';
        $config['prev_tag_open'] = '<li id="top">';
        $config['prev_tag_close'] = '</li>';
        $config['num_links'] = 5;

        $config['full_tag_open'] = '<div class="wrap"><div class="fenye"><ul>';
        $config['full_tag_close'] = '</ul></div></div>';
        
        #初始化分页类
        $this->pagination->initialize($config);

         #生成分页信息
        $data['pageinfo'] = $this->pagination->create_links();
        $limit = $config["per_page"];

        $term = array('gid'=>$gid,'status !='=>2);
        $listInfo = $this->exchange->getExchangeList($term,$limit,$offset);

        foreach ($listInfo as $k=>$v) {

            //$v['uid'] 链接discuz进行获得用户姓名
            $result = $this->user->getUserFromDiscuz($v['uid']);
            $listInfo[$k]['name'] =$result['username'];
            $listInfo[$k]['exchange_time'] = date("Y-m-d H:i:s",$v['exchange_time']);
            $listInfo[$k]['status'] = $this->status[$v['status']];
            $listInfo[$k]['c_status'] = $v['status'];
        }
        $data['listInfo'] = $listInfo;
        $data['title'] = '兑换管理';
        $data['cate'] = $this->checkCate($gid);
        //$this->_view('exchangeList',$data);
        $this->load->view('common/innerheader',$data);
       
        $this->load->view('admin/score/exchangeList',$data);
    }
    public function checkCate($id)
    {
        $this->load->model('score/goods_model', 'goods');
        $result = $this->goods->getGoodsInfo($id);
        return $result['cate'];
    }
    /**
     * [operate 兑换处理]
     */
    public function operate()
    {
       $data['status'] = $_POST['status'];
       $goods_id  = $this->exchange->update($data, $_POST['id']);
       echo $goods_id>0?$this->json(1,''):$this->json(2,'操作失败');
    }
}