package com.gree.mall.miniapp.controller.workorder;

import com.gree.mall.miniapp.bean.order.WorkerOrderDetail;
import com.gree.mall.miniapp.bean.workorder.PgOrderProductDetailCount;
import com.gree.mall.miniapp.bean.workorder.PgOrderProductDetailVO;
import com.gree.mall.miniapp.helper.ResponseHelper;
import com.gree.mall.miniapp.logic.order.PayOrderLogic;
import com.gree.mall.miniapp.logic.workorder.ChangeOrderLogic;
import com.gree.mall.miniapp.plus.entity.PgOrderProductDetail;
import com.gree.mall.miniapp.plus.entity.SysDictCompany;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotBlank;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;

@Slf4j
@RestController
@Api(value = "工单变更", tags = {"工单变更"})
@RequestMapping(value = "/changeOrder", produces = "application/json; charset=utf-8")
public class ChangeOrderController {

    @Autowired
    private ChangeOrderLogic changeOrderLogic;

    @Autowired
    PayOrderLogic payOrderLogic;


    @PostMapping("/changeAppointmentTime")
    @ApiOperation("修改预约时间")
    public ResponseHelper changeAppointmentTime(
            @ApiParam(value = "工单号" , required = true) @RequestParam(required = true) String id,
            @ApiParam(value = "预约开始时间" , required = true) @RequestParam(required = true) String appointmentTime,
            @ApiParam(value = "预约结束时间" , required = true) @RequestParam(required = true) String appointmentEndTime,
            @ApiParam(value = "预约结束时间" , required = false) @RequestParam(required = false) String remark,
            HttpServletRequest request
    ) throws ParseException {
        changeOrderLogic.changeAppointmentTime(id,appointmentTime,appointmentEndTime,remark);
        return ResponseHelper.success();
    }



    @PostMapping("/getOrderProduct")
    @ApiOperation("获得设备")
    public ResponseHelper<List<PgOrderProductDetail>> getOrderProduct(
            @ApiParam(value = "工单号" , required = true) @RequestParam(required = true) String id,
            @ApiParam(value = "WAIT 待采集 WAIT_SAVE 待完善  OK 已采集" , required = true) @RequestParam(required = true) List<String> status,
            HttpServletRequest request
    ) throws ParseException {
        List<PgOrderProductDetail> pgOrderProducts = changeOrderLogic.getOrderProduct(id,status);
        return ResponseHelper.success(pgOrderProducts);
    }

    @PostMapping("/getOrderProductCount")
    @ApiOperation("获得设备数")
    public ResponseHelper<PgOrderProductDetailCount> getOrderProductCount(
            @ApiParam(value = "工单号" , required = true) @RequestParam(required = true) String id,
            HttpServletRequest request
    ) throws ParseException {
        PgOrderProductDetailCount pgOrderProducts = changeOrderLogic.getOrderProductCount(id);
        return ResponseHelper.success(pgOrderProducts);
    }


    @PostMapping("/productDetail")
    @ApiOperation("采集详情")
    public ResponseHelper<PgOrderProductDetailVO> productDetail(
            @ApiParam(value = "采集详细id" , required = true) @RequestParam(required = true) String id,
            HttpServletRequest request
    ) throws ParseException {
        PgOrderProductDetailVO pgOrderProductDetail = changeOrderLogic.productDetail(id);
        return ResponseHelper.success(pgOrderProductDetail);
    }

    @PostMapping("/sysDict")
    @ApiOperation(value = "获得字典值")
    public ResponseHelper<List<SysDictCompany>> sysDict(
            @ApiParam(required = true, value = "BUG_IMG 故障图片 GATHER_IMG采集图片") @RequestParam(required = true) String dictType

    ) throws Exception {
        List<SysDictCompany> sysDictCompanyList = payOrderLogic.sysDict(dictType);
        return ResponseHelper.success(sysDictCompanyList);
    }





    @PostMapping("/submit")
    @ApiOperation("提交采集")
    public ResponseHelper submit(
            @RequestBody PgOrderProductDetailVO pgOrderProductDetail,
            HttpServletRequest request
    ) throws ParseException {
        changeOrderLogic.submit(pgOrderProductDetail);
        return ResponseHelper.success();
    }

    @PostMapping("/submitProductDetail")
    @ApiOperation("提交全部采集")
    public ResponseHelper submitProductDetail(
            @ApiParam(required = true, value = "工单id") @RequestParam(required = true) String orderBaseId,
            @ApiParam(required = true, value = "提交地址") @RequestParam(required = true) String address,


            HttpServletRequest request
    ) throws ParseException {
        changeOrderLogic.submitProductDetail(orderBaseId,address);
        return ResponseHelper.success();
    }

    @PostMapping("/submitAll")
    @ApiOperation("完工反馈提交")
    public ResponseHelper submitAll(
            @ApiParam(required = true, value = "工单id") @RequestParam(required = true) String orderBaseId,
            @ApiParam(required = true, value = "完工备注") @RequestParam(required = true) String overRemark,
            @ApiParam(required = true, value = "客户签名") @RequestParam(required = true) String userSign,
            @ApiParam(required = true, value = "提交地址") @RequestParam(required = true) String address,
            HttpServletRequest request
    ) throws ParseException {
        changeOrderLogic.submitAll(orderBaseId,overRemark,userSign,address);
        return ResponseHelper.success();
    }


    @PostMapping("/submitPic")
    @ApiOperation("传图片地址获得定位水印")
    public ResponseHelper<String> submitPic(
            @ApiParam(required = true, value = "图片地址") @RequestParam(required = true) String patch,
            @ApiParam(required = true, value = "产品id") @RequestParam(required = true) String productDetailId,
            @NotBlank(message = "定位地址不能为空") @ApiParam(required = true, value = "定位地址") @RequestParam(required = false) String positionName,

            HttpServletRequest request
    ) throws Exception {
        String pic = changeOrderLogic.imageGenerateWatermark(productDetailId,patch,positionName);
        return ResponseHelper.success(pic);
    }





    @PostMapping("/getOrderList")
    @ApiOperation(value = "获得电子支付")
    public ResponseHelper<List<WorkerOrderDetail>> getOrder(
            @ApiParam(required = false, value = "工单号") @RequestParam(required = false) String workerOrderId

    ) throws Exception {
        List<WorkerOrderDetail> workerOrderDetail = payOrderLogic.getOrderList(workerOrderId);
        return ResponseHelper.success(workerOrderDetail);
    }

    @PostMapping("/productDetailList")
    @ApiOperation(value = "完工明细")
    public ResponseHelper<List<PgOrderProductDetailVO>> productDetailList(
            @ApiParam(required = false, value = "工单号") @RequestParam(required = false) String workerOrderId

    ) throws Exception {
        List<PgOrderProductDetailVO> workerOrderDetail = changeOrderLogic.productDetailList(workerOrderId);
        return ResponseHelper.success(workerOrderDetail);
    }

}
