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

class Shop extends Admin_Controller {


    private $category = array(
        0 => '全部',
        1 => '经典美食',
        2 => '休闲娱乐',
        3 => '其他'
    );

    public function __construct()
    {
        parent::__construct();
        $this->load->library('pagination');
        $this->load->model('shop_model', 'shop');
        $this->config->load('otherconf',TRUE);
    }
    /**
     * [index description]
     * @return [type] [description]
     */
    public function index($offset ='')
    {
        #配置分页信息
        $config['base_url'] = site_url('admin/shop/index');
        $config['total_rows'] = $this->shop->count();
        $config['per_page'] = 50;
        $config['uri_segment'] = 4;

        $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"];
        
        $listInfo = $this->shop->getSellerList($limit,$offset);

        $status = $this->config->item('shopStatus','otherconf');

        foreach ($listInfo as $k=>$v) {
            $listInfo[$k]['status'] = $status[$v['status']];
            //($v['level1_des'] != '')?  $listInfo[$k]['level1_des'] = '已开通': $listInfo[$k]['level1_des'] = '未开通';
        }
        $data['listInfo'] = $listInfo;
		$this->load->view('common/innerheader',$data);
		$this->load->view('admin/sellerList',$data);
        $this->load->view('common/footer');
		
    }

    public function edit($id){
        $data['areaInfo'] = $this->area_show();
        $shopinfo = $this->shop->getShopInfo($id);
        $data['info'] = $shopinfo;
        $data['category'] = $this->category;
        $this->load->view('common/innerheader');
        $this->load->view('admin/shop_edit',$data);
        $this->load->view('common/footer');
    }

    public function changeStatus(){
        $sid = $this->input->post('sid');
        $status = $this->input->post('status');
        $data = array('status'=>$status);
        $result = $this->shop->updateShopStatus($data,$sid); 
        if($result === true){
            echo json_encode(array('code' => 1, 'msg' => '成功'));exit;
        }else{
            echo json_encode($result);exit;
        }
    }

    public function add(){
        $data['areaInfo'] = $this->area_show();
        $this->load->view('common/innerheader',$data);
        $this->load->view('admin/shop_add',$data);
        $this->load->view('common/footer');
    }

    /**
     * [area_show 取得区域商圈]
     * @return [type] [关联数组]
     */
    public function area_show()
    {
        $this->load->model('area_model','area');
        return $this->area->area_list();
    }

    public function upload($mode){

        $path = '/card/uploads/';
        $config['upload_path']      = dirname(FCPATH).$path;
        $config['allowed_types']    = 'gif|jpg|png';
        $url = dirname(base_url());
        $config['file_name'] = date('YmdHis').rand(0,100);
        $this->load->library('upload', $config);

        if ( ! $this->upload->do_upload('avatar'))
        {
            $error = array('error' => $this->upload->display_errors());
            echo json_encode($error);exit;
        }
        else
        {
            $data = $this->upload->data();
            $url = $url.$path.$data['file_name'];

            $thumconfig['image_library'] = 'gd2';
            $thumconfig['maintain_ratio'] = TRUE;
            $thumconfig['source_image'] = dirname(FCPATH).$path.$data['file_name'];
            if($mode =="pic")
            {
                $thumconfig['width']     = 720;
                $thumconfig['height']   = 403; 
            }

            if($mode =="small_pics")
            {
                $thumconfig['width']     = 202;
                $thumconfig['height']   = 148; 
            }
            //详情图 670px 高度不限
            if($mode =="message_pics")
            {
                $thumconfig['width']     = 670;
            }
            $this->load->library('image_lib', $thumconfig);
            $this->image_lib->resize();
            list($width, $height, $type, $attr) = getimagesize($url);
            $pic['url'] = $url.'|'.$width.'|'.$height;     
            echo json_encode($pic);
            exit;

        }
       
    }
    public function pic_delete(){
        echo '{}';
    }

    public function store($id = 0){
        $rules_config = array(
            array(
                'field' => 'shopname',
                'label' => '店名',
                'rules' => 'trim|required|max_length[15]'
            ),
            array(
                'field' => 'address',
                'label' => '地址',
                'rules' => 'trim|required|max_length[255]'
            ),
            array(
                'field' => 'message',
                'label' => '详细规则',
                'rules' => 'trim|required'
            ),
            array(
                'field' => 'shop_des',
                'label' => '简介',
                'rules' => 'trim|required|max_length[255]'
            ),
            array(
                'field' => 'phone[]',
                'label' => '商家电话',
                'rules' => 'required|trim'
            ),
            array(
                'field' => 'loc',
                'label' => '坐标',
                'rules' => 'required|trim'
            ),
        );


        if(!$id){
            $rules_config[] = array(
                'field' => 'password1',
                'label' => '密码',
                'rules' => 'trim|required|min_length[6]|max_length[20]'
            );

            $rules_config[] = array(
                'field' => 'password2',
                'label' => '密码',
                'rules' => 'trim|required|matches[password1]'
            );            
            
        }

        $this->load->library('form_validation', $rules_config);
        $this->form_validation->set_data($_POST);


        if($this->form_validation->run() == FALSE)
        {
            print_r($this->form_validation->error_array());
        }
        else
        {
            $this->load->model('shop_model', 'shop');
            $shop_id = $id ? $this->shop->update($_POST, $id) : $this->shop->insert($_POST);
            $this->shop->updateShopTel($_POST['phone'], $shop_id);
            $this->shop->updateShopPic($_POST['small_pics'], 1, $shop_id);
            $this->shop->updateShopPic($_POST['pics'], 2, $shop_id);
            $this->shop->updateShopPic($_POST['message_pics'], 3, $shop_id);

        }
        redirect('admin/shop/index');
    }


    public function level(){
        $start_date = $this->input->post('start_date');
        $end_date = $this->input->post('end_date');
        $mode = $this->input->post('mode');
        $shop_id = $this->input->post('sid');
        $date = explode(',', $this->input->post('date'));

        $level2_des = $this->input->post('level2_des');
 



        $this->load->library('shop_service');
        $result = $this->shop_service->updateShopDate($date, $start_date, $end_date, $level2_des, $shop_id, $mode);

        if($result === true){
            echo json_encode(array('code' => 1, 'msg' => '成功'));exit;
        }else{
            echo json_encode($result);exit;
        }

    }


    public function level_info($sid){
        $this->load->model('shop_model');
        $info = $this->shop_model->getShopInfo($sid);


        if(empty($info['date'])){
            echo json_encode(array('code' => 0));
            exit;
        }
        $data['code'] = 1;
        $data['level2_des'] = $info['level2_des'];
        $data['start_date'] = date('Y-m-d', $info['start_time']);
        $data['end_date'] = date('Y-m-d', $info['end_time']);
        $data['extra'] = unserialize($info['date']);
        echo json_encode($data);
        exit;
    }

    public function password(){
        $this->load->model('shop_model');
        $new_password = $this->input->post('password');
        $sid = $this->input->post('sid');
        var_dump($this->shop_model->updatePassword($new_password, $sid));
    }




    public function shop_tel_valid($shop_tel){
   
        var_dump($shop_tel);exit;
    }


    public function change_sort($sid,$sort){
        $sid = intval($sid);
        $sort = intval($sort);

        $this->load->model('shop_model');
        $sort = $this->shop_model->updateShopStatus(array('sort' => $sort), $sid);
    }
}
