<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Validator;

class CustomerBaseController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        if(!Gate::allows('客户-查询')){ return redirect('denied');  }
        $customers = app('CustomerService')->paginate();
        return response()->view('maintenance.customer.index',compact("customers"));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        if(!Gate::allows('客户-录入')){ return redirect('denied');  }
        return response()->view('maintenance.customer.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  Request  $request
     * @return Response
     */
    public function store(Request $request)
    {
        if(!Gate::allows('客户-录入')){ return redirect('denied');  }
        $this->validator($request->input())->validate();
        app('CustomerService')->create([
            "code"=>$request->input("code"),
            "name"=>$request->input("name"),
            "company_name"=>$request->input("company_name"),
        ]);
        return response()->redirectTo("maintenance/customer")->with("successTip","成功创建客户“".$request->input("name")."”");
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        if(!Gate::allows('客户-编辑')){ return redirect('denied');  }
        $customer = app('CustomerService')->find($id);
        return response()->view('maintenance.customer.create',compact("customer"));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function update(Request $request, $id)
    {
        if(!Gate::allows('客户-编辑')){ return redirect('denied');  }
        $this->validator($request->input(),$id)->validate();
        $result = app('CustomerService')->update(["id"=>$id],[
            "code"=>$request->input("code"),
            "name"=>$request->input("name"),
            "company_name"=>$request->input("company_name"),
        ]);
        if ($result == 1){
            return response()->redirectTo("maintenance/customer")->with("successTip","成功修改客户“".$request->input("name")."”的信息");
        }
        return response()->view("exception.default",["code"=>"509"]);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return array
     */
    public function destroy($id)
    {
        if(!Gate::allows('客户-删除')){ return ["success"=>false,"data"=>"无权操作！"];  }
        $result = app('CustomerService')->destroy($id);
        if ($result == 1)return ["success"=>true];
        return ["success"=>false,"data"=>"删除了“".$result."”行"];
    }

    private function validator(array $params, $id = null)
    {
        return Validator::make($params,[
            'code'=>['required',$id?"unique:customers,code,$id":'unique:customers,code','max:20'],
            'name'=>['required','max:20'],
        ],[
            'required'=>':attribute 为必填项',
            'max'=>':attribute 字符过多或输入值过大',
            'unique'=>':attribute 已存在',
        ],[
            'code'=>'客户代码',
            'name'=>'客户名称',
        ]);
    }
}
