<?php namespace Excel\Lib;
use Dever;
Dever::apply('autoload', 'excel', 'vendor');
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;

class Export extends Core
{
    public function act($data = array(), $header = array(), $fileName = '', $sheet = 0, $sheetName = '', $return = false, $xls = false, $password = '', $save = '')
    {
        if (!$xls) {
            $xls = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
        }
        if ($sheet > 0) {
            $xls->createSheet();
        }
        $act = $xls->setActiveSheetIndex($sheet);
        if ($sheetName) {
            $act->setTitle($sheetName);
        }

        $dropdownOptions = []; // 存下拉列配置

        // ====== 多行表头处理 ======
        $row = 1;
        if ($header) {
            $headerRowCount = count($header);
            $colCount = max(array_map('count', $header));

            for ($r = 0; $r < $headerRowCount; $r++) {
                $rowHeader = $header[$r];
                for ($c = 0; $c < $colCount; $c++) {
                    $col = $this->cell[$c];
                    $value = $rowHeader[$c] ?? null;
                    if (is_array($value)) {
                        $act->setCellValue($col . $row, $value[0]);
                        $dropdownOptions[$col] = $value[1] ?? [];
                    } else {
                        $act->setCellValue($col . $row, $value);
                    }
                    $act->getColumnDimension($col)->setWidth(30);
                }
                $row++;
            }

            // 自动合并空单元格
            for ($r = 0; $r < $headerRowCount; $r++) {
                $rowHeader = $header[$r];
                $startCol = null;
                $mergeCount = 0;
                for ($c = 0; $c < $colCount; $c++) {
                    $col = $this->cell[$c];
                    $cellVal = $act->getCell($col . ($r+1))->getValue();
                    if ($cellVal !== null && $cellVal !== '') {
                        if ($mergeCount > 1 && $startCol !== null) {
                            $act->mergeCells($this->cell[$startCol] . ($r+1) . ':' . $this->cell[$c-1] . ($r+1));
                        }
                        $startCol = $c;
                        $mergeCount = 1;
                    } else {
                        $mergeCount++;
                    }
                }
                if ($mergeCount > 1 && $startCol !== null) {
                    $act->mergeCells($this->cell[$startCol] . ($r+1) . ':' . $this->cell[$colCount-1] . ($r+1));
                }
            }
        }

        // ====== 数据处理 ======
        if($data) {
            $i = 0;
            $height = $max = 80;
            foreach($data as $v) {
                $j = 0;
                foreach($v as $cell) {
                    $col = $this->cell[$j];
                    $addr = $col . ($i+$row);

                    $html = \Dever\Helper\Str::ishtml($cell);
                    if ($html) {
                        $wizard = new \PhpOffice\PhpSpreadsheet\Helper\Html;
                        $cell = $wizard->toRichTextObject('<?xml encoding="UTF-8">' . $cell);
                    }

                    if (!$html && (strstr($cell, '.jpg') || strstr($cell, '.gif') || strstr($cell, '.png'))) {
                        // === 图片处理逻辑保持原样 ===
                        $key = ($i+$row);
                        $value = false;

                        if (strpos($cell, '||')) {
                            $t = explode('||', $cell);
                            $cell = $t[1];
                            $value = $t[0];
                        }
                        $temp = explode(',', $cell);
                        foreach ($temp as $ck => $cv) {
                            $objDrawing[$ck] = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
                            if (!is_file($cv)) continue;
                            $objDrawing[$ck]->setPath($cv);
                            $objDrawing[$ck]->setHeight($height);
                            $objDrawing[$ck]->setCoordinates($addr);
                            $objDrawing[$ck]->setOffsetX(12);
                            $offsetY = ($ck == 0) ? 5 : ($offsetY + $height + 5);
                            $objDrawing[$ck]->setOffsetY($offsetY);
                            $objDrawing[$ck]->setWorksheet($act);
                        }
                        if ($value) {
                            $act->setCellValue($addr, $value);
                        }
                        $th = $height * count($temp);
                        if ($th > $max) $max = $th;
                        $act->getRowDimension($i+$row)->setRowHeight($max);
                    } else {
                        if (is_numeric($cell) && mb_strlen($cell) >= 10) $cell .= "\t";
                        $act->setCellValue($addr, $cell);
                        $act->getStyle($addr)->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
                    }

                    // ==== 下拉逻辑保持不变 ====
                    if (!empty($dropdownOptions[$col])) {
                        $validation = $act->getCell($addr)->getDataValidation();
                        $validation->setType(DataValidation::TYPE_LIST);
                        $validation->setErrorStyle(DataValidation::STYLE_INFORMATION);
                        $validation->setAllowBlank(false);
                        $validation->setShowInputMessage(true);
                        $validation->setShowErrorMessage(true);
                        $validation->setShowDropDown(true);
                        $validation->setFormula1('"' . implode(',', $dropdownOptions[$col]) . '"');
                    }

                    $act->getColumnDimension($col)->setAutoSize(true);
                    $act->getColumnDimension($col)->setWidth(30);
                    $j++;
                }
                $i++;
            }
        }

        // ====== 保存逻辑保持原样 ======
        if ($header && $return) {
            return $xls;
        }
        if (!$fileName) {
            $fileName = uniqid(time(),true);
        }
        $fileName .= '-' . date('Ymd-His') . '-' . rand(1000,9999);
        $write = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($xls, "Xlsx");
        if ($save) {
            $save = Dever::file('excel/' . $save . '.xlsx');
            $write->save($save);
            if ($password) {
                Dever::apply('autoload', 'excel', 'secure');
                $encrypt = new \Nick\SecureSpreadsheet\Encrypt();
                $encrypt->input($save)->password($password)->output($save);
            }
            return $save;
        } else {
            if ($password) {
                $save = Dever::file('excel/' . $save . '.xlsx');
                $write->save($save);
                Dever::apply('autoload', 'excel', 'secure');
                $encrypt = new \Nick\SecureSpreadsheet\Encrypt();
                $encrypt->input($save)->password($password)->output($save);
                header('Content-Type: application/octet-stream');
                header('Content-Transfer-Encoding: Binary');
                header("Content-disposition: attachment; filename=\"" . basename($save) . "\"");
                readfile($save);
                @unlink($save);
            } else {
                ob_end_clean();
                header('Content-Type: application/vnd.ms-excel');
                header('pragma:public');
                header("Content-Disposition:attachment;filename=$fileName.xlsx");
                $write->save('php://output');
            }
        }
    }
}
