<?php

namespace app\admin\controller;

use app\admin\logic\CompanyLogic;
use app\BaseController;
use think\exception\ValidateException;
use think\facade\Config;
use think\facade\Validate;

//企业
class Company extends BaseController
{

    //获取企业列表
    public function list()
    {
        $param = $this->request->only([
            'page' => 1,
            'size' => 10,
            'title' => '',
            'contacts' => '',
            'mobile' => '',
            'status' => '',
        ], 'post');

        return CompanyLogic::list($param);
    }

    //获取全部企业
    public function all()
    {
        $keyword = $this->request->post('keyword', '');

        return CompanyLogic::all($keyword);
    }

    //添加企业
    public function add()
    {
        $param = $this->request->only(['title', 'contacts', 'mobile', 'remark' => '',], 'post');

        $val = Validate::rule(Config::get('validate_rules.CompanyAdd'));

        if (!$val->check($param)) throw new ValidateException($val->getError());

        return CompanyLogic::add($param);
    }

    //读取企业详情
    public function read()
    {
        $id = $this->request->post('id/d', 0);
        return CompanyLogic::read($id);
    }

    //编辑企业
    public function edit()
    {
        $param = $this->request->only(['id', 'title', 'contacts', 'mobile', 'remark' => '',], 'post');

        $val = Validate::rule(array_merge(Config::get('validate_rules.CompanyAdd'), ['id' => 'require|number|gt:0']));

        if (!$val->check($param)) throw new ValidateException($val->getError());
        return CompanyLogic::edit($param);
    }

    //企业启禁用
    public function status()
    {
        $param = $this->request->only(['id', 'status'], 'post');

        $val = Validate::rule(Config::get('validate_rules.status'));

        if (!$val->check($param)) throw new ValidateException($val->getError());
        return CompanyLogic::status($param);
    }

    //删除企业
    public function delete()
    {
        $id = $this->request->post('id/d', 0);
        return CompanyLogic::delete($id);
    }

}