This commit is contained in:
Naz 2025-03-24 16:39:58 +08:00
parent e131348fb8
commit 802a81357e
6 changed files with 264 additions and 2 deletions

View File

@ -14,5 +14,6 @@ namespace PSTW_CentralSystem.Areas.OTcalculate.Models
[ForeignKey("StateId")] [ForeignKey("StateId")]
public virtual StateModel? States { get; set; } public virtual StateModel? States { get; set; }
} }
} }

View File

@ -8,6 +8,14 @@ namespace PSTW_CentralSystem.Areas.OTcalculate.Models
{ {
[Key] [Key]
public int StateId { get; set; } public int StateId { get; set; }
[Required]
public required string StateName { get; set; } public required string StateName { get; set; }
public int? WeekendId { get; set; }
[ForeignKey("WeekendId")]
public virtual WeekendModel? Weekends { get; set; }
} }
} }

View File

@ -0,0 +1,13 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using PSTW_CentralSystem.Models;
namespace PSTW_CentralSystem.Areas.OTcalculate.Models
{
public class WeekendModel
{
[Key]
public int WeekendId { get; set; }
public required string Day { get; set; }
}
}

View File

@ -111,7 +111,47 @@
@* Update Weekend *@ @* Update Weekend *@
<div v-if="activeTab === 'weekend'" class="card shadow-sm"> <div v-if="activeTab === 'weekend'" class="card shadow-sm">
<div class="card-body"> <div class="card-body">
<p>Weekend Update Section</p> <label>State:</label>
<div class="row">
<div class="col-md-4" v-for="state in stateList" :key="state.stateId">
<div class="form-check">
<input type="checkbox" class="form-check-input" :value="state.stateId" v-model="selectedStates">
<label class="form-check-label">{{ state.stateName }}</label>
</div>
</div>
</div>
</div>
<div class="card-body">
<label for="dayDropdown">Days:</label>
<select id="dayDropdown" class="form-control" v-model="selectedDay">
<option value="" disabled selected>Select Day</option>
<option v-for="weekend in dayList" :key="weekend.weekendId" :value="weekend.day">
{{ weekend.day }}
</option>
</select>
</div>
<div class="card-body text-center">
<button class="btn btn-danger" v-on:click="clearWeekend">Clear</button>
<button class="btn btn-success ms-3" v-on:click="updateWeekend">Save Weekend</button>
</div>
</div>
<div v-if="activeTab === 'weekend'" class="card shadow-sm">
<div class="card-body">
<h5>Weekend Days per State</h5>
<div class="row">
<div class="col-md-6" v-for="(states, day) in groupedWeekends" :key="day">
<div class="card p-2 mb-2 shadow-sm">
<strong>{{ day }}</strong>
<ul class="list-unstyled ps-3">
<li v-for="state in states" :key="state">{{ state }}</li>
</ul>
</div>
</div>
</div>
</div> </div>
</div> </div>
@ -132,13 +172,19 @@
selectedStates: [], selectedStates: [],
holidayName: '', holidayName: '',
selectedState: null, selectedState: null,
holidayList: [] holidayList: [],
selectedDay: "",
weekendList: [],
stateWeekends: []
}; };
}, },
mounted() { mounted() {
this.fetchStates(); this.fetchStates();
this.fetchHolidays(); this.fetchHolidays();
this.fetchWeekends();
this.fetchStateWeekends();
}, },
computed: { computed: {
@ -146,6 +192,16 @@
return this.selectedState return this.selectedState
? this.holidayList.filter(h => h.stateId === this.selectedState) ? this.holidayList.filter(h => h.stateId === this.selectedState)
: []; : [];
},
groupedWeekends() {
const grouped = {};
this.stateWeekends.forEach(weekend => {
if (!grouped[weekend.day]) {
grouped[weekend.day] = [];
}
grouped[weekend.day].push(weekend.stateName);
});
return grouped;
} }
}, },
@ -222,11 +278,84 @@
} }
}, },
async fetchWeekends() {
try {
const response = await fetch("/OvertimeAPI/GetWeekendDay");
if (!response.ok) throw new Error("Failed to fetch weekends");
this.dayList = await response.json();
} catch (error) {
console.error("Error fetching weekends:", error);
alert("Failed to load weekend list.");
}
},
async fetchStateWeekends() {
try {
const response = await fetch("/OvertimeAPI/GetStateWeekends");
if (!response.ok) throw new Error("Failed to fetch state weekends");
this.stateWeekends = await response.json();
console.log("Fetched state weekends:", this.stateWeekends);
} catch (error) {
console.error("Error fetching state weekends:", error);
alert("Failed to load state weekend data.");
}
},
async updateWeekend() {
try {
if (this.selectedStates.length === 0 || !this.selectedDay) {
alert("Please select at least one state and a day.");
return;
}
const selectedDayObject = this.dayList.find(day => day.day === this.selectedDay);
if (!selectedDayObject) {
alert("Selected day not found in the list.");
return;
}
const payload = this.selectedStates.map(stateId => {
const state = this.stateList.find(s => s.stateId === stateId);
return {
StateId: stateId,
StateName: state.stateName,
WeekendId: selectedDayObject.weekendId
};
});
console.log("Payload being sent to API:", payload);
const response = await fetch("/OvertimeAPI/UpdateWeekend", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload)
});
if (response.ok) {
alert("Weekend updated successfully!");
this.clearWeekend();
await this.fetchStateWeekends();
} else {
alert("Failed to update weekend.");
}
} catch (error) {
console.error("Error updating weekend:", error);
alert("An error occurred while updating weekend.");
}
},
clearForm() { clearForm() {
this.selectedDate = ''; this.selectedDate = '';
this.selectedStates = []; this.selectedStates = [];
this.holidayName = ''; this.holidayName = '';
this.selectedDay = '';
},
clearWeekend() {
this.selectedDay = "";
this.selectedStates = [];
} }
} }
}); });

