<?php
/**
 * 仓库管理
 * @Copyright (C) 2016 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Controller;
class ShopsDeliveryController extends BaseController{
	
	public function _initialize(){
        parent::_initialize();
    }
    /**
     * 列表页
     */
    public function index(){
		$map = array();
		$map['status'] = 1;
		$map['id'] = array('gt',0);
        $list = $this->lists('Shops', $map);
		
		// echo "<pre>";
		// print_r($list);exit;
        $this->assign('list', $list);
        $this->display();
    }
	/**
     * 列表页
     */
    public function tc_edit(){
		$shop_id = I('shop_id');
        $this->get_shipping_info($shop_id);
		$this->get_bagging_info($shop_id);
        $this->assign('shop_id', $shop_id);
        $this->display();
    }
	
    /**
     * 获取运费设置
     * @param $shop_id  仓库id，使用方法前先判断好权限
     */
    private function get_shipping_info($shop_id){
        $where=array();
		$where['shop_id']=$shop_id;
        $ShopsDelivery=M('ShopsDelivery')->where($where)->field('start_price,scale,delivery_price')->select();
        if (!empty($ShopsDelivery)) {
            foreach ($ShopsDelivery as $v) {
                // $moreimgarr['start_price'][] = $v['start_price'];
                $moreimgarr['scale'][] = $v['scale'];
                $moreimgarr['delivery_price'][] = $v['delivery_price'];
            }
			$shipping_price = $ShopsDelivery[0]['start_price'];
            $moreshipstr = implode('', $moreimgarr['start_price']) . implode('', $moreimgarr['scale']) . implode('', $moreimgarr['delivery_price']);
        }
        $this->assign('shipping_price', $shipping_price);
        $this->assign('shops_delivery', $ShopsDelivery);
        $this->assign('moreshipstr', md5($moreshipstr));
    }

    /**
     * 获取包装费
     * @param $shop_id 仓库id，使用方法前先判断好权限
     */
    private function get_bagging_info($shop_id){
        $where = $map = array();
		$where['shop_id']=$shop_id;
		$map['id']=$shop_id;
		$info = M('Shops')->where($map)->field('business_week_start,business_week_end,business_hours_start,business_hours_end')->find();
        $ShopsPackage = M('ShopsPackage')->where($where)->field(true)->find();
		if($ShopsPackage['to_time'] > 60){
			$ShopsPackage['time_type'] = 2;
			$ShopsPackage['to_time'] = sprintf("%.1f",$ShopsPackage['to_time']/60);
		}
		// $long_time_arr = array('0'=>'当天下单','1'=>'1','2'=>'2','3'=>'3','4'=>'4','5'=>'5','6'=>'6','7'=>'7','8'=>'8','9'=>'9','10'=>'10','11'=>'11','12'=>'12','13'=>'13','14'=>'14','15'=>'15','16'=>'16','17'=>'17','18'=>'18','19'=>'19','20'=>'20','21'=>'21','22'=>'22','23'=>'23','24'=>'24','25'=>'25','26'=>'26','27'=>'27','28'=>'28','29'=>'29','30'=>'30');
		$long_time_arr = array('0'=>'当天下单','1'=>'1','2'=>'2','3'=>'3','4'=>'4','5'=>'5','6'=>'6','7'=>'7','8'=>'8','9'=>'9','10'=>'10','20'=>'20','30'=>'30');
        $this->assign('long_time_arr',$long_time_arr);
        $this->assign($ShopsPackage);
        $this->assign($info);
    }
	/**
     * 更新仓库信息
     */
    public function update_setting(){
        $model = D('Shop/ShopsAccount');
        $postdata = I('post.');
		
        $jumpUrl = U(CONTROLLER_NAME . '/index');
        if(empty($postdata['shop_id'])){
			$this->error('参数错误！');
		}
		// if(empty($postdata['shipping_price'])){
			// $this->error('免邮金额必须大于0！');
		// }
		
		foreach($postdata['start_price'] as $k => $v){
			$postdata['start_price'][$k] = $postdata['shipping_price'];
		}
		unset($postdata['shipping_price']);
		// echo "<pre>";
		// print_r($postdata);exit;
        
        $msg = array_merge(array('success' => L('edit_ok'), 'error' => L('edit_error'), 'url' => $jumpUrl, 'ajax' => IS_AJAX), (array)$msg);
        
		$model->update_shipping_info($postdata);//更新配送信息
		$model->change_update_bagging_info($postdata);//更新包装费信息
		unset($postdata);
		$this->success($msg['success'], $msg['url'], $msg['ajax']);
    }
	/**
     * 列表页
     */
    public function kd_edit(){
		$shop_id = I('shop_id');
        $this->get_shipping_fee($shop_id);
        $this->assign('shop_id', $shop_id);
        $this->display();
    }
	public function get_shipping_fee($shop_id){
		$map = array();
		$map['shop_id'] = $shop_id;
		// $map['id'] = 1;
		$info = M('ShippingFee')->where($map)->field(true)->select();
		// echo "<pre>";
		// print_r($info);exit;
		$new_arr = array();
		foreach($info as $k => $v){
			if (empty($v)) $v['type'] = 1;
			$v['areas'] = unserialize($v['configure']);
			$v['region'] = explode(',', $v['area_region']);
			unset($v['configure'],$v['area_region']);
			$new_arr[] = $v;
		}
		// print_r($new_arr);exit;
		
		// echo "<pre>";
		// print_r($areas);print_r($region);print_r($info);exit;
		// $this->assign('region', $region);
		// $this->assign($areas);
		
		$this->assign('type', $new_arr[0]['areas']['type']);
		$this->assign('money', $new_arr[0]['areas']['money']);
		$this->assign('len',count($new_arr));
		$this->assign('new_arr',$new_arr);
		$this->assign('shop_id', $shop_id);
	}
	public function sub_edit(){
        $post = I('post.');
		
		// echo "<pre>";
		// print_r($post);exit;
        if ($post) {
            if(empty($post['regions'])){
                $this->error("配送区域不得为空");
            }else{
				$map = array();
				$map['shop_id'] = $post['shop_id'];
				M('ShippingFee')->where($map)->delete();
			}
            // $data['name'] = $post['name'];
			// if(empty($post['money'])){
				// $post['money'] = 0;
			// }
			foreach($post['regions'] as $k => $v){
				$info_ship['type'] = $post['type'];
				$info_ship['money'] = $post['money'];
				$info_ship['first_weight'] = $post['first_weight'][$k];
				$info_ship['first_weight_money'] = $post['first_weight_money'][$k];
				$info_ship['x_weight'] = $post['x_weight'][$k];
				$info_ship['x_weight_money'] = $post['x_weight_money'][$k];
				$regions = implode(',', $post['regions'][$k]);
				$data['configure'] = serialize($info_ship);
				$data['area_region'] = $regions;
				$data['shop_id'] = $post['shop_id'];
				$info = M('ShippingFee')->add($data);
			}
            if ($info) {
                $url = U(CONTROLLER_NAME . '/index');
                $this->success(L('DO_OK'), $url);
            } else {
                $this->error(L('DO_ERROR'));
            }
        }
    }
}
?>