package com.bofeng.controller;

import com.bofeng.Consts;
import com.bofeng.JwtHelper;
import com.bofeng.entity.Order;
import com.bofeng.entity.User;
import com.bofeng.service.CashierService;
import com.bofeng.service.UserService;
import com.google.common.collect.Maps;
import com.yvan.Model;
import com.yvan.ModelOps;
import com.yvan.PageDb;
import com.yvan.mvc.Pd;
import com.yvan.platform.Conv;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@RestController
@RequiresPermissions(Consts.USER_TYPE_CASHIER)
public class CashierCustomerAccountsController {

    @Autowired
    private CashierService cashierService;

    @Autowired
    private UserService userService;

    @GetMapping("/cashier/customer_accounts.html")
    public ModelAndView customerAccounts() {
        System.out.println(JwtHelper.getUserType());
        return new ModelAndView("/cashier/customer_accounts.ftl");
    }

    @GetMapping("/cashier/customer_accounts.json")
    public Model<List<Order>> getCustomerAccounts(PageDb pageDb,
                                                  @Pd(name = "saleName", required = false, defaultValue = "") String saleName,
                                                  @Pd(name = "orderStartAt", required = false, defaultValue = "") String orderStartAt,
                                                  @Pd(name = "orderEndAt", required = false, defaultValue = "") String orderEndAt,
                                                  @Pd(name = "roomName", required = false, defaultValue = "") String roomName,
                                                  @Pd(name = "customerName", required = false, defaultValue = "") String customerName) {
        Map<String, Object> queryParam = Maps.newLinkedHashMap();
        queryParam.put("saleName", saleName);
        queryParam.put("orderStartAt", orderStartAt);
        queryParam.put("orderEndAt", orderEndAt);
        queryParam.put("roomName", roomName);
        queryParam.put("customerName", customerName);
        queryParam.put("paid", Consts.PAID_NO);
        User currentUser = userService.getUserById(Conv.NL(JwtHelper.getUserId()));
        queryParam.put("placeId", currentUser.getPlaceId());

        return Model.newSuccess(pageDb, cashierService.search(pageDb, queryParam));
    }

    @PostMapping("/cashier/order_discard.json")
    public ModelOps orderDiscard(@Pd(name = "orderId") Long orderId) {
        return ModelOps.newSuccess(cashierService.discard(orderId));
    }

    @PostMapping("/cashier/pay_order.json")
    public ModelOps accountsAmount(@Pd(name = "orderId") Long orderId,
                                   @Pd(name = "payAmount") BigDecimal payAmount) {
        return ModelOps.newSuccess(cashierService.pay(orderId, payAmount));
    }

}