View File

@ -197,5 +197,115 @@ namespace PSTW_CentralSystem.Controllers.API
} }
#endregion #endregion
#region Weekend
[HttpGet("GetWeekendDay")]
public async Task<IActionResult> GetWeekendDay()
{
try
{
var weekends = await _centralDbContext.Weekends
.Select(w => new
{
w.WeekendId,
w.Day
})
.ToListAsync();
return Json(weekends);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpPost("UpdateWeekend")]
public async Task<IActionResult> UpdateWeekend([FromBody] List<StateModel> states)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
try
{
foreach (var state in states)
{
var existingState = await _centralDbContext.States
.FirstOrDefaultAsync(s => s.StateId == state.StateId);
if (existingState != null)
{
// Corrected: Updating WeekendId
existingState.WeekendId = state.WeekendId;
_centralDbContext.States.Update(existingState);
}
else
{
// Ensure new states are added correctly
_centralDbContext.States.Add(new StateModel
{
StateId = state.StateId,
StateName = state.StateName,
WeekendId = state.WeekendId
});
}
}
await _centralDbContext.SaveChangesAsync();
var updatedWeekend = await _centralDbContext.States
.Include(w => w.Weekends)
.Select(w => new
{
w.StateId,
w.StateName,
w.WeekendId,
Day = w.Weekends.Day
})
.ToListAsync();
return Json(updatedWeekend);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpGet("GetAllWeekends")]
public IActionResult GetAllWeekends()
{
var weekends = _centralDbContext.Weekends.ToList();
return Ok(weekends);
}
[HttpGet("GetStateWeekends")]
public async Task<IActionResult> GetStateWeekends()
{
try
{
var stateWeekends = await _centralDbContext.States
.Include(s => s.Weekends)
.Where(s => s.WeekendId != null)
.Select(s => new
{
s.StateId,
s.StateName,
Day = s.Weekends.Day
})
.ToListAsync();
return Json(stateWeekends);
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
#endregion
} }
} }

View File

@ -102,5 +102,6 @@ namespace PSTW_CentralSystem.DBContext
public DbSet<RateModel> Rates { get; set; } public DbSet<RateModel> Rates { get; set; }
public DbSet<CalendarModel> Holidays { get; set; } public DbSet<CalendarModel> Holidays { get; set; }
public DbSet<StateModel> States { get; set; } public DbSet<StateModel> States { get; set; }
public DbSet<WeekendModel> Weekends { get; set; }
} }
} }