<?php
// +----------------------------------------------------------------------
// | CatchAdmin [Just Like ~ ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2020 http://catchadmin.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://github.com/yanwenwu/catch-admin/blob/master/LICENSE.txt )
// +----------------------------------------------------------------------
// | Author: JaguarJack [ njphper@gmail.com ]
// +----------------------------------------------------------------------
namespace catchAdmin\device\excel;

use catcher\base\CatchRequest as Request;
use catchAdmin\device\model\Device as deviceModel;
use catcher\library\excel\ExcelContract;
use PhpOffice\PhpSpreadsheet\Style\Alignment;

class DeviceExport implements ExcelContract
{
    public $memory = '1024M';

    protected $badgeModel;

    public function __construct(deviceModel $deviceModel)
    {
        $this->deviceModel = $deviceModel;
    }

    /**
     * 设置头部
     *
     * @time 2020年09月08日
     * @return string[]
     */
    public function headers(): array
    {
        // TODO: Implement headers() method.
        return [
            '所属部门','告警状态','GPS编号','RFID编号','设备名称','设备状态','绑定物编号','设备型号','在线时间', '入库时间'
        ];
    }


    /**
     * 处理数据
     *
     * @time 2020年09月08日
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @return \think\Collection
     */
    public function sheets()
    {
        $list = $this->deviceModel->getDevicesExportList();
        foreach ($list as &$val) {
            $val->bind_number ="\t".$val->bind_number."\t";
            $val->imei ="\t".$val->imei."\t";
            $val->rfid ="\t".$val->rfid."\t";
            $val->name ="\t".$val->name."\t";
            if($val->device_state === 1){
                $val->use_state = '已注册';
            }else{
                $val->use_state = '未注册';
            }
        }
        return $list;
    }
    public function keys(): array
    {
        // TODO: Implement keys() method.
        return [
            'depart_name','alarm_state_text','imei','rfid','name','use_state','bind_number','model_text','online_time','created_at'
        ];
    }
    /**
     * 设置开始行
     *
     * @time 2020年09月08日
     * @return int
     */
    public function setRow()
    {
        return 2;
    }

    /**
     * 设置标题
     *
     * @time 2020年09月08日
     * @return array
     */
    public function setTitle()
    {
        return [
            'A1:I1', '导出设备', Alignment::HORIZONTAL_CENTER
        ];
    }
     /**
     * 设置宽度
     *
     * @time 2020年09月08日
     * @return array
     */
    public function setWidth()
    {
        return [
            'A' => 20,
            'B' => 20,
            'C' => 20,
            'D' => 20,
            'E' => 20,
            'F' => 20,
            'G' => 20,
            'H' => 20,
            'I' => 20,
            'J' => 20
        ];
    }
    public function getWorksheet($sheet)
    {
        // $sheet->getStyle('B')->getNumberFormat()->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
        // $sheet->getColumnDimension('B')->setAutoSize(true);
        // $sheet->getColumnDimension('C')->setAutoSize(true);
        // $sheet->getColumnDimension('D')->setAutoSize(true);
        // $sheet->getActiveSheet()->getColumnDimension('C')->setAutoSize(true);
        // $sheet->getActiveSheet()->getColumnDimension('D')->setAutoSize(true);
        return $sheet;
    }
}