package com.bohuikeji.delivery.backstage.controller.views;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bohuikeji.delivery.backstage.entity.*;
import com.bohuikeji.frame.module.admin.base.PageTemplateControllerBase;
import com.bohuikeji.frame.module.admin.entity.back.PageEntity;
import com.bohuikeji.frame.module.admin.entity.sys.SysUser;
import com.bohuikeji.frame.module.admin.entity.web.block.ButtonBlock;
import com.bohuikeji.frame.module.admin.entity.web.event.Function;
import com.bohuikeji.frame.module.admin.entity.web.template.TableTemplate;
import com.bohuikeji.frame.module.admin.enums.ButtonBlockEnum;
import com.bohuikeji.frame.module.admin.service.ITFranchiseeInformationService;
import com.bohuikeji.frame.module.admin.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

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

/**
 * 商户共享页面
 * @author 10552
 */
@Slf4j
@Controller
@RequestMapping("/views/merchasnt")
public class MerchantPage extends PageTemplateControllerBase {
    @Autowired
    ITFranchiseeInformationService franchiseeInformationService;

    /**
     * 订单管理 ->待接单管理
     *
     * @return
     */
    @GetMapping("/table/order/wait")
    public PageEntity toOrderWait(HttpServletRequest request) {
        //创建模板页面
        TableTemplate pageTemplate = (TableTemplate) createPage(TableTemplate.class);
        pageTemplate.setTableModel(pageTemplate.getTableModel().setUrl("/order/table/model/form"));
        List<ButtonBlock> buts = new ArrayList<>(4);
        buts.add(ButtonBlock.createButton(ButtonBlockEnum.ADD, "下单").setIcon("mdi mdi-playlist-plus"));
        buts.add(ButtonBlock.createButton(ButtonBlockEnum.ADD, "快捷下单").setIcon("mdi mdi-airplane-takeoff").setStyle("btn btn-purple").setOnClick("quiteOrder()"));
        buts.add(new ButtonBlock(null).setStyle("btn btn-info m-r-5").setIcon("mdi mdi-delete-empty").setTitle("取消订单").setOnClick("orderRefund()"));
        //buts.add(ButtonBlock.createButton(ButtonBlockEnum.ADD, "查询第三方推送结果").setOnClick("showThirdResult()").setIcon("mdi mdi-magnify"));
        //buts.add(ButtonBlock.createButton(ButtonBlockEnum.DELETE, "删除订单"));
        pageTemplate.setBatchButton(buts);
        QueryWrapper<TOrder> tOrderQueryWrapper = getFranQueryWrapper();
        tOrderQueryWrapper.ne("is_test",1);
        tOrderQueryWrapper.eq("player_id",getLoginUser().getCustomerId());
        tOrderQueryWrapper.eq("order_batch_num","0");
        tOrderQueryWrapper.eq("order_status", "待接单");
        pageTemplate.setQueryWrapper(tOrderQueryWrapper);
        session.setAttribute("pageTemplate", pageTemplate);
        return PageEntity.page("/order/wait");
    }

    /**
     * 订单管理 ->进行中管理
     *
     * @return
     */
    @GetMapping("/table/order/underway")
    public PageEntity toOrderUnderway(HttpServletRequest request) {
        List<ButtonBlock> buts = new ArrayList<>(3);
        buts.add(new ButtonBlock(null).setStyle("btn btn-info m-r-5").setIcon("mdi mdi-delete-empty").setTitle("取消订单").setOnClick("orderRefund()"));
        TableTemplate pageTemplate = (TableTemplate) createPage(TableTemplate.class);
        pageTemplate.setTableModel(pageTemplate.getTableModel().setUrl("/order/table/model/form"));
        pageTemplate.setBatchButton(buts);
        QueryWrapper<TOrder> tOrderQueryWrapper = getFranQueryWrapper();
        tOrderQueryWrapper.ne("is_test",1);
        tOrderQueryWrapper.eq("player_id",getLoginUser().getCustomerId());
        tOrderQueryWrapper.eq("order_batch_num","0");
        tOrderQueryWrapper.in("order_status", "待取件", "取件中", "待派送", "派送中","待支付");
        pageTemplate.setQueryWrapper(tOrderQueryWrapper);
        session.setAttribute("pageTemplate", pageTemplate);
        return PageEntity.page("/order/underway");
    }

