<?php
/**
 * Ajax
 * @Copyright (C) 2018 汉潮 All rights reserved.
 * @License http://www.hanchao9999.com
 * @Author xiaogg <xiaogg@sina.cn>
 */
 
namespace Admin\Controller;
class AjaxController extends BaseController{
    public function choose_goods(){
        $postdata=I('post.');
		$map=array();
        $map['g.status']=1;
        //$map自行扩展
        $map['g.sale_type']=0;
        $map['g.goods_type']=5;
        $map['g.pt_id']=0;
        $map['g.promote_id']=0;
        if(!isset($postdata['keywords'])){
            $postdata['keywords']='';
        }
        if (is_numeric($postdata['keywords'])) {
            $map['g.id|g.goods_name'] = array(intval($postdata['keywords']), array('like', '%' . $postdata['keywords'] . '%'), '_multi' => true);
        } else if (!empty($postdata['keywords'])) {
            $map['g.goods_sn|g.goods_name'] = array('like', '%' . (string)$postdata['keywords'] . '%');
        }
        if (!empty($postdata['search_catid'])) {
            $cat_sons = get_children($postdata['search_catid'], 1,5);
            $catstr = implode(',', $cat_sons);
            $extension_goods_id = get_extension_goods($postdata['search_catid'], 1);
            $extensionstr = implode(',', $extension_goods_id);
            $where = array();
            if (!empty($cat_sons) && !empty($catstr)) {
                $where['g.cat_id'] = array('in', $cat_sons);
            }
            if (!empty($extension_goods_id) && !empty($extensionstr)) {
                $where['g.id'] = array('in', $extension_goods_id);
            }
            if (!empty($where)) {
                $where['_logic'] = 'or';
                $map['_complex'] = $where;
            }
        }
        $join="";
        if($postdata['shop_id']>0){
            $map['s.shop_id']=$postdata['shop_id'];
            $join=" LEFT JOIN __SHOPS_GOODS_STOCK__ s ON g.id=s.goods_id";
        }
        $total = M('Goods')->alias('g')->where($map)->join($join)->count();
        if(empty($total)){
            $return['status']=2;
            $return['info']='没有查询到商品';
            $this->ajaxReturn($return);//返回查询商品为空
        }
        $listRows=!empty($postdata['pagesize'])?$postdata['pagesize']:8;
        $page = new \Common\Vendor\Page($total, $listRows);
        $pageconfig='%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%';
        $page->setConfig('theme',$pageconfig);
        $page->rollPage=6;
        $pagehtml = $page->show();
        $list=array();
        $field=true;
        if($postdata['shop_id']>0){$field="g.*";}
        $list = M('Goods')->alias('g')->where($map)->join($join)->field($field)->limit($page->firstRow . ',' . $listRows)->select();
        if($postdata['out_price']){//如果是出货，调整出货价
            $goods_ids=array_map(function($val){return $val['id'];},$list);
            $postdata['goods_ids']=$goods_ids;
            $data=D('Common/InvoicingOrder')->count_goods_price($postdata);
            foreach($list as $ke=>$va){
                $list[$ke]['supply_price']=format_decimal($data[$va['id']]);
            }
        }
        $html='';
        $type=!empty($postdata['type'])?$postdata['type']:1;//1单选 2多选
        if(!in_array($type,array(1,2)))$type=1;
        foreach($list as $k=>$v){
            $input=$type==2?'<input type="checkbox" value="'.$v['id'].'" name="choose_goods" data-id="'.$v['id'].'" class="f_l" />':'<input value="'.$v['id'].'" name="choose_goods" data-id="'.$v['id'].'" type="radio" class="f_l" />';
            $html.=
            '<div class="choose_goods_item clearfix" title="'.$v['goods_name'].'" attr_data="'.$v['attr_key'].'" attr_name="'.$v['attr'].'" supplier_id="'.$v['supplier_id'].'" goods_id="'.$v['id'].'" unit_name="'.$v['unit_name'].'" goods_sn="'.$v['goods_sn'].'" supply_price="'.$v['supply_price'].'"><label>'.$input.
                '<img src="'.format_url($v['img']).'" />'.
                '<p>'.str_cut($v['goods_name'],70).'【'.$v['id'].'】</p><span>'.str_cut($v['attr'],70).'</span></label>'.
            '</div>';
        }
        $return['status']=1;
        $return['data']=$html;
        $return['page']=$pagehtml;
        $return['totalpages']=$page->totalPages;
        $this->ajaxReturn($return);

	}
    /**2018-03-15**/
    /**选择商品加属性***/
    //搜索单商品
    public function add_goods_list(){
        $return=array('status'=>1,'msg'=>'');
        $postdata=I('param.');
        $result=D('Common/InvoicingOrder')->search_by_sn($postdata);
        if($result['status']==0){
            $return['status']=0;
            $return['msg']=$result['msg'];
            $this->ajaxReturn($return);
        }
        $return['data']=$result['data'];
        $this->ajaxReturn($return);
    }
    //弹窗搜索商品
    public function search_goods_list(){
        $return=array('status'=>1,'msg'=>'');
        $postdata=I('post.');
        $result=D('Common/InvoicingOrder')->search_by_keyword($postdata);
        if($result['status']==0){
            $return['status']=0;
            $return['msg']=$result['msg'];
            $this->ajaxReturn($return);
        }
        $left_html=$right_html='';
        foreach($result['data']['left_list'] as $k=>$v){
            if(empty($v['goods_attrs'])){
                $left_html.='<div class="choose_goods_item clearfix">
                        <input type="checkbox" value="'.$v['index'].'" data-id="'.$v['index'].'" title="'.$v['goods_name'].'" attr_data="" attr_name="" supplier_id="'.$v['suppliers_id'].'" goods_id="'.$v['id'].'" unit_name="'.$v['unit_name'].'" supply_price="'.$v['supply_price'].'" goods_sn="'.$v['goods_sn'].'" stock="'.$v['stock'].'" name="choose_goods" class="check_box f_l" />
                        <dl class="flex_outside" onclick="name_search_select(\''.$v['index'].'\')">
                            <dt><img src="'.$v['img'].'"></dt>
                            <dd class="flex_inside">
                                <p class="ellipsis_one">'.str_cut($v['goods_name'],70).'</p>
                                <p class="ellipsis_one">条码:<span>'.$v['goods_sn'].'</span></p>
                                <p class="ellipsis_one">￥'.$v['supply_price'].'</p>
                            </dd>
                        </dl>
                    </div>';
            }else{
                $left_html.='<div class="choose_goods_item clearfix">
                            <em class="shrink f_l down_img" onclick="shrink(this)"><img src="'.__ROOT__.'/Public/images/more_dow.png" /></em>
                            <dl class="flex_outside">
                                <dt><img src="'.$v['img'].'"></dt>
                                <dd class="flex_inside">
                                    <p class="ellipsis_one">'.str_cut($v['goods_name'],70).'</p>
                                    <p class="ellipsis_one">条码:<span>'.$v['goods_sn'].'</span></p>
                                    <p class="ellipsis_one">￥'.$v['supply_price'].'</p>
                                </dd>
                            </dl>
                            <div class="goods_shuxing" style="display: none;">';
                foreach($v['goods_attrs'] as $ke=>$va){
                    $left_html.='<div class="goos_sx_item">
                                    <input type="checkbox" value="'.$va['index'].'" data-id="'.$va['index'].'" title="'.$va['goods_name'].'" attr_data="'.$va['attr_key'].'" attr_name="'.$va['name'].'" supplier_id="'.$v['suppliers_id'].'" goods_id="'.$va['goods_id'].'" unit_name="'.$va['unit_name'].'" supply_price="'.$va['supply_price'].'" goods_sn="'.$va['goods_sn'].'" goods_attr_sn="'.$va['goods_attr_sn'].'" stock="'.$va['stock'].'" name="choose_goods" class="check_box f_l" />
                                    <dl class="flex_outside" onclick="name_search_select(\''.$va['index'].'\')">
                                        <dt><img src="'.$va['img'].'"></dt>
                                        <dd class="flex_inside">
                                            <p class="ellipsis_one">'.$va['name'].'</p>
                                            <p class="ellipsis_one">条码:<span>'.$va['goods_attr_sn'].'</span></p>
                                            <p class="ellipsis_one">￥'.$va['supply_price'].'</p>
                                        </dd>
                                    </dl>
                                </div>';
                }
                $left_html.='</div>
                        </div>';
            }
        }
        foreach($result['data']['right_list'] as $k=>$v){
            if(empty($v['goods_attrs'])){
                $right_html.='<div class="choose_goods_item clearfix">
                        <input type="checkbox" value="'.$v['index'].'" data-id="'.$v['index'].'" title="'.$v['goods_name'].'" attr_data="" attr_name="" supplier_id="'.$v['suppliers_id'].'" goods_id="'.$v['id'].'" unit_name="'.$v['unit_name'].'" supply_price="'.$v['supply_price'].'" goods_sn="'.$v['goods_sn'].'" stock="'.$v['stock'].'" name="choose_goods" class="check_box f_l" />
                        <dl class="flex_outside" onclick="name_search_select(\''.$v['index'].'\')">
                            <dt><img src="'.$v['img'].'"></dt>
                            <dd class="flex_inside">
                                <p class="ellipsis_one">'.str_cut($v['goods_name'],70).'</p>
                                <p class="ellipsis_one">条码:<span>'.$v['goods_sn'].'</span></p>
                                <p class="ellipsis_one">￥'.$v['supply_price'].'</p>
                            </dd>
                        </dl>
                    </div>';
            }else{
                $right_html.='<div class="choose_goods_item clearfix">
                            <em class="shrink f_l down_img" onclick="shrink(this)"><img src="'.__ROOT__.'/Public/images/more_dow.png" /></em>
                            <dl class="flex_outside">
                                <dt><img src="'.$v['img'].'"></dt>
                                <dd class="flex_inside">
                                    <p class="ellipsis_one">'.str_cut($v['goods_name'],70).'</p>
                                    <p class="ellipsis_one">条码:<span>'.$v['goods_sn'].'</span></p>
                                    <p class="ellipsis_one">￥'.$v['supply_price'].'</p>
                                </dd>
                            </dl>
                            <div class="goods_shuxing" style="display: none;">';
                foreach($v['goods_attrs'] as $ke=>$va){
                    $right_html.='<div class="goos_sx_item">
                                    <input type="checkbox" value="'.$va['index'].'" data-id="'.$va['index'].'" title="'.$va['goods_name'].'" attr_data="'.$va['attr_key'].'" attr_name="'.$va['name'].'" supplier_id="'.$v['suppliers_id'].'" goods_id="'.$va['goods_id'].'" unit_name="'.$va['unit_name'].'" supply_price="'.$va['supply_price'].'" goods_sn="'.$va['goods_sn'].'" goods_attr_sn="'.$va['goods_attr_sn'].'" stock="'.$va['stock'].'" name="choose_goods" class="check_box f_l" />
                                    <dl class="flex_outside" onclick="name_search_select(\''.$va['index'].'\')">
                                        <dt><img src="'.$va['img'].'"></dt>
                                        <dd class="flex_inside">
                                            <p class="ellipsis_one">'.$va['name'].'</p>
                                            <p class="ellipsis_one">条码:<span>'.$va['goods_attr_sn'].'</span></p>
                                            <p class="ellipsis_one">￥'.$va['supply_price'].'</p>
                                        </dd>
                                    </dl>
                                </div>';
                }
                $right_html.='</div>
                        </div>';
            }
        }
        $return['data']['left_html']=$left_html;
        $return['data']['right_html']=$right_html;
        $return['page']=$result['data']['page'];
        $this->ajaxReturn($return);
    }
    /**
     * 模板页调取链接
     */
    public function ajax_diy_give(){
      $id=I('id');
      $vau=I('value');
      $m=M("UrlRules");
      $se=$m->where('id='. $id)->find();
      $result=array();
      $result['status']=1;
      $result['is_module']=$se['is_module'];//是否关联
      $result['urlparam']=$se['urlparam'];//是否参数
      $result['module']=$se['module'];
      $result['moduleid']=$se['moduleid'];
      $result['modulename']=$se['modulename'];
      $result['is_param']=$se['is_param'];//是否参数
      $result['urlparam']=$se['urlparam'];
      $result['theurl']=$se['theurl'];//z地址表达式           
      $is_module=$result['is_module'];
      $module=$result['module']; 
      $modulename=$result['modulename'];
      $moduleid=$result['moduleid'];
      $newarr = array(); 
      if($is_module == 1){
        $ms=M($module);
        if(empty($module))die;
        $fields=$ms->getDbFields();
        if($se['is_tree'] && in_array('pid', $fields)){
          $selarr=$ms->where('status=1')->field($moduleid." as id,pid,".$modulename.' as name')->select();
            $Tree = new \Common\Vendor\Tree();
          $newarr=$Tree->treemini($selarr);
          foreach($newarr as $k=>$v){
             $newarr[$k]['name'] = str_repeat('&nbsp;&nbsp;',$v['Count']).'|--'.$v['name']; 
             $newarr[$k]['fname'] = $v['name'];
          }
        }else{
          $map=array();
          if(in_array('status', $fields)){$map['status']=1;}
          if(in_array('is_delete', $fields)){$map['is_delete']=0;}
          if($module=='Goods'){$map['is_internet']=1;}          
          $selid=$ms->where($map)->field($moduleid.",".$modulename)->select();
          foreach ($selid as $ke => $val) {      
            $newarr[$ke]['id'] = $val[$moduleid]; 
            $newarr[$ke]['name'] = $val[$modulename];
          }
        }
      }
      if(!empty($newarr))$result['optin']=$newarr;
      $this->ajaxReturn($result);
  }
}