package com.sifang.modules.releorder.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.completionrectification.vo.CrmCompletionInfoVo;
import com.sifang.modules.releorder.entity.CrmInvoiceOperation;
import com.sifang.modules.releorder.service.CrmInvoiceOperationService;
import com.sifang.modules.releorder.service.CrmReleOrderService;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.sys.common.UserUtils;
import com.sifang.modules.sys.common.entity.User;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "${adminPath}/rele")
public class CrmReleOrderController extends BaseExceptionController {

    @Autowired
    private CrmReleOrderService releOrderService;

    @Autowired
    private CrmInvoiceOperationService invoiceOperationService;

    @RequestMapping(value = "releOrderPage")
    public String addCompletion(Model model, HttpServletRequest request){
        User user = UserUtils.getUser();
        model.addAttribute("userType", user.getUserType());
        return "modules/yangguang/releOrder/releOrderPage";
    }

    @ResponseBody
    @RequestMapping("/getReleOrderList")
    public Object getReleOrderList(HttpServletRequest request){
        LayuiPage<Record> page = new LayuiPage<>(request);
        Map<String, String> map = getParams(request);
        releOrderService.getReleOrderList(page, map);
        return page.renderJson();
    }

    @RequestMapping(value = "releOrderDetail")
    public String rectificationDetail(Model model, HttpServletRequest request){
        String releCode = request.getParameter("releCode");
        Record releOrder = releOrderService.getRectificationOrderStationList(releCode);
        model.addAttribute("releOrder", releOrder);
        return "modules/yangguang/releOrder/releOrderDetail";
    }

    @RequestMapping(value = "invoiceFileDetail")
    public String invoiceFileDetail(Model model, HttpServletRequest request){
        return "modules/yangguang/releOrder/invoiceFileDetail";
    }

    @RequestMapping(value = "invoiceUpload")
    public String invoiceUpload(Model model, HttpServletRequest request){
        String releCode = request.getParameter("releCode");
        List<CrmInvoiceOperation> invoiceList = invoiceOperationService.findInvoiceByreleCode(releCode);
        model.addAttribute("releCode", releCode);
        model.addAttribute("invoiceList", invoiceList);
        return "modules/yangguang/releOrder/invoiceUpload";
    }

    @RequestMapping(value = "invoiceInfoAdd")
    public String invoiceInfoAdd(Model model, HttpServletRequest request){
        String releCode = request.getParameter("releCode");
        model.addAttribute("releCode", releCode);
        return "modules/yangguang/releOrder/invoiceInfoAdd";
    }

    /**
     * 发票信息保存
     */
    @ResponseBody
    @RequestMapping("/saveInvoiceData")
    public Result saveInvoiceData(HttpServletRequest request, CrmInvoiceOperation invoiceOperation){
        return invoiceOperationService.saveInvoiceData(invoiceOperation);
    }

    @RequestMapping(value = "invoiceInfoEdit")
    public String invoiceInfoEdit(Model model, HttpServletRequest request){
        String releCode = request.getParameter("releCode");
        String id = request.getParameter("id");
        CrmInvoiceOperation invoice = invoiceOperationService.findInvoiceById(id);
        model.addAttribute("releCode", releCode);
        model.addAttribute("id", id);
        model.addAttribute("invoice", invoice);
        return "modules/yangguang/releOrder/invoiceInfoEdit";
    }

    @RequestMapping(value = "invoiceInfoDetail")
    public String invoiceInfoDetail(Model model, HttpServletRequest request){
        String releCode = request.getParameter("releCode");
        String id = request.getParameter("id");
        CrmInvoiceOperation invoice = invoiceOperationService.findInvoiceById(id);
        model.addAttribute("releCode", releCode);
        model.addAttribute("id", id);
        model.addAttribute("invoice", invoice);
        return "modules/yangguang/releOrder/invoiceInfoDetail";
    }

    @ResponseBody
    @RequestMapping("/submitInvoiceData")
    public Result submitInvoiceData(HttpServletRequest request){
        String releCode = request.getParameter("releCode");
        return invoiceOperationService.submitInvoiceData(releCode);
    }


}