package io.renren.modules.app.controller;

import io.renren.common.utils.MyUtils;
import io.renren.common.utils.R;
import io.renren.modules.admin.entity.AttendersEntity;
import io.renren.modules.admin.entity.AttendersviewEntity;
import io.renren.modules.admin.entity.MemberEntity;
import io.renren.modules.admin.service.AttendersService;
import io.renren.modules.admin.service.AttendersviewService;
import io.renren.modules.admin.service.MemberService;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.admin.entity.HotalEntity;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/app/attenders")
public class AppAttendersController {
    @Autowired
    private AttendersService attendersService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private AttendersviewService attendersviewService;

    @GetMapping("notToken")
    public R notToken(){
        return R.ok().put("msg", "无需token也能访问。。。");
    }

    @PostMapping("save")
    public R save(@RequestBody AttendersviewEntity attendersform){
        Long meetingId = attendersform.getMeetingId();
        if (meetingId == 0L) {
            return R.error("会议id不可为空");
        }

        String symbol = MyUtils.md5(attendersform.getTruename() + attendersform.getPhone());

        MemberEntity member = memberService.getBySymbol(symbol);

        // 存会员表(个人信息, 开票信息)
        if (member == null) {
            member = new MemberEntity();
        }
        member.setTruename(attendersform.getTruename());
        member.setOrganization(attendersform.getOrganization());
        member.setPosition(attendersform.getPosition());
        member.setPhone(attendersform.getPhone());
        member.setEmail(attendersform.getEmail());
        member.setSex(attendersform.getSex());
        member.setRemark(attendersform.getRemark());
        member.setTaxType(attendersform.getTaxType());
        member.setTaxTitle(attendersform.getTaxTitle());
        member.setTaxNumber(attendersform.getTaxNumber());
        member.setCompanyAddress(attendersform.getCompanyAddress());
        member.setCompanyPhone(attendersform.getCompanyPhone());
        member.setBankAccount(attendersform.getBankAccount());
        member.setMailingAddress(attendersform.getMailingAddress());
        member.setPostcode(attendersform.getPostcode());

        if (member.getId()==null || member.getId() == 0L) {
            memberService.saveMember(member);
        } else {
            memberService.updateMember(member);
        }

        Long memberId = member.getId();
        // 存参会信息
        AttendersEntity attenders = new AttendersEntity();
        attenders.setMemberId(memberId);
        attenders.setMeetingId(meetingId);
        attenders.setMemberType(attendersform.getMemberType());
        attenders.setFeeType(attendersform.getFeeType());
        attenders.setPaperType(attendersform.getPaperType());
        attenders.setIsReport(attendersform.getIsReport());
        attenders.setReportTitle(attendersform.getReportTitle());
        attenders.setHotelType(attendersform.getHotelType());
        attenders.setRoomType(attendersform.getRoomType());
        attenders.setIsShare(attendersform.getIsShare());
        attenders.setCheckInDate(attendersform.getCheckInDate());
        attenders.setCheckOutDate(attendersform.getCheckOutDate());
        attenders.setPayType(attendersform.getPayType());
        attenders.setIsPay(attendersform.getIsPay());
        attenders.setMoney(attendersform.getMoney());
        attenders.setCreateTime(new Date());
        attenders.setIsOnline(attendersform.getIsOnline());

        attendersService.save(attenders);

        return R.ok();
    }

    @GetMapping("/infobysymbol/{token}")
    public R notToken(@PathVariable("symbol") String symbol){
        // AttendersviewEntity attenders = attendersviewService.getBySymbol(token);
        // return R.ok().put("attenders", attenders);
        return null;
    }

    @Login
    @GetMapping("/hotal/{mid}")
    public R getHotal(@RequestAttribute("userId") Integer userId, @PathVariable("mid") Long mid){
        Long uid = userId.longValue();
        AttendersEntity attenders = attendersService.getByMMId(uid, mid);
        if (attenders == null) {
            return R.error("未找到您的报名信息, 请先报名参会.");
        }
        
        HotalEntity hotal = new HotalEntity();
        hotal.setAid(attenders.getId());
        hotal.setMeetingId(mid);
        hotal.setHotelType(attenders.getHotelType());
        hotal.setIsShare(attenders.getIsShare());
        hotal.setRoomType(attenders.getRoomType());
        hotal.setCheckInDate(attenders.getCheckInDate());
        hotal.setCheckOutDate(attenders.getCheckOutDate());
        
        return R.ok().put("hotalInfo", hotal);
    }

    @Login
    @PostMapping("/hotalsave")
    public R saveHotal(@RequestBody HotalEntity hotalForm){
        AttendersEntity attenders = attendersService.getById(hotalForm.getAid());
        attenders.setHotelType(hotalForm.getHotelType());
        attenders.setIsShare(hotalForm.getIsShare());
        attenders.setRoomType(hotalForm.getRoomType());
        attenders.setCheckInDate(hotalForm.getCheckInDate());
        attenders.setCheckOutDate(hotalForm.getCheckOutDate());

        attendersService.updateById(attenders);

        return R.ok();
    }


}