    /**
     * 订单管理 ->已完成管理
     *
     * @return
     */
    @GetMapping("/table/order/complete")
    public PageEntity toOrderComplete(HttpServletRequest request) {
        TableTemplate pageTemplate = (TableTemplate) createPage(TableTemplate.class);
        pageTemplate.setTableModel(pageTemplate.getTableModel().setUrl("/order/table/model/form"));
        List<ButtonBlock> list = new ArrayList<>(0);
        //list.add(ButtonBlock.createButton(ButtonBlockEnum.ADD, "订单结算"));
        pageTemplate.setBatchButton(list);
        //设置订单状态
        QueryWrapper<TOrder> tOrderQueryWrapper = getFranQueryWrapper();
        tOrderQueryWrapper.ne("is_test",1);
        tOrderQueryWrapper.eq("player_id",getLoginUser().getCustomerId());
        tOrderQueryWrapper.eq("order_batch_num","0");
        tOrderQueryWrapper.in("order_status", "已完成", "待评价");
        pageTemplate.setQueryWrapper(tOrderQueryWrapper);
        session.setAttribute("pageTemplate", pageTemplate);
        return PageEntity.page("/order/complete");
    }

    /**
     * 异常管理管理——待处理
     *
     * @return
     */
    @GetMapping("/table/order/exception/wait")
    public PageEntity toOrderExceptionWait() {
        TableTemplate pageTemplate = (TableTemplate) createPage(TableTemplate.class);
        List<ButtonBlock> list = new ArrayList<>(0);
        pageTemplate.setBatchButton(list);
        String sql=getFranSqlByKey("t_order","order_id");
        QueryWrapper<TOrderExceptionEntity> queryWrapper=new QueryWrapper<TOrderExceptionEntity>().eq("exception_status","待处理");
        if(sql!=null){
            queryWrapper.inSql("order_id",sql);
        }
        session.setAttribute("pageTemplate", pageTemplate.setQueryWrapper(queryWrapper));
        return PageEntity.page("/order/exception_wait");
    }
    /**
     * 异常管理管理——处理历史
     *
     * @return
     */
    @GetMapping("/table/order/exception/history")
    public PageEntity toOrderException() {
        TableTemplate pageTemplate = (TableTemplate) createPage(TableTemplate.class);
        List<ButtonBlock> list = new ArrayList<>(0);
        pageTemplate.setBatchButton(list);
        String sql=getFranSqlByKey("t_order","order_id");
        QueryWrapper<TOrderExceptionEntity> queryWrapper=new QueryWrapper<TOrderExceptionEntity>().eq("exception_status","处理完成");
        if(sql!=null){
            queryWrapper.inSql("order_id",sql);
        }
        session.setAttribute("pageTemplate", pageTemplate.setQueryWrapper(queryWrapper));
        return PageEntity.page("/order/exception_history");
    }

    /**
     * 订单管理 ->全部订单
     *
     * @return
     */
    @GetMapping("/table/order/all")
    public PageEntity toOrderAll() {
        List<ButtonBlock> buts = new ArrayList<>(1);
        ///buts.add(ButtonBlock.createButton(ButtonBlockEnum.EDIT, "查看第三方配送信息").setOnClick("showThirdDetail()").setIcon("mdi mdi-gesture-tap"));
        //buts.add(ButtonBlock.createButton(ButtonBlockEnum.ADD, "查询第三方推送结果").setOnClick("showThirdResult()").setIcon("mdi mdi-magnify"));
        //buts.add(ButtonBlock.createButton(ButtonBlockEnum.EDIT, "查看物流信息").setOnClick("showOrderRecord()").setIcon("mdi mdi-gesture-tap"));
        buts.add(new ButtonBlock(null).setStyle("btn btn-info m-r-5").setIcon("mdi mdi-delete-empty").setTitle("取消订单").setOnClick("orderRefund()"));
        TableTemplate pageTemplate = (TableTemplate) createPage(TableTemplate.class);
        pageTemplate.setBatchButton(buts);
        QueryWrapper<TOrder> queryWrapper=getFranQueryWrapper();
        queryWrapper.ne("is_test",1);
        queryWrapper.eq("player_id",getLoginUser().getCustomerId());
        queryWrapper.eq("order_batch_num","0");
        pageTemplate.setQueryWrapper(queryWrapper);
        session.setAttribute("pageTemplate", pageTemplate);
        return PageEntity.page("/order/all");
    }

    /**
     * 订单管理 -> 订单模板管理
     * @return
     */
    @GetMapping("/table/order/template")
    public PageEntity toOrderTemplate() {
        TableTemplate pageTemplate = (TableTemplate) createPage(TableTemplate.class);
        pageTemplate.getTableModel().setUrl("/admin/order/template/table/model/form");
        pageTemplate.setQueryWrapper(getFranQueryWrapper());
        session.setAttribute("pageTemplate", pageTemplate);
        return PageEntity.page("/order/template");
    }
    /**
     * 订单管理 ->京东订单
     *
     * @return
     */
    @GetMapping("/table/order/jd")
    public PageEntity toJd() {
        List<ButtonBlock> buts = new ArrayList<>(1);
        buts.add(ButtonBlock.createButton(ButtonBlockEnum.ADD, "查看物流信息").setOnClick("showOrderRecord()").setIcon("mdi mdi-gesture-tap"));
        TableTemplate pageTemplate = (TableTemplate) createPage(TableTemplate.class);
        pageTemplate.setBatchButton(buts);
        QueryWrapper<TOrder> queryWrapper=getFranQueryWrapper();
        queryWrapper.ne("is_test",1);
        queryWrapper.eq("order_batch_num","0");
        queryWrapper.like("order_num","D%");
        pageTemplate.setQueryWrapper(queryWrapper);
        session.setAttribute("pageTemplate", pageTemplate);
        return PageEntity.page("/order/jd");
    }


