package com.sifang.modules.supply.order.controller;

import com.jfinal.plugin.activerecord.Record;
import com.sifang.modules.sunshine.SystemSetting.utils.Result;
import com.sifang.modules.supply.order.dao.MaterialOutStockOrderDao;
import com.sifang.modules.supply.order.dao.MaterialOutStockOrderDetailDao;
import com.sifang.modules.supply.order.entity.CrmSupplyMaterialUsedOrder;
import com.sifang.modules.supply.order.entity.CrmSupplyMaterialUsedOrderDetail;
import com.sifang.modules.supply.order.request.OutStockReq;
import com.sifang.modules.supply.order.service.OutStockOrderService;
import com.sifang.modules.supply.stock.utils.MaterialUtils;
import com.sifang.modules.sys.entity.Ascription;
import com.sifang.modules.sys.util.CrmUtils;
import com.sifang.modules.sys.util.LayuiPage;
import com.sifang.modules.sys.web.BaseExceptionController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

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

/**
 * 出库单
 */
@Controller
@RequestMapping(value = "${adminPath}/outStockOrder")
public class OutStockOrderController extends BaseExceptionController {
    Logger logger = LoggerFactory.getLogger(OutStockOrderController.class);

    @Autowired
    private OutStockOrderService outStockOrderService;
    @Autowired
    private MaterialOutStockOrderDao outStockOrderDao;
    @Autowired
    private MaterialOutStockOrderDetailDao outStockOrderDetailDao;

    /**
     * 出库单页面
     */
    @RequestMapping(value = "toOutStockOrderPage")
    public String toOutStockOrderPage(Model model) {
        List<Record> warehouseList = MaterialUtils.getWarehouseList();
        model.addAttribute("warehouseList", warehouseList);
        return "modules/supply/order/outStockOrderList";
    }

    /**
     * 出库单列表数据
     */
    @ResponseBody
    @RequestMapping(value = "ajaxForOutStockOrderData")
    public Object ajaxForOutStockOrderData(HttpServletRequest request) {
        LayuiPage page = new LayuiPage(request);
        Map<String, String> map = getParams(request);
        Ascription ascription = CrmUtils.getAscription();
        map.put("ascriptionId", ascription.getAscriptionId());
        logger.info(" into ajaxForOutStockOrderData, map={}", map);
        outStockOrderService.getOutStockOrderPage(page, map);
        return page.renderJson();
    }

    /**
     * 新增页面
     */
    @RequestMapping(value = "toAddForm")
    public String toAddForm(Model model) {
        List<Record> warehouseList = MaterialUtils.getWarehouseList();
        model.addAttribute("warehouseList", warehouseList);
        return "modules/supply/order/addOutStockOrder";
    }

    /**
     * 保存出库单
     */
    @ResponseBody
    @RequestMapping(value = "saveOrder")
    public Result saveOrder(@RequestBody @Valid OutStockReq req) {
        logger.info(" into saveOrder, req={}", req);
        outStockOrderService.saveOrder(req);
        return Result.ok();
    }

    /**
     * 出库单详情
     */

    @RequestMapping(value = "toDetailsForm")
    public String toDetailsForm(String id, Model model) {
        logger.info(" into toDetailsForm, id={}", id);
        CrmSupplyMaterialUsedOrder order = outStockOrderDao.get(id);
        List<CrmSupplyMaterialUsedOrderDetail> details = outStockOrderDetailDao.listDetails(id);
        List<Record> warehouseList = MaterialUtils.getWarehouseList();
        model.addAttribute("warehouseList", warehouseList);
        model.addAttribute("order", order);
        model.addAttribute("details", details);
        return "modules/supply/order/outStockOrderDetail";
    }

    /**
     * 查看物料清单
     */
    @RequestMapping(value = "materialDetails")
    public String materialDetails(String id, Model model) {
        List<CrmSupplyMaterialUsedOrderDetail> details = outStockOrderDetailDao.listDetails(id);
        model.addAttribute("details", details);
        return "modules/supply/order/outStockOrderMaterialList";
    }

}
