package com.sooka.apply.controller;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sooka.apply.domain.IntDetailed;
import com.sooka.common.annotation.Log;
import com.sooka.common.core.controller.BaseController;
import com.sooka.common.core.domain.AjaxResult;
import com.sooka.common.core.page.TableDataInfo;
import com.sooka.common.enums.BusinessType;
import com.sooka.common.utils.poi.ExcelUtil;
import com.sooka.framework.util.ShiroUtils;
import com.sooka.system.domain.SysUser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.sooka.apply.domain.IntRecord;
import com.sooka.apply.service.IIntRecordService;

/**
 * 申请记录Controller
 *
 * @author yhliang
 * @date 2021-07-06
 */
@Controller
@RequestMapping("/apply/record")
public class IntRecordController extends BaseController
{
    private String prefix = "apply/record";

    @Autowired
    private IIntRecordService intRecordService;

    @RequiresPermissions("apply:record:view")
    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询申请记录列表
     */
    @RequiresPermissions("apply:record:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(IntRecord intRecord)
    {
        intRecord.setApplyUserId(ShiroUtils.getUserId());
        startPage();
        List<IntRecord> list = intRecordService.selectIntRecordList(intRecord);
        return getDataTable(list);
    }

    /**
     * 新增申请记录
     */
    @RequiresPermissions("apply:record:add")
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        mmap.put("applyDeptName", sysUser.getDept().getDeptName());
        mmap.put("applyUserName", sysUser.getUserName());
        return prefix + "/add";
    }

    /**
     * 新增保存申请记录
     */
    @Log(title = "申请记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(IntRecord intRecord)
    {
        intRecord.setIntDetailedList(structureSubData(intRecord.getSubData()));
        return toAjax(intRecordService.insertIntRecord(intRecord));
    }

    private List<IntDetailed> structureSubData(String subData){
        JSONArray jsonArray = JSONArray.parseArray(subData);
        List<IntDetailed> list = new ArrayList<>();
        for(int i=0;i< jsonArray.size();i++){
            JSONArray jsonArray1 = JSONArray.parseArray(jsonArray.get(i).toString());
            IntDetailed intDetailed = new IntDetailed();
            for(int j=0;j<jsonArray1.size();j++){
                JSONObject jsonObject = JSONObject.parseObject(jsonArray1.get(j).toString());
                String name = jsonObject.getString("name");
                if(name.equals("intId")){
                    intDetailed.setIntId(jsonObject.getString("value"));
                }else if(name.equals("purpose")){
                    intDetailed.setPurpose(jsonObject.getString("value"));
                }else if(name.equals("file")){
                    intDetailed.setFile(jsonObject.getString("value"));
                }
            }
            list.add(intDetailed);
        }
        return list;
    }

    /**
     * 附件单独用iframe
     */
    @GetMapping(value={"/uploadFrame","/uploadFrame/{subId}"})
    public String uploadFrame(@PathVariable(value = "subId", required = false) String subId, ModelMap mmap)
    {
        if(subId != null){
            IntDetailed intDetailed = intRecordService.selectIntRecordDetailedBySubId(subId);
            mmap.put("file",intDetailed.getFile());
        }
        return prefix + "/uploadFrame";
    }

    /**
     * 附件单独用iframe
     */
    @GetMapping(value={"/uploadFrameDisabled/{subId}"})
    public String uploadFrameDisabled(@PathVariable(value = "subId", required = false) String subId, ModelMap mmap)
    {
        if(subId != null){
            IntDetailed intDetailed = intRecordService.selectIntRecordDetailedBySubId(subId);
            mmap.put("file",intDetailed.getFile());
        }
        return prefix + "/uploadFrameDisabled";
    }

    /**
     * 修改申请记录
     */
    @RequiresPermissions("apply:record:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        IntRecord intRecord = intRecordService.selectIntRecordById(id);
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(intRecord.getIntDetailedList()));
        mmap.put("intRecord", intRecord);
        mmap.put("intDetailedList", jsonArray);
        return prefix + "/edit";
    }

    /**
     * 修改保存申请记录
     */
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(IntRecord intRecord)
    {
        intRecord.setIntDetailedList(structureSubData(intRecord.getSubData()));
        return toAjax(intRecordService.editIntRecord(intRecord));
    }

    /**
     * 保存驳回信息字段
     */
    @PostMapping("/reject")
    @ResponseBody
    public AjaxResult reject(IntRecord intRecord)
    {
        return toAjax(intRecordService.rejectIntRecord(intRecord));
    }

    /**
     * 导出申请记录列表
     */
    @RequiresPermissions("apply:record:export")
    @Log(title = "申请记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(IntRecord intRecord)
    {
        List<IntRecord> list = intRecordService.selectIntRecordList(intRecord);
        ExcelUtil<IntRecord> util = new ExcelUtil<IntRecord>(IntRecord.class);
        return util.exportExcel(list, "record");
    }

    /**
     * 删除申请记录
     */
    @RequiresPermissions("apply:record:remove")
    @Log(title = "申请记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(intRecordService.deleteIntRecordByIds(ids));
    }

    /**
     * 修改状态
     */
    @PostMapping("/updateStatus")
    @ResponseBody
    public AjaxResult updateStatus(String id,String status)
    {
        return toAjax(intRecordService.updateStatus(id,status));
    }

    /**
     * 查看详情
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap)
    {
        IntRecord intRecord = intRecordService.selectIntRecordById(id);
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(intRecord.getIntDetailedList()));
        mmap.put("intRecord", intRecord);
        mmap.put("intDetailedList", jsonArray);
        return prefix + "/detail";
    }

    /**
     * 驳回原因弹框
     */
    @GetMapping("/rejectDialog/{id}")
    public String rejectDialog(@PathVariable("id") String id, ModelMap mmap)
    {
        IntRecord intRecord = intRecordService.selectIntRecordById(id);
        mmap.put("intRecord", intRecord);
        return prefix + "/rejectDialog";
    }






/***********************以下为一期原有方法******************************/

    /**
     * 查询申请记录列表
     */
    @RequiresPermissions("apply:record:list")
    @PostMapping("/intDetailedList")
    @ResponseBody
    public TableDataInfo intDetailedList(IntDetailed intDetailed)
    {
        startPage();
        List<IntDetailed> list = intRecordService.selectIntDetailedList(intDetailed);
        return getDataTable(list);
    }

    /**
     * 状态修改
     */
    @Log(title = "接口管理", businessType = BusinessType.UPDATE)
    @RequiresPermissions("apply:record:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(IntDetailed intDetailed)
    {
        return toAjax(intRecordService.changeStatus(intDetailed));
    }


}