    /**
     * 客户钱包记录
     *
     * @return
     */
    @GetMapping("/table/wallet/record")
    public PageEntity twalletrecord() {
        TableTemplate pageTemplate = (TableTemplate) createPage(TableTemplate.class);
        QueryWrapper<TCustomerWallet> queryWrapper = getFranQueryWrapper();
        pageTemplate.setQueryWrapper(queryWrapper.eq("customer_id",getLoginUser().getCustomerId()));
        List<ButtonBlock> buts = new ArrayList<>(0);
        pageTemplate.setBatchButton(buts);
        session.setAttribute("pageTemplate", pageTemplate);
        return PageEntity.page("/open/merchantWallet/wallet");
    }

    /**
     * 客户钱包记录
     *
     * @return
     */
    @GetMapping("/table/wallet/detail")
    public PageEntity toWalletDetail() {
        TableTemplate pageTemplate = (TableTemplate) createPage(TableTemplate.class);
        QueryWrapper<TWalletDetail> queryWrapper = getFranQueryWrapper();
        pageTemplate.setQueryWrapper(queryWrapper.eq("customer_id",getLoginUser().getCustomerId()));
        List<ButtonBlock> buts = new ArrayList<>(0);
        pageTemplate.setBatchButton(buts);
        session.setAttribute("pageTemplate", pageTemplate);
        return PageEntity.page("/table/index");
    }

    /**
     * 客户钱包充值记录
     *
     * @return
     */
    @GetMapping("/table/customer/pay")
    public PageEntity toTCustomerPay() {
        TableTemplate pageTemplate = (TableTemplate) createPage(TableTemplate.class);
        QueryWrapper<TCustomerPay> queryWrapper = getFranQueryWrapper();
        pageTemplate.setQueryWrapper(queryWrapper.eq("customer_id",getLoginUser().getCustomerId()));
        List<ButtonBlock> buts = new ArrayList<>(0);
        pageTemplate.setBatchButton(buts);
        session.setAttribute("pageTemplate", pageTemplate);
        return PageEntity.page("/table/index");
    }

    /**
     * 客户钱包充值记录
     *
     * @return
     */
    @GetMapping("/table/wallet/wxtransfers")
    public PageEntity toTWxtransfers() {
        TableTemplate pageTemplate = (TableTemplate) createPage(TableTemplate.class);
        QueryWrapper<TWxtransfers> queryWrapper = getFranQueryWrapper();
        pageTemplate.setQueryWrapper(queryWrapper.eq("customer_id",getLoginUser().getCustomerId()));
        List<ButtonBlock> buts = new ArrayList<>(0);
        pageTemplate.setBatchButton(buts);
        session.setAttribute("pageTemplate", pageTemplate);
        return PageEntity.page("/table/index");
    }

    /**
     * 客户钱包充值赠金记录
     *
     * @return
     */
    @GetMapping("/table/wallet/gift/price")
    public PageEntity giftRecord() {
        TableTemplate pageTemplate = (TableTemplate) createPage(TableTemplate.class);
        QueryWrapper<TGiveDetail> queryWrapper = getFranQueryWrapper();
        pageTemplate.setQueryWrapper(queryWrapper.eq("customer_id",getLoginUser().getCustomerId()));
        List<ButtonBlock> buts = new ArrayList<>(0);
        pageTemplate.setBatchButton(buts);
        session.setAttribute("pageTemplate", pageTemplate);
        return PageEntity.page("/table/index");
    }

    /**
     * 开发配置
     *
     * @return
     */
    @GetMapping("/table/open/customer/set")
    public PageEntity openCostSet() {
        SysUser user = (SysUser) session.getAttribute("loginUser");
        TableTemplate pageTemplate = (TableTemplate) createPage(TableTemplate.class);
        pageTemplate.setQueryWrapper(new QueryWrapper<TOpen>().eq("custemer_id", user.getCustomerId()));
        List<ButtonBlock> buttonBlocks = new ArrayList<ButtonBlock>(1);
        //buttonBlocks.add(ButtonBlock.createButton(ButtonBlockEnum.EDIT,"修改配置"));
        pageTemplate.setBatchButton(buttonBlocks);
        session.setAttribute("pageTemplate", pageTemplate);
        return PageEntity.page("/table/index");
    }

}
