<?php

namespace App;

use App\Traits\CsvImportable;
use App\Traits\CsvSplitable;

class Staff2000Order extends RichSearchableModel
{
    use CsvImportable;
    use CsvSplitable;

    public $incrementing = false;
    public $errors = [];
    const CREATED_AT = NULL;
    const UPDATED_AT = 'modified';
    protected $table = 'tcd_staff2000order';
    protected $primaryKey = 'code';
    protected $fillable = [
        'code',
        '2co_code',
        'dispatch_destination_name',
        'dispatch_destination_dep',
        'dispatch_destination_zip',
        'dispatch_destination_address_1',
        'dispatch_destination_address_2',
        'dispatch_destination_address_3',
        'dispatch_destination_tel',
        'decree_26_duties_1',
        'decree_26_duties_2',
    ];
    const DEFAULT_LIST = [
        'number' => 100,
        'search_method' => 1,
        'custom_list_sorts' => [
            [
                'field' => 'code',
                'sort' => CustomListSort::DESC,
                'priority' => 1
            ]
        ],
        'custom_list_conditions' => [],
        'custom_list_fields' => [
            [
                'field' => '2co_code'
            ],
            [
                'field' => 'dispatch_destination_name'
            ],
            [
                'field' => 'dispatch_destination_dep'
            ],
            [
                'field' => 'dispatch_destination_address_1'
            ],
            [
                'field' => 'dispatch_destination_address_2'
            ],
            [
                'field' => 'dispatch_destination_address_3'
            ],
            [
                'field' => 'dispatch_destination_tel'
            ],
            [
                'field' => 'modified'
            ],
        ],
    ];

    const FIELD_NAME = [
        'code' => 'ｵｰﾀﾞｰｺｰﾄﾞ',
        '2co_code' => '就業先:派遣先CD',
        'dispatch_destination_name' => '就業先:派遣先(正式名称)',
        'dispatch_destination_dep' => '就業先:部署(正式名称)',
        'dispatch_destination_zip' => '就業先:郵便番号',
        'dispatch_destination_address_1' => '就業先:住所都道府県',
        'dispatch_destination_address_2' => '就業先住所市区郡',
        'dispatch_destination_address_3' => '就業先住所詳細',
        'dispatch_destination_tel' => '就業先:電話番号',
        'decree_26_duties_1' => '政令26業務の付随業務',
        'decree_26_duties_2' => '政令26業務の付随的業務',
        'modified' => 'レコード更新日',
    ];

    public static $function_id = CustomList::STAFF2000_ORDER;
    public static $simple_searchable_fields = [
        'code',
        '2co_code',
        'dispatch_destination_name',
        'dispatch_destination_dep',
        'dispatch_destination_zip',
        'dispatch_destination_address_1',
        'dispatch_destination_address_2',
        'dispatch_destination_address_3',
        'dispatch_destination_tel',
        'decree_26_duties_1',
        'decree_26_duties_2',
        'modified',
    ];

    protected $validateFields = [
        'date_field' => ['date', 'datetime'],
        'num_field' => [],
        'bool_field' => [],
    ];

    public function staff2000_allocation()
    {
        return $this->hasOne(Staff2000Allocation::class, '2or_code', 'code');
    }

    // 一覧画面向けアクセサ
    public function getIndex2coCodeAttribute()
    {
        if (empty($this->{'2co_code'})) return $this->{'2co_code'};
        return $this->getLinkHtml(url('staff2000_company/view/'.$this->{'2co_code'}), $this->{'2co_code'}, true);
    }

    public function getIndexDispatchDestinationNameAttribute()
    {
        if (empty($this->dispatch_destination_name)) return $this->dispatch_destination_name;
        return $this->getLinkHtml(url('staff2000_company/view/'.$this->{'2co_code'}), $this->dispatch_destination_name, true);
    }

    public function getIndexCodeAttribute()
    {
        return $this->getLinkHtml(url('staff2000_allocation/view/'.$this->code), $this->code, true);
    }

    public function scopeModified($query, $condition, $searchMethod)
    {
        return $query->datetime($condition, $searchMethod);
    }

    public function getContractStatusAttribute()
    {
        return $this->staff2000_allocation->contract_status ?? '';
    }

    public function getFullAddressAttribute()
    {
        return $this->dispatch_destination_address_1.$this->dispatch_destination_address_2.$this->dispatch_destination_address_3;
    }
}
