<?php

namespace App\Http\Controllers\api\thirdPart\weixin;
/**
 * 接口路径（微信线上）的需要修改，因为route文件和路径都改成了新的标准
 */
use App\Unit;
use App\User;
use App\UserToken;
use App\Waybill;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;

class WxController extends Controller
{
    public function index(Request $request){
        $carrierArr=[];
        $carriersName=[];
        $api_token=$request->input('api_token');
        $user=UserToken::getUser($api_token);
        if ($user){
            foreach ($user->logistics as $logistic){
                array_push($carrierArr, $logistic->id);
                array_push($carriersName, $logistic->name);
            }
            $count=Waybill::select('id')->whereIn('logistic_id',$carrierArr)->whereDate('created_at',date("Y-m-d "))->count();
            $waybill=Waybill::orderBy('created_at','DESC')->select('created_at','waybill_number','wms_bill_number','carrier_bill')
                ->whereIn('logistic_id',$carrierArr)->where('status','!=','已完结')->where('status','!=','无模型')->get();
            return ['waybill'=>$waybill,'carriersName'=>$carriersName,'count'=>$count];
        }
        return ['error'=>'请检查您的登录账号!'];
    }

    public function show(Request $request){
        $units_name=[];
        $waybill_number=$request->input('waybill_number');
        $waybill=Waybill::select('id','waybill_number','wms_bill_number','owner_id','origination','destination','recipient','recipient_mobile',
                'carrier_weight','carrier_weight_unit_id','carrier_weight_other','carrier_weight_unit_id_other','pick_up_fee','carrier_bill')
            ->where('waybill_number',$waybill_number)->first();
        $units=Unit::select('name')->get();
        foreach ($units as $unit){
            array_push($units_name, $unit->name);
        }
        return ['waybill'=>$waybill,'units_name'=>$units_name];
    }

    public function update(Request $request){
        $id=$request->input('id');
        $validator=Validator::make($request->input(),[
            'carrier_bill'=>"required|max:50|unique:waybills,carrier_bill,$id",
            'pick_up_fee'=>'nullable|min:0|numeric|max:999999',
            'carrier_weight'=>'required|min:0|numeric|max:999999',
            'carrier_weight_unit_name'=>'required_with:carrier_weight',
            'carrier_weight_other'=>'nullable|min:0|numeric|max:999999',
            'carrier_weight_unit_other_name'=>'required_with:carrier_weight_other',
        ],[
            'required'=>':attribute 为必填项',
            'alpha_num'=>':attribute 应为字母或数字',
            'max'=>':attribute 字符过多或输入值过大',
            'min'=>':attribute 不得为负',
            'numeric'=>':attribute 应为数字',
            'unique'=>':attribute 已存在',
            'required_with'=>':attribute 未填',
        ],[
            'carrier_bill'=>'承运商单号',
            'pick_up_fee'=>'提货费',
            'carrier_weight'=>'计数一',
            'carrier_weight_unit_id'=>'计数单位一',
            'carrier_weight_other'=>'计数二',
            'carrier_weight_unit_other_name'=>'计数单位二',
        ]);
        if ($validator->fails()) {
            //将返回错误循环组装成字符串
            $arr = [];
            foreach ($validator->getMessageBag()->toArray() as $k=>$error){
                array_push($arr, $error[0]);
            }
            return [
                'success' => false,
                'errors' => $arr
            ];
        }
        $unit=Unit::where('name',$request->input('carrier_weight_unit_name'))->first();
        $unit_other=Unit::where('name',$request->input('carrier_weight_unit_other_name'))->first();
        $waybill=Waybill::find($id);
        $carrier_bill=$request->input('carrier_bill');
        $pick_up_fee=$request->input('pick_up_fee');
        $carrier_weight=$request->input('carrier_weight');
        $carrier_weight_unit_id=$unit->id;
        $carrier_weight_other=$request->input('carrier_weight_other');
        if ($unit_other){
            $carrier_weight_unit_id_other=$unit_other->id;
            $waybill->carrier_weight_unit_id_other=$carrier_weight_unit_id_other;
        }
        $waybill->carrier_bill=$carrier_bill;
        if($pick_up_fee)$waybill->pick_up_fee=$pick_up_fee;
        $waybill->carrier_weight=$carrier_weight;
        $waybill->carrier_weight_unit_id=$carrier_weight_unit_id;
        $waybill->carrier_weight_other=$carrier_weight_other;

        if ($waybill->save()){
            app('LogService')->log(__METHOD__,__FUNCTION__,json_encode($request->toArray()),Auth::user()['id']);
            return ['success'=>true];
        }
        else return ['success'=>false];
    }
}
