# frozen_string_literal: true

class PieceCompanyRolesController < ApplicationController
  before_action :authenticate_user!, only: %i[create destroy edit new update]
  before_action :set_piece_company_role, only: %i[show edit update destroy]

  # GET /piece_company_roles or /piece_company_roles.json
  def index
    @piece_company_roles = PieceCompanyRole.all
  end

  # GET /piece_company_roles/1 or /piece_company_roles/1.json
  def show; end

  # GET /piece_company_roles/new
  def new
    @piece_company_role = authorize PieceCompanyRole.new
  end

  # GET /piece_company_roles/1/edit
  def edit
    authorize @piece_company_role
  end

  # POST /piece_company_roles or /piece_company_roles.json
  def create
    authorize PieceCompanyRole

    @piece_company_role = PieceCompanyRole.new(piece_company_role_params)

    respond_to do |format|
      if @piece_company_role.save
        format.html { redirect_to @piece_company_role, notice: 'Piece company role was successfully created.' }
        format.json { render :show, status: :created, location: @piece_company_role }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @piece_company_role.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /piece_company_roles/1 or /piece_company_roles/1.json
  def update
    authorize PieceCompanyRole

    respond_to do |format|
      if @piece_company_role.update(piece_company_role_params)
        format.html { redirect_to @piece_company_role, notice: 'Piece company role was successfully updated.' }
        format.json { render :show, status: :ok, location: @piece_company_role }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @piece_company_role.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /piece_company_roles/1 or /piece_company_roles/1.json
  def destroy
    authorize PieceCompanyRole

    @piece_company_role.destroy
    respond_to do |format|
      format.html { redirect_to piece_company_roles_url, notice: 'Piece company role was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_piece_company_role
    @piece_company_role = PieceCompanyRole.find(params[:id])
  end

  # Only allow a list of trusted parameters through.
  def piece_company_role_params
    params.require(:piece_company_role).permit(:company_id, :piece_id, :role_id, :begin_date_year,
                                               :begin_date_month, :begin_date_day, :end_date_year, :end_date_month, :end_date_day)
  end
end
