<?php
/**
 * Add/edit employee form template.
 *
 * @package MerchantKits\AttendanceManager
 *
 * @var string $action   'add' or 'edit'.
 * @var array  $employee Employee row (empty for add).
 * @var array  $shifts   Active shifts.
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

$employee = isset( $employee ) && is_array( $employee ) ? $employee : array();
$shifts   = isset( $shifts ) ? $shifts : array();
$is_edit  = ! empty( $employee['id'] );

$value = static function ( $key, $default = '' ) use ( $employee ) {
	return isset( $employee[ $key ] ) && null !== $employee[ $key ] ? $employee[ $key ] : $default;
};

$employment_types = array(
	'full_time' => __( 'Full-time', 'merchantkits-attendance-manager' ),
	'part_time' => __( 'Part-time', 'merchantkits-attendance-manager' ),
	'contract'  => __( 'Contract', 'merchantkits-attendance-manager' ),
	'intern'    => __( 'Intern', 'merchantkits-attendance-manager' ),
);

$statuses = array(
	'active'     => __( 'Active', 'merchantkits-attendance-manager' ),
	'inactive'   => __( 'Inactive', 'merchantkits-attendance-manager' ),
	'terminated' => __( 'Terminated', 'merchantkits-attendance-manager' ),
	'on_leave'   => __( 'On Leave', 'merchantkits-attendance-manager' ),
);

$next_id = $is_edit ? $value( 'employee_id' ) : MKAM_Employee::next_employee_id();
?>
<div class="wrap">
	<h1><?php echo $is_edit ? esc_html__( 'Edit Employee', 'merchantkits-attendance-manager' ) : esc_html__( 'Add Employee', 'merchantkits-attendance-manager' ); ?></h1>

	<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>">
		<input type="hidden" name="action" value="mkam_save_employee" />
		<input type="hidden" name="employee_id_pk" value="<?php echo esc_attr( $value( 'id', 0 ) ); ?>" />
		<?php wp_nonce_field( 'mkam_save_employee' ); ?>

		<table class="form-table" role="presentation">
			<tr>
				<th scope="row"><label for="first_name"><?php esc_html_e( 'First Name', 'merchantkits-attendance-manager' ); ?> *</label></th>
				<td><input name="first_name" id="first_name" type="text" class="regular-text" required value="<?php echo esc_attr( $value( 'first_name' ) ); ?>" /></td>
			</tr>
			<tr>
				<th scope="row"><label for="last_name"><?php esc_html_e( 'Last Name', 'merchantkits-attendance-manager' ); ?> *</label></th>
				<td><input name="last_name" id="last_name" type="text" class="regular-text" required value="<?php echo esc_attr( $value( 'last_name' ) ); ?>" /></td>
			</tr>
			<tr>
				<th scope="row"><label for="email"><?php esc_html_e( 'Email', 'merchantkits-attendance-manager' ); ?> *</label></th>
				<td><input name="email" id="email" type="email" class="regular-text" required value="<?php echo esc_attr( $value( 'email' ) ); ?>" /></td>
			</tr>
			<tr>
				<th scope="row"><label for="phone"><?php esc_html_e( 'Phone', 'merchantkits-attendance-manager' ); ?></label></th>
				<td><input name="phone" id="phone" type="text" class="regular-text" value="<?php echo esc_attr( $value( 'phone' ) ); ?>" /></td>
			</tr>
			<tr>
				<th scope="row"><label for="employee_id"><?php esc_html_e( 'Employee ID', 'merchantkits-attendance-manager' ); ?> *</label></th>
				<td><input name="employee_id" id="employee_id" type="text" class="regular-text" required value="<?php echo esc_attr( $next_id ); ?>" /></td>
			</tr>
			<tr>
				<th scope="row"><label for="department"><?php esc_html_e( 'Department', 'merchantkits-attendance-manager' ); ?></label></th>
				<td><input name="department" id="department" type="text" class="regular-text" value="<?php echo esc_attr( $value( 'department' ) ); ?>" /></td>
			</tr>
			<tr>
				<th scope="row"><label for="designation"><?php esc_html_e( 'Designation', 'merchantkits-attendance-manager' ); ?></label></th>
				<td><input name="designation" id="designation" type="text" class="regular-text" value="<?php echo esc_attr( $value( 'designation' ) ); ?>" /></td>
			</tr>
			<tr>
				<th scope="row"><label for="shift_id"><?php esc_html_e( 'Shift', 'merchantkits-attendance-manager' ); ?></label></th>
				<td>
					<select name="shift_id" id="shift_id">
						<option value="0"><?php esc_html_e( '— None —', 'merchantkits-attendance-manager' ); ?></option>
						<?php foreach ( $shifts as $shift ) : ?>
							<option value="<?php echo esc_attr( $shift['id'] ); ?>" <?php selected( $value( 'shift_id' ), $shift['id'] ); ?>><?php echo esc_html( $shift['name'] ); ?></option>
						<?php endforeach; ?>
					</select>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="employment_type"><?php esc_html_e( 'Employment Type', 'merchantkits-attendance-manager' ); ?></label></th>
				<td>
					<select name="employment_type" id="employment_type">
						<?php foreach ( $employment_types as $key => $label ) : ?>
							<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $value( 'employment_type', 'full_time' ), $key ); ?>><?php echo esc_html( $label ); ?></option>
						<?php endforeach; ?>
					</select>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="hourly_rate"><?php esc_html_e( 'Hourly Rate', 'merchantkits-attendance-manager' ); ?></label></th>
				<td><input name="hourly_rate" id="hourly_rate" type="number" step="0.01" min="0" value="<?php echo esc_attr( $value( 'hourly_rate' ) ); ?>" /></td>
			</tr>
			<tr>
				<th scope="row"><label for="monthly_salary"><?php esc_html_e( 'Monthly Salary', 'merchantkits-attendance-manager' ); ?></label></th>
				<td><input name="monthly_salary" id="monthly_salary" type="number" step="0.01" min="0" value="<?php echo esc_attr( $value( 'monthly_salary' ) ); ?>" /></td>
			</tr>
			<tr>
				<th scope="row"><label for="join_date"><?php esc_html_e( 'Join Date', 'merchantkits-attendance-manager' ); ?></label></th>
				<td><input name="join_date" id="join_date" type="date" value="<?php echo esc_attr( $value( 'join_date' ) ); ?>" /></td>
			</tr>
			<tr>
				<th scope="row"><label for="wp_user_id"><?php esc_html_e( 'Link to WP User', 'merchantkits-attendance-manager' ); ?></label></th>
				<td>
					<?php
					wp_dropdown_users(
						array(
							'name'             => 'wp_user_id',
							'id'               => 'wp_user_id',
							'selected'         => (int) $value( 'wp_user_id', 0 ),
							'show_option_none' => __( '— None —', 'merchantkits-attendance-manager' ),
							'option_none_value' => 0,
						)
					);
					?>
				</td>
			</tr>
			<tr>
				<th scope="row"><label for="status"><?php esc_html_e( 'Status', 'merchantkits-attendance-manager' ); ?></label></th>
				<td>
					<select name="status" id="status">
						<?php foreach ( $statuses as $key => $label ) : ?>
							<option value="<?php echo esc_attr( $key ); ?>" <?php selected( $value( 'status', 'active' ), $key ); ?>><?php echo esc_html( $label ); ?></option>
						<?php endforeach; ?>
					</select>
				</td>
			</tr>
		</table>

		<?php submit_button( $is_edit ? __( 'Update Employee', 'merchantkits-attendance-manager' ) : __( 'Add Employee', 'merchantkits-attendance-manager' ) ); ?>
		<a class="button" href="<?php echo esc_url( admin_url( 'admin.php?page=mkam-employees' ) ); ?>"><?php esc_html_e( 'Cancel', 'merchantkits-attendance-manager' ); ?></a>
	</form>
</div>
