package com.jwipc.order.controller.pc;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson2.JSON;
import com.jwipc.order.dto.AppointmentDayOffBaseDTO;
import com.jwipc.order.logic.AppointmentDayOffLogic;
import com.jwipc.order.model.query.AppointmentDayOffQuery;
import com.jwipc.security.LoginUtil;
import com.jwipc.sys.client.SysConfigClient;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static com.jwipc.order.constant.ModuleConst.MODULE_ROOT;

/**
 * <p>
 * 送货单预约记录表 前端控制器
 * </p>
 *
 * @author WT
 * @since 2024-03-05
 */
@Controller
@RequiredArgsConstructor
@RequestMapping(MODULE_ROOT + "/appointmentRecord")
public class AppointmentRecordPageController {

    private final AppointmentDayOffLogic appointmentDayOffLogic;
    private final SysConfigClient sysConfigClient;

    /**
     * 送货单预约记录 主页
     *
     * @return
     */
    @RequestMapping("/index")
    public String index(Model model) {
        model.addAttribute("isSupplier", LoginUtil.isSupplier() ? 1 : 0);
        return MODULE_ROOT + "/delivery/appointmentRecord/list";
    }

    /**
     * 预约送货页面 or 变更页面
     *
     * @param model       model
     * @param ids         送货单IDs or 预约记录的ids
     * @param factoryCode 工厂编码
     * @param dockerCode  码头编码
     * @return 页面
     */
    @GetMapping("/appointment")
    public String appointment(Model model, String id, String ids,
                              @RequestParam(required = false, defaultValue = "") String supplierCode,
                              @RequestParam(required = false, defaultValue = "") String supplierName,
                              String factoryCode, String dockerCode)
    {
        AppointmentDayOffQuery query = new AppointmentDayOffQuery();
        query.setStartTime(new Date());
        query.setEndTime(DateUtil.offsetDay(new Date(), 45));
        List<AppointmentDayOffBaseDTO> dtoList = appointmentDayOffLogic.selectDataByQuery(query);
        String appointmentDate = sysConfigClient.getTextConfig("appointment_date");
        Integer timeRange = appointmentDayOffLogic.getTimeRange(new Date(), appointmentDate, dtoList);

        model.addAttribute("appointmentDate", timeRange);
        model.addAttribute("id", id);
        model.addAttribute("ids", ids);
        model.addAttribute("factoryCode", factoryCode);
        model.addAttribute("dockerCode", dockerCode);
        model.addAttribute("supplierCode", supplierCode);
        model.addAttribute("supplierName", supplierName);
        //控制页面日期不可点击的数据
        List<Object> dayOffArr = dtoList.stream().map(t -> LocalDateTimeUtil.format(t.getDay(), "yyyy-M-d"))
                .collect(Collectors.toList());
        model.addAttribute("dayOffArr", JSON.toJSONString(dayOffArr));
        return MODULE_ROOT + "/delivery/appointmentRecord/appointment";
    }

    /**
     * 预约看板
     *
     * @return
     */
    @RequestMapping("/appointmentShow")
    public String appointmentShow(Model model) {
        String refreshMinute = sysConfigClient.getTextConfig("appointment_refresh_minute");
        model.addAttribute("factoryCode", "1400");
        model.addAttribute("dockerCode", "A1400");
        model.addAttribute("dockerId", 16);
        model.addAttribute("refreshMinute", refreshMinute);
        return MODULE_ROOT + "/delivery/appointmentRecord/showIndex";
    }

}
