/*
 * @Author: Kane
 * @Date: 2023-02-06 14:12:11
 * @LastEditors: Kane
 * @LastEditTime: 2023-02-24 09:31:03
 * @FilePath: /it-console/src/utils/api/LocalStorage.js
 * @Description: 初始化localStorage中保存的值。
 * 
 * Copyright (c) ${2022} by Kane, All Rights Reserved. 
 */

//常量
const REQUIREMRNT_UI = `requirement_ui`;

/**
 * 从localStorage中读取 REQUIREMRNT_UI 的值，并转换成对象。
 * 如果转换不成功或者缺少属性，就补充属性，设置为空值。
 * 时间属性设置为初始日期。
 * @returns 读取并装配好的ui对象
 */
function loadRequirementUI()
{
    let requirementUI = null;

    try
    {
        requirementUI = JSON.parse(window.localStorage.getItem(REQUIREMRNT_UI));
    }
    catch (error)
    {
        console.log(`转换requirement-ui失败：${error}。`);
        requirementUI = {};
    }

    //如果之前不存在，json转换结果也会是null
    requirementUI = requirementUI || {};
    requirementUI.selected_status = requirementUI.selected_status || [];
    requirementUI.title = requirementUI.title || "";
    requirementUI.serial_no = requirementUI.serial_no || "";
    requirementUI.request_people = requirementUI.request_people || "";
    requirementUI.commit_end_date = requirementUI.commit_end_date || new Date();

    if (requirementUI.commit_start_date === undefined)
    {
        requirementUI.commit_start_date = new Date();
        requirementUI.commit_start_date.setMonth(0);
        requirementUI.commit_start_date.setDate(1);
    }

    return requirementUI;
}

export { loadRequirementUI };