package memberAction;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Choice;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.Box;
import javax.swing.JApplet;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableColumnModel;
import javax.swing.table.DefaultTableModel;

import vofiles.EmpVOmember;
import Member.MemberDeleteBean;
import Member.MemberSearchBean;

public class memberDeleteAction extends JApplet implements ActionListener {

    private static final long serialVersionUID = 1L;
    JTable table;
    DefaultTableModel model;
    String[] header = new String[] { "Name", "NickName", "Age", "Birth", "From", "Course",
            "Position", "Highschool", "Message" };

    String name = null;
    String nickname = null;
    String age = null;
    String birth = null;
    String from = null;
    String course = null;
    String position = null;
    String highschool = null;
    String message = null;

    EmpVOmember[][] data = new EmpVOmember[][] { {} };

    JPanel panel = new JPanel();
    JTextField number = new JTextField(30);
    JTextField birthDay = new JTextField(8);
    JTextField messAge = new JTextField(100);
    Button transitBtn = new Button("選択");
    Button searchBtn = new Button("検索なっしー");
    Button deleteBtn = new Button("削除★★");
    Label labelNumber = new Label("名前を入力してください。");
    Label labelBirthday = new Label("誕生日を入力してください。");
    Label labelMessage = new Label("メッセージを入力してください。");
    Choice cho;
    private String fileName = "Members.txt";


    public void init() {

        setLayout(null);

        // 背景色の設定
        Container c = getContentPane();
        c.setBackground(Color.WHITE);
        int w = getWidth();
        int h = getHeight();
        setLayout(new BorderLayout());

        // テーブルオブジェクトの生成
        model = new DefaultTableModel(data, header);
        table = new JTable(model);
        table.setGridColor(Color.BLUE);
        DefaultTableColumnModel columnModel = (DefaultTableColumnModel) table.getColumnModel();
        columnModel.getColumn(1).setPreferredWidth(20);
        // テーブルをレイアウトに追加
        JScrollPane sc = new JScrollPane(table);
        sc.setBackground(Color.WHITE);
        //初期画面の非表示部分を修正
        sc.setPreferredSize(new Dimension(w, h - 260));
        add(sc, BorderLayout.NORTH);
        panel.setPreferredSize(new Dimension(w, 30));
        panel.setLayout(new BorderLayout());
        number.setPreferredSize(new Dimension(10, 25));
        panel.add(number, BorderLayout.WEST);
        birthDay.setPreferredSize(new Dimension(20, 25));
        panel.add(birthDay, BorderLayout.CENTER);
        panel.add(messAge, BorderLayout.EAST);
        add(panel, BorderLayout.CENTER);
        add(labelNumber, BorderLayout.CENTER);
        add(number, BorderLayout.CENTER);
        add(labelBirthday, BorderLayout.CENTER);
        add(birthDay, BorderLayout.CENTER);
        add(labelMessage, BorderLayout.CENTER);
        add(messAge, BorderLayout.CENTER);



        number.setBounds(200, 200, 200, 20);;
        birthDay.setBounds(200, 250, 200, 20);
        messAge.setBounds(200, 300, 200, 20);

        labelNumber.setBounds(200, 175, 200, 20);
        labelBirthday.setBounds(200, 225, 100, 20);
        labelMessage.setBounds(200, 275, 100, 20);
        transitBtn.setPreferredSize(new Dimension(100, 30));
        transitBtn.addActionListener(this);
        searchBtn.setPreferredSize(new Dimension(100, 30));
        searchBtn.addActionListener(this);
        searchBtn.setBounds(200, 0, 20, 20);
        deleteBtn.setPreferredSize(new Dimension(50,1));
        deleteBtn.addActionListener(this);
        add(searchBtn, BorderLayout.LINE_START);
        add(deleteBtn, BorderLayout.LINE_END);
        cho = (Choice) add(new Choice());
        cho.add("--選択してください--");
        cho.add("1.全件表示");
        cho.add("2.１件検索");
        cho.add("3.新規登録");
        cho.add("4.削除更新");
        cho.add("5.書換更新");
        cho.add("6.メインメニューに戻る");
        add(cho, BorderLayout.SOUTH);
        cho.setBounds(200, 350, 200, 20);
        add(transitBtn, BorderLayout.PAGE_END);

        // テーブルの上下左右の余白設定
        add(Box.createHorizontalStrut(10), BorderLayout.EAST);
        add(Box.createHorizontalStrut(10), BorderLayout.WEST);
    }

    public void actionPerformed(ActionEvent event) {
        if (event.getSource() == searchBtn) {
            MemberSearchBean memberSearchBean = new MemberSearchBean();
            String checkNumber = number.getText();
            String checkBirthDay = birthDay.getText();
            String checkMessAge = messAge.getText();

            EmpVOmember data = new EmpVOmember();
            data = memberSearchBean.executeReturn(fileName, checkNumber, checkBirthDay, checkMessAge);

            //アプレット上ではインサートされているが、実際はテーブルに登録しない。
            if(data != null){
            model.removeRow(model.getRowCount() - 1);
            model.addRow(new String[] { data.getName(), data.getNickname(), data.getAge(), data.getBirth(),
                    data.getFrom(), data.getCourse(), data.getPosition(), data.getHighschool(), data.getMessage() });
            }else{
                model.removeRow(model.getRowCount() - 1);
                model.addRow(new String[] { null, null, null, null,
                        null, null, null, null, null });
            }
            System.out.println(cho + "," + cho.getSelectedItem() + cho.getSelectedIndex());
        }
        if(event.getSource() == deleteBtn){

            MemberSearchBean memberSearchBean = new MemberSearchBean();
            MemberDeleteBean memberDeleteBean = new MemberDeleteBean();
            String checkNumber = number.getText();
            String checkBirthDay = birthDay.getText();
            String checkMessAge = messAge.getText();

            EmpVOmember searchData = new EmpVOmember();
            searchData = memberSearchBean.executeReturn(fileName, checkNumber, checkBirthDay, checkMessAge);

            if(searchData != null){
                EmpVOmember data = new EmpVOmember();
                data = memberDeleteBean.executeReturn(fileName, checkNumber, checkBirthDay, checkMessAge);
                if(data != null){
                    model.removeRow(model.getRowCount() - 1);
                    model.addRow(new String[] { data.getName(), data.getNickname(), data.getAge(), data.getBirth(),
                            data.getFrom(), data.getCourse(), data.getPosition(), data.getHighschool(), data.getMessage() });
                    }else{
                        model.removeRow(model.getRowCount() - 1);
                        model.addRow(new String[] { null, null, null, null,
                                null, null, null, null, null });
                    }
                    System.out.println(cho + "," + cho.getSelectedItem() + cho.getSelectedIndex());

            }

            //アプレット上ではインサートされているが、実際はテーブルに登録しない。
            if(searchData != null){
            model.removeRow(model.getRowCount() - 1);
            model.addRow(new String[] { searchData.getName(), searchData.getNickname(), searchData.getAge(), searchData.getBirth(),
                    searchData.getFrom(), searchData.getCourse(), searchData.getPosition(), searchData.getHighschool(), searchData.getMessage() });
            }else{
                model.removeRow(model.getRowCount() - 1);
                model.addRow(new String[] { null, null, null, null,
                        null, null, null, null, null });
            }
            System.out.println(cho + "," + cho.getSelectedItem() + cho.getSelectedIndex());
        }
        if (event.getSource() == transitBtn) {
            if (cho.getSelectedIndex() == 1) {
                System.out.println("全件表示");
                System.out.println("Address was transfer");
            } else if (cho.getSelectedIndex() == 2) {
                System.out.println("１件表示");
                System.out.println("Address was transfer");
            } else if (cho.getSelectedIndex() == 3) {
                System.out.println("新規登録");
                System.out.println("Address was transfer");
            } else if (cho.getSelectedIndex() == 4) {
                System.out.println("削除");
                System.out.println("Address was transfer");
            } else if (cho.getSelectedIndex() == 5) {
                System.out.println("更新");
                System.out.println("Address was transfer");
            } else if (cho.getSelectedIndex() == 6) {
                System.out.println("戻る");
                System.out.println("Address was transfer");
            }
        }
    }

}
