<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 团购联盟
 */
namespace app\allwin\controller\Manage;
use app\allwin\controller\Common;
use app\allwin\model\AllwinUnmarket;
use app\allwin\model\AllwinUnmarketOrder;
use app\allwin\model\AllwinUnmarketStore;
use app\allwin\model\AllwinUnmarketVerif;
use app\allwin\model\AllwinUnmarketBooking;
use app\allwin\model\AllwinStore;

class Unmarket extends Common{

    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'团购联盟','url' => url('manage.unmarket/index')]]);
    }

    /**
     * 列表
     */
    public function index(){
        $condition = [];

        $condition['member_miniapp_id'] = $this->member_miniapp_id;
        $view['lists'] = AllwinUnmarket::where($condition)->order('id desc')->paginate(20); 
        return view()->assign($view);
    }


    /**
     * 添加
     */
    public function add(){
        if(request()->isAjax()){
            $data = [
                'uid'               => input('post.uid/d'),
                'member_miniapp_id' => $this->member_miniapp_id,
                'telphone'          => input('post.telphone/s'),
                'mycode'            => input('post.mycode/s'),
                'tips'              => input('post.tips/s'),
                'title'             => input('post.title/s'),
                'bonus_types'       => input('post.bonus_types/d'),
                'bonus_user_types'  => input('post.bonus_user_types/d'),
                'share_ratio_user'  => input('post.share_ratio_user/d'),
                'share_ratio'       => input('post.share_ratio/f'),
                'price'             => input('post.price/f'),
                'name'              => input('post.name/s'),
                'address'           => input('post.address/s'),
                'tel'               => input('post.address/s'),
                'share_img'         => input('post.share_img/s'),
                'share_title'       => input('post.share_title/s'),
                'share_text'        => input('post.share_text/s'),
                'music'             => input('post.music/s'),
                'note'              => input('post.note/s'),
                'img'               => input('post.img/s'),
                'imgs'              => input('post.imgs/a'),
                'start_time'        => input('post.start_time/s'),
                'end_time'          => input('post.end_time/s'),
            ];
            $validate = $this->validate($data,'Unmarket.add');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = AllwinUnmarket::edit($data);
            if($result){
                return json(['code'=>200,'url'=>url('manage.unmarket/index'),'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            return view();
        }
    }

    //编辑
    public function edit(){
        if(request()->isAjax()){
            $data = [
                'id'                => input('post.id/d'),
                'uid'               => input('post.uid/d'),
                'member_miniapp_id' => $this->member_miniapp_id,
                'telphone'          => input('post.telphone/s'),
                'mycode'            => input('post.mycode/s'),
                'tips'              => input('post.tips/s'),
                'title'             => input('post.title/s'),
                'bonus_types'       => input('post.bonus_types/d'),
                'bonus_user_types'  => input('post.bonus_user_types/d'),
                'share_ratio_user'  => input('post.share_ratio_user/d'),
                'share_ratio'       => input('post.share_ratio/f'),
                'price'             => input('post.price/f'),
                'share_img'         => input('post.share_img/s'),
                'share_title'       => input('post.share_title/s'),
                'share_text'        => input('post.share_text/s'),
                'music'             => input('post.music/s'),                
                'note'              => input('post.note/s'),
                'img'               => input('post.img/s'),                
                'imgs'              => input('post.imgs/a'),
                'start_time'        => input('post.start_time/s'),
                'end_time'          => input('post.end_time/s'),
            ];
            $validate = $this->validate($data,'Unmarket.edit');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = AllwinUnmarket::edit($data);
            if($result){
                return json(['code'=>200,'url'=>url('manage.unmarket/index'),'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $condition = [];
            $condition['id']                = input('get.id/d');
            $condition['member_miniapp_id'] = $this->member_miniapp_id;
            $info = AllwinUnmarket::where($condition)->find();
            if(!$info){
                $this->error("404 NOT FOUND");
            }
            $info['imgs'] = json_decode($info['imgs'],true);
            $view['info'] = $info;
            return view()->assign($view);
        }
    }

    //删除
    public function delete(){
        $id = input('get.id/d');
        $result = AllwinUnmarketOrder::where(['share_id' => $id])->find();
        if(isset($result)){
            return json(['code'=>0,'msg'=>'有订单数据,禁止删除']);
        }
        $result = AllwinUnmarket::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $id])->delete();
        if($result){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败']);
        } 
    }

    /**
     * 联盟商家
     */
    public function store(int $id){
        $condition['member_miniapp_id'] = $this->member_miniapp_id;
        $condition['union_id']          = $id;
        $view['lists']    = AllwinUnmarketStore::where($condition)->order('id desc')->paginate(20); 
        $view['union_id'] = $id;
        $view['pathMaps'] = [
            ['name'=>'团购联盟','url' => url('manage.unmarket/index')],
            ['name'=>'联盟商家','url' => 'javascript:;'],
        ];
        return view()->assign($view);
    }

    /**
     * 添加商家
     */
    public function storeAdd(){
        if(request()->isAjax()){
            $store_id = input('post.store_id/d');
            if(empty($store_id)){
                return json(['code'=>0,'msg'=>'未找到好店']);
            }
            $store = AllwinStore::where(['id' => $store_id])->find();
            $data = [
                'union_id'          => input('post.union_id/d'),
                'member_miniapp_id' => $this->member_miniapp_id,
                'store_id'          => $store_id,
                'name'              => $store->name,
                'address'           => $store->address,
                'tel'               => $store->telphone,
                'end_time'          => input('post.endtime/s'),
                'content'           => input('post.content/s'),
                'note'              => input('post.note/s'),
                'about'             => input('post.about/s'),
                'pictext'           => input('post.pictext/s'),
                'img'               => input('post.img/s'),
                'imgs'              => input('post.imgs/a')
            ];
            $validate = $this->validate($data, 'Unmarket.store');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = AllwinUnmarketStore::edit($data);
            if($result){
                return json(['code'=>200,'url'=>url('manage.unmarket/store',['id' => $data['union_id']]),'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $union_id = input('get.union_id/d');
            $info = AllwinUnmarket::where(['id' => $union_id])->find();
            if(empty($info)){
                $this->error("添加所属资源不存在");
            }
            $view['union'] = $info;
            return view()->assign($view);
        }
    }

     /**
     * 店铺配置
     */   
    public function  storeEdit(){
        if(request()->isAjax()){
            $store_id = input('post.store_id/d');
            if(empty($store_id)){
                return json(['code'=>0,'msg'=>'操作失败']);
            }
            $store = AllwinStore::where(['id' => $store_id])->find();
            $data = [
                'id'                => input('post.id/d'),
                'union_id'          => input('post.union_id/d'),
                'member_miniapp_id' => $this->member_miniapp_id,
                'store_id'          => $store_id,
                'name'              => $store->name,
                'address'           => $store->address,
                'tel'               => $store->telphone,
                'end_time'          => input('post.endtime/s'),
                'content'           => input('post.content/s'),
                'note'              => input('post.note/s'),
                'about'             => input('post.about/s'),
                'pictext'           => input('post.pictext/s'),
                'img'               => input('post.img/s'),
                'imgs'              => input('post.imgs/a'),
            ];
            $validate = $this->validate($data,'Unmarket.store');
            if(true !== $validate){
                return json(['code'=>0,'msg'=>$validate]);
            }
            $result = AllwinUnmarketStore::edit($data);
            if($result){
                return json(['code'=>200,'url'=>url('manage.unmarket/store',['id' => $data['union_id']]),'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }else{
            $id   = input('get.id/d');
            $info = AllwinUnmarketStore::where(['member_miniapp_id' => $this->member_miniapp_id])->where(['id' => $id])->find();
            if(empty($info)){
                $this->error("商家不存在");
            }
            $view['info']     = $info;
            $view['union_id'] = input('get.union_id/d');
            $info['imgs']     = json_decode($info['imgs'],true);
            return view()->assign($view);
        }
    }

    /**
     * 删除商家
     */
    public function storeDelete(){
        $id = input('get.id/d');
        $result = AllwinUnmarketStore::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $id])->delete();
        if($result){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败']);
        } 
    }

    /**
     * 订单裂变
     */
    public function order(int $id,int $state = 0){
        $condition['state']             = $state ? 0: 1;
        $condition['member_miniapp_id'] = $this->member_miniapp_id;
        $condition['share_id']          = $id;
        $view['lists'] = AllwinUnmarketOrder::where($condition)->order('id desc')->paginate(20,false,['query'=>['id'=>$id,'state' => $state]]); 
        $view['state'] = $state;
        $view['id']    = $id;
        return view()->assign($view);
    }
    
    /**
     * 删除订单
     */
    public function deleteOrder(){
        $id = input('get.id/d');
        $result = AllwinUnmarketOrder::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $id])->delete();
        if($result){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败']);
        } 
    }


    /**
     * 核销列表
     */
     public function Verification(int $id){
        $view['info']  = AllwinUnmarket::where(['id' => $id])->field('title,note')->find();
        if(empty($view['info'])){
            $this->error("活动不存在");
        }
        $condition['union_id']          = $id;
        $condition['member_miniapp_id'] = $this->member_miniapp_id;
        $view['lists'] = AllwinUnmarketVerif::where($condition)->order('id desc')->paginate(20); 
        return view()->assign($view);
    }

    /**
     * 预约商家
     */
    public function Booking(){
        $condition['member_miniapp_id'] = $this->member_miniapp_id;
        $view['lists'] = AllwinUnmarketBooking::where($condition)->order('id desc')->paginate(20); 
        return view()->assign($view);
    }

    /**
     * 删除商家联系
     */
    public function deleteBooking(){
        $id = input('get.id/d');
        $result = AllwinUnmarketBooking::where(['member_miniapp_id' => $this->member_miniapp_id,'id' => $id])->delete();
        if($result){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>0,'msg'=>'删除失败']);
        } 
    }
}