<?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\ais\controller;
use app\ais\model\AisFund;
use app\ais\model\AisFundBill;
use app\ais\model\AisCouponSubsidize;
use app\ais\model\AisCoupon;
use app\ais\model\AisOrder;
use filter\Filter;

class Fund extends Common{

    public $mini_program   = []; 

    public function initialize() {
        parent::initialize();
        $this->assign('pathMaps',[['name'=>'采购基金','url'=>url("fund/index")]]);
    }

    /**
     * 采购基金
     * @return void
     */
    public function index(int $types = 0){
        $fund = AisFund::where($this->mini_program)->find();
        $counts = [];
        if(empty($fund)){
            $counts['balance']       = 0;
            $counts['subsidize']     = 0;
            $counts['subsidize_num'] = 0;
        }else{
            $counts['balance'] = $fund->balance;
            $counts['subsidize'] = $fund->subsidize;
            $counts['subsidize_num'] = $fund->subsidize_num;
        }
        $view['fund']  = $counts;
        if($types <= 1){
            $view['bill']  = AisFundBill::where($this->mini_program)->where(['types' => $types])->order('id desc')->paginate(10,false,['query'=>['types' => $types]]);
        }else{

            $view['bill']  = AisCouponSubsidize::with('coupon')->where($this->mini_program)->order('id desc')->paginate(10,false,['query'=>['types' => $types]]);
        }
        $view['types'] = $types;
        return view()->assign($view);
    }

    /**
     * 删除关联
     * @return void
     */
    public function delAisCoupon(int $id){
        $result = AisCouponSubsidize::where($this->mini_program)->where(['id' => $id])->delete();
        if($result){
            return json(['code'=>200,'msg'=>'操作成功']);
        }else{
            return json(['code'=>403,'msg'=>'删除失败']);
        }
    }  
    
    /**
     * 关联优惠券
     * @return void
     */
    public function winCoupon(){
        if(request()->isAjax()){
            $ids = input('post.ids/s');
            if(empty($ids)){
                return json(['code'=>0,'msg'=>'请选择要关联的优惠券']);
            }
            $ida = ids($ids,true);
            $data = [];
            foreach ($ida as $key => $value) {
                $data[$key]['member_miniapp_id'] = $this->member_miniapp_id;
                $data[$key]['coupon_id']         = $value;
                $data[$key]['how_much']          = 0;
            }
            $result = AisCouponSubsidize::insertAll($data);
            if($result){
                return json(['code'=>302,'msg'=>'关联补贴优惠券成功','data' =>[]]);
            }else{
                return json(['code'=>0,'msg'=>'关联补贴优惠券失败']);
            }
        }else{
            $coupon = AisCouponSubsidize::where($this->mini_program)->field('coupon_id')->select()->toArray();
            $coupon_ids  = [];
            if(!empty($coupon)){
                $coupon_ids = array_column($coupon,'coupon_id');
            }
            $view['lists']  = AisCoupon::where($this->mini_program)->whereNotIn('id',$coupon_ids)->order('id desc')->paginate(10);
            return view()->assign($view);
        }
    } 
    
    /**
     * 排序
     */
    public function howmuch(){
        if(request()->isAjax()){
            $data = [
                'sort' => input('post.sort/f'),
                'id'   => input('post.id/d'),
            ];
            $result = AisCouponSubsidize::where($this->mini_program)->where(['id' =>$data['id']])->update(['how_much' => $data['sort']]);
            if($result){
                return json(['code'=>200,'msg'=>'操作成功']);
            }else{
                return json(['code'=>0,'msg'=>'操作失败']);
            }
        }
    }


    /**
     * 重置分账队列
     * @return void
     */
    public function reset(string $order_no){
        $result = AisOrder::where(['state' => 1,'order_no' => Filter::filter_escape(substr($order_no, 0, -2))])->find();
        if($result && AisOrder::subsidy($result)){
            return enjson(200);
        }else{
            return enjson(0);
        }
    }
}