package com.gree.mall.manager.controller.order;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gree.mall.manager.bean.tax.RspOrderTax;
import com.gree.mall.manager.exception.RemoteServiceException;
import com.gree.mall.manager.helper.ResponseHelper;
import com.gree.mall.manager.logic.order.OrderLogic;
import com.gree.mall.manager.logic.order.OrderTaxLogic;
import com.gree.mall.manager.plus.entity.OrderTax;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.rmi.RemoteException;

@Slf4j
@RestController
@Api(value = "发票管理", tags = {"发票管理"})
@RequestMapping(value = "/tax", produces = "application/json; charset=utf-8")
public class OrderTaxController {

    @Autowired
    private OrderTaxLogic orderTaxLogic;

    @PostMapping("/update")
    @ApiOperation("上传发票pdf地址")
    public ResponseHelper updateTax(
            @ApiParam(value = "发票id", required = true) @RequestParam(required = true) String orderTaxId,
            @ApiParam(value = "发票pdf的url,使用urlencode编码", required = true) @RequestParam(required = true) String taxlink
    ) throws RemoteServiceException {
        orderTaxLogic.updateTax(orderTaxId, taxlink);
        return ResponseHelper.success();
    }

    @PostMapping("/offline")
    @ApiOperation("已线下处理")
    public ResponseHelper updateTax(
            @ApiParam(value = "发票id", required = true) @RequestParam(required = true) String orderTaxId
    ) throws RemoteServiceException {
        orderTaxLogic.offline(orderTaxId);
        return ResponseHelper.success();
    }

    @GetMapping("/list")
    @ApiOperation(value = "发票列表")
    public ResponseHelper<Page<RspOrderTax>> pageTax(
            @ApiParam(required = false, value = "开票状态true:已开发票,false:未开,不传:所有的") @RequestParam(required = false) Boolean status,
            @ApiParam(required = false, value = "订单号") @RequestParam(required = false) String orderId,
            @ApiParam(value = "页号",required = true) @RequestParam Integer pageNum,
            @ApiParam(value = "页大小",required = true) @RequestParam Integer pageSize,
            HttpServletRequest request
    ) throws Exception{

        IPage<RspOrderTax> page = orderTaxLogic.page(status,orderId,pageNum,pageSize,request);
        return ResponseHelper.success(page);
    }

    @PostMapping("/updateWkp")
    @ApiOperation("修改未开票信息")
    public ResponseHelper updateTaxInfo(
            @ApiParam(value = "orderTaxId",required = true)@RequestParam(required = true) String orderTaxId,
            @ApiParam(value = "公司名称或个人名",required = true)@RequestParam(required = true) String name,
            @ApiParam(value = "纳税人识别号",required = true)@RequestParam(required = true) String taxNo,
            @ApiParam(value = "收票人名称",required = true)@RequestParam(required = true) String receiverName,
            @ApiParam(value = "接收电话",required = true)@RequestParam(required = true) String receiverPhone,
            @ApiParam(value = "接收邮箱",required = true)@RequestParam(required = true) String receiverEmail,
            @ApiParam(value = "收票人地址",required = true)@RequestParam(required = true) String receiverAddress
     ) throws RemoteServiceException {
        orderTaxLogic.updateInfo(orderTaxId,name,taxNo,receiverName,receiverPhone,receiverEmail,receiverAddress);
        return ResponseHelper.success();
    }


    @GetMapping("/export")
    @ApiOperation("导出发票信息")
    public void exportOrderTax(
            @ApiParam(required = false, value = "开票状态true:已开发票,false:未开,不传:所有的") @RequestParam(required = false) Boolean status,
            @ApiParam(required = false, value = "订单号") @RequestParam(required = false) String orderId,
            HttpServletResponse response,
            HttpServletRequest request
    ) throws Exception {
        orderTaxLogic.exportOrderTax(status,orderId,request,response);
    }


}
