<?php

namespace app\admin\model;

use think\Model;

/**
 * Test
 */
class Test extends Model
{
    // 表名
    protected $name = 'test';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 追加属性
    protected $append = [
        'city_text',
    ];

    // 字段类型转换
    protected $type = [
        'array'     => 'json',
        'timestamp' => 'timestamp:Y-m-d H:i:s',
    ];

    protected static function onAfterInsert($model)
    {
        if ($model->weigh == 0) {
            $pk = $model->getPk();
            if (strlen($model[$pk]) >= 19) {
                $model->where($pk, $model[$pk])->update(['weigh' => $model->count()]);
            } else {
                $model->where($pk, $model[$pk])->update(['weigh' => $model[$pk]]);
            }
        }
    }

    public function getImagesAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setImagesAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getFilesAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setFilesAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getCheckboxAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setCheckboxAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getSelectsAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setSelectsAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getFloatAttr($value): float
    {
        return (float)$value;
    }

    public function getCityAttr($value): array
    {
        if ($value === '' || $value === null) return [];
        if (!is_array($value)) {
            return explode(',', $value);
        }
        return $value;
    }

    public function setCityAttr($value): string
    {
        return is_array($value) ? implode(',', $value) : $value;
    }

    public function getCityTextAttr($value, $row): string
    {
        if ($row['city'] === '' || $row['city'] === null) return '';
        $cityNames = \think\facade\Db::name('area')->whereIn('id', $row['city'])->column('name');
        return $cityNames ? implode(',', $cityNames) : '';
    }

    public function getArrayAttr($value): array
    {
        return !$value ? [] : json_decode($value, true);
    }

    public function setTimeAttr($value): string
    {
        return $value ? date('H:i:s', strtotime($value)) : '';
    }

    public function getEditorAttr($value): string
    {
        return !$value ? '' : htmlspecialchars_decode($value);
    }
}