<?php
/**
 *  挖掘线索
 * @author  system
 * @version    1.0
 * @date 2018-11-19 08:23:08
 *
 */

namespace App\Http\Controllers\Admin\Company;

use App\Http\Controllers\Admin\Controller;
use App\Imports\CompanyInfoImport;
use App\Models\CompanyContactsModel;
use App\Models\CompanyInfoModel;
use App\Models\UserCompanyCollectionModel;
use Illuminate\Http\Request;
use App\Repositories\Company\InfoRepository;
use Maatwebsite\Excel\Facades\Excel;
use \PhpOffice\PhpSpreadsheet\IOFactory;

class InfoController extends Controller
{
    private $repository;

    public function __construct(InfoRepository $repository)
    {
        if (!$this->repository) $this->repository = $repository;
    }


    /*
     * 搜索页视图
     * */
    public function search()
    {
        $fields = (new CompanyInfoModel())->filterFields;
        return view('admin.company.info.search', compact('fields'));
    }

    /**
     * 列表页
     */
    function index(Request $request)
    {
        $search = $request->all();

        $order = array();
        if (isset($request['sort_field']) && $request['sort_field'] && isset($request['sort_field_by'])) {
            $order[$request['sort_field']] = $request['sort_field_by'];
        } else {
            $order['id'] = 'DESC';
        }

        $list = $this->repository->searchCompany($search, $order);

        if ($request->ajax()) {
            $view = view('admin.company.info.data', compact('list'))->render();
            return response()->json(['html' => $view]);
        }

        $fields = (new CompanyInfoModel())->filterFields;

        $user_id = \Auth::guard('admin')->user()->id;
        $hasCollection = UserCompanyCollectionModel::where('user_id', $user_id)->count();

        return view('admin.company.info.index', compact('list', 'fields', 'hasCollection'));
    }

    /***
     * 导入企业信息
     * @param Request $request
     */
    public function import(Request $request)
    {
        Excel::import(new CompanyInfoImport, request()->file('company_info'));
        $this->showMessage('操作成功');
    }

    /***
     * 企业详情页
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function view(Request $request)
    {
        $data = $this->repository->find(request('id'));
        $contacts = $data->contacts()->orderBy('sort', 'desc')->get();
        return view('admin.company.info.view', compact('data', 'contacts'));
    }


    /**
     * 删除
     */
    public function destroy(Request $request)
    {
        $bool = $this->repository->destroy($request->get('id'));
        if ($bool) {
            return $this->showMessage('操作成功');
        } else {
            return $this->showWarning("操作失败");
        }
    }

    /***
     * 添加联系人备注
     * @param Request $request
     * @return int
     */
    public function addremarks(Request $request)
    {
        $contact = CompanyContactsModel::find($request->get('id'));
        $contact->remark = $request->get('remark');
        $contact->save();

        return 200;
    }
}