<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

use live\Request\V20161101 as live;

class Member extends CI_Controller {

    function __construct() {
        parent::__construct();
        if(!$this->session->userdata('cust_code'))
        {
            redirect('index.php/site');
        }
    }

    function index() {
		
        $cust_code = $this->session->userdata('cust_code');
        $this->load->model('ser_users');
        $user = $this->ser_users->getOne($cust_code);
	
        $data['user'] = $user;
        $this->load->view('front/member',$data);
    }

    function data() {
	
        $cust_code = $this->session->userdata('cust_code');
		//$cust_code = '13311729032';
        $this->load->model('ser_users');
        $user = $this->ser_users->getOne($cust_code);

        $data['user'] = $user;
        $this->load->view('front/data',$data);
    }

    function data_save()
    {
		//header('Content-type: text/html;charset=GB2312');
        $name1 = $this->input->post('name1',true);
        $name2 = $this->input->post('name2',true);
        $sex1 = $this->input->post('sex1');
        $logo = $this->input->post('logo');
        $birthdate = $this->input->post('birthdate');
        $addr1_l1 = $this->input->post('addr1_l1');
		
        $data = array(
            'name1' =>  $name1,
            'name2' =>  $name2,
            'sex1' => $sex1,
            'logo' => $logo,
            'birthdate' => $birthdate,
            'addr1_l1' => $addr1_l1,
        );

		//echo $data['name1'];die;
	
        $this->load->model('ser_users');
        $this->ser_users->update($this->session->userdata('cust_code'),$data);

		redirect('index.php/member/data');
    }

    function card() {
        $this->load->view('front/card');
    }

    function consume1() {

		$cust_no = $this->session->userdata('cust_code');
		//$cust_no = '0417468624';


		$prev_year = date("Y-m-d",strtotime("-1 year"));
	    $this->load->model('ser_users');

	
	   $result = $this->ser_users->tran_sales_header_group_ceshi($cust_no,$prev_year)->result_array();
		foreach($result as $k=>$v)
		{
			$result[$k]['list'] = $this->ser_users->tran_sales_header_ceshi($cust_no,$v['tran_date'])->result_array();
		}
		
		$data['result'] = $result;
        $this->load->view('front/consume',$data);
    }

	
    function consume() {

		$cust_no = $this->session->userdata('cust_code');
		//$cust_no = '0417468624';

		$prev_year = date("Y-m-d",strtotime("-1 year"));
	    $this->load->model('ser_users');

	
	   $result = $this->ser_users->tran_sales_header_group($cust_no,$prev_year)->result_array();
	
		foreach($result as $k=>$v)
		{
			$result[$k]['month_zh'] = $this->Array_Month($v['tran_month']);
			$result[$k]['list'] = $this->ser_users->tran_sales_header($cust_no,$v['tran_year'],$v['tran_month'])->result_array();
		}
		
		$data['result'] = $result;
        $this->load->view('front/consume',$data);
    }

    function consume_info() {
		$check_no = $this->input->get('check_no',true);
        //$check_no = "001530";
        $this->load->model('ser_users');
        $consume_info = $this->ser_users->consume_one($check_no);
        $result = $this->ser_users->details($check_no)->result_array();
		//print_r($consume_info);die;
        $data['result'] = $result;
        $data['consume_info'] = $consume_info;
        $this->load->view('front/consume_info',$data);
    }

	//数字汉字互转
	function Array_Month($month)
	{
		$arr=array('一','二','三','四','五','六','七','八','九','十','十一','十二');
		foreach($arr as $key=>$val)
		{
			if($key+1==$month)
			{
				return iconv('gbk', 'UTF-8', $val);
			}
		}
	}

	//上传头像
	 function JqueryUploadFile()
    {
        $file = $_FILES['files'];
        $path = 'uppic/files/'.date("Y").'/'.date('m').'/'.date('d');
	
        if(!is_dir($path))
        {
            if (!mkdir($path,0777,true))
            {
                $result = array(
                    'code' => '205',
                    'message' => '创建文件夹失败'
                );
                exit(json_encode($result,JSON_UNESCAPED_UNICODE));
            }
        }

        $str = date("mdHis").rand(10000,99999).substr($file['name'],strripos($file['name'],'.'));

        $head_url = $path.'/'.$str;

        if(move_uploaded_file($file['tmp_name'],$head_url))
        {
            echo json_encode(array('file'=>base_url().$head_url));
        }
	
    }
}
