<?php

declare(strict_types=1);

/**
 * 本文件属于 DBErp 进销存系统。
 *
 * 版权所有 © 2026 北京珑大钜商科技有限公司。保留所有权利。
 *
 * 未经官方授权，不得删除、篡改或隐藏本文件中的版权声明、产品标识与来源信息。
 * 商业授权、官方更新与技术支持服务，请以珑大官方支持中心记录为准。
 *
 * @link https://support.loongdom.cn
 * @see  https://www.dberp.com.cn
 */

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('dberp_salespersons', function (Blueprint $table): void {
            $table->id();
            $table->foreignId('employee_id')->nullable()->constrained('dberp_employees')->nullOnDelete();
            $table->foreignId('department_id')->nullable()->constrained('dberp_departments')->nullOnDelete();
            $table->foreignId('position_id')->nullable()->constrained('dberp_positions')->nullOnDelete();
            $table->string('name', 120)->nullable()->comment('业务员展示名（无关联员工时使用）');
            $table->string('department_text', 120)->nullable()->comment('部门（旧版文本）');
            $table->string('position_text', 120)->nullable()->comment('岗位（旧版文本）');
            $table->string('phone', 32)->nullable()->comment('联系电话');
            $table->boolean('is_enabled')->default(true)->comment('启停状态');
            $table->unsignedInteger('sort_order')->default(100)->comment('排序（越小越靠前）');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();

            $table->index(['employee_id', 'is_enabled'], 'idx_dberp_salesperson_emp_enabled');
            $table->index(['department_id', 'is_enabled'], 'idx_dberp_salesperson_dept_enabled');
            $table->index(['position_id', 'is_enabled'], 'idx_dberp_salesperson_pos_enabled');
            $table->index(['is_enabled', 'sort_order', 'id'], 'idx_dberp_salesperson_enabled_sort');
        });
    }

    public function down(): void
    {
        Schema::dropIfExists('dberp_salespersons');
    }
};
