<?php
use Dcat\Admin\Admin;
use Dcat\Admin\Grid;
use Dcat\Admin\Form;
use Dcat\Admin\Grid\Filter;
use Dcat\Admin\Show;
use Dcat\Admin\Layout\Navbar;
use Illuminate\Support\Facades\DB;

/**
 * Dcat-admin - admin builder based on Laravel.
 * @author jqh <https://github.com/jqhph>
 *
 * Bootstraper for Admin.
 *
 * Here you can remove builtin form field:
 *
 * extend custom field:
 * Dcat\Admin\Form::extend('php', PHPEditor::class);
 * Dcat\Admin\Grid\Column::extend('php', PHPEditor::class);
 * Dcat\Admin\Grid\Filter::extend('php', PHPEditor::class);
 *
 * Or require js and css assets:
 * Admin::css('/packages/prettydocs/css/styles.css');
 * Admin::js('/packages/prettydocs/js/main.js');
 *
 */

$user = Admin::user();

if (!empty($user)) {
    $db = DB::connection('sqlsrv');

    //根据门店号查询是否存在客户信息
    $info=$db->selectOne("SELECT SX5.SXDM,SX5.SXMC,SX5.MID,KHDM,KHMC 
    FROM KEHU KH
    INNER JOIN KHSX5 SX5 ON KH.SX5_ID=SX5.MID
    WHERE KH.KHDM = '{$user->username}' AND SX5.MID NOT IN ('000', 'C000')");
    
    // $md=$db->select("SELECT KHDM,KHMC FROM KEHU WHERE KHDM = '{$user->username}' 
    // UNION ALL 
    // SELECT KHDM,KHMC FROM KEHU WHERE SX5_ID IN (SELECT SX5_ID FROM KEHU WHERE KHDM = '{$user->username}') 
    // AND SX5_ID NOT IN ('000', 'C000') AND KHMC NOT LIKE '%停%' AND KHDM<>'{$user->username}'");

    if ($user->username<>'admin' && empty($user->sddm)) {
        $user->sddm = $user->username; // 修改指定字段值
        $user->save(); // 保存修改
    }
    if ($user->username<>'admin') {
        $user->khsx5 = $info->SXDM;
        $user->save();
        Admin::navbar(function (Navbar $navbar) use($info){
            $navbar->right(view('select',['info'=>$info]));
        });
    }
}

// 注册全局自定义样式
Admin::style('div.col-md-12 {
    position: relative;
    width: 100%;
    padding-right: 0px;
    padding-left: 0px;
}
div.content div.content-wrapper {
    padding: 7rem 2rem 0;
}');

