package com.sqx.modules.invoice.controller;

import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.sqx.common.utils.Result;
import com.sqx.modules.invoice.entity.Invoice;
import com.sqx.modules.invoice.service.InvoiceService;
import com.sqx.modules.utils.excel.ExcelData;
import com.sqx.modules.utils.excel.ExportExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

@RestController
@Api(value = "发票管理", tags = {"管理端-发票管理"})
@RequestMapping(value = "/admin/invoice/")
public class AdminInvoiceController {
    @Autowired
    private InvoiceService invoiceService;

    @ApiOperation("获取所有发票列表")
    @GetMapping("adminGetIndentList")
    public Result adminGetIndentList(Integer page, Integer limit, Long userId, String phone, String indentNumber, Integer status, String startTime, String endTime) {
        return Result.success().put("data", invoiceService.adminGetIndentList(page, limit,userId,phone, indentNumber, status,startTime,endTime));
    }

    @ApiOperation("用户获取发票信息")
    @GetMapping("getInvoiceInfo")
    public Result getInvoiceInfo(Long invoiceId, String indentNumber) {
        return Result.success().put("data", invoiceService.getInvoiceInfo(invoiceId, indentNumber));
    }

    @ApiOperation("管理员修改发票信息")
    @PostMapping("updateInvoice")
    public Result updateInvoice(String ids,Integer status) {
        return invoiceService.updateInvoice(ids,status);
    }

    @ApiOperation("管理员修改发票信息")
    @PostMapping("updateInvoiceInfo")
    public Result updateInvoiceInfo(Invoice invoice) {
        return invoiceService.updateById(invoice)?Result.success():Result.error();
    }
    /**
     * 导出
     */
    @ApiOperation("发票导出")
    @ResponseBody
    @GetMapping("/export")
    public void export(HttpServletResponse response,
                       Integer userType,
                       Integer invoiceType,
                       String invoiceTitle,
                       String taxpayerNumber,
                       String bankDeposit,
                       String userPhone,
                       String indentId,
                       Integer status,
                       String startTime, String endTime
    ) throws Exception {
        ExcelData data = invoiceService.listExcel(userType, invoiceType, invoiceTitle, taxpayerNumber, bankDeposit, userPhone, indentId, status,startTime,endTime);
        ExportExcelUtils.exportExcel(response, "发票列表.xlsx", data);
    }
}