package nc.ui.hr.baod_orders.view;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.util.List;
import java.util.Map;

import javax.swing.JLabel;
import javax.swing.JPanel;

import com.alibaba.fastjson.JSONObject;
import com.borland.jbcl.layout.VerticalFlowLayout;

import nc.hr.utils.SpecialDataUtil;
import nc.ui.gl.accbook.DateChooser;
import nc.ui.pub.beans.MessageDialog;
import nc.ui.pub.beans.UIDialog;
import nc.ui.pub.beans.UITextField;
import nc.ui.pubapp.uif2app.event.card.CardHeadTailAfterEditEvent;
import nc.ui.querytemplate.dlg.ButtonPanel;
import nc.vo.pub.lang.UFDouble;

public class ShowMoneyDialog extends UIDialog {
    private static final long serialVersionUID = -8255260629066398635L;
    private ButtonPanel buttonPanel;
    private UITextField textField;
    private DateChooser startDateChooser;
    private CardHeadTailAfterEditEvent edit;

    @SuppressWarnings("deprecation")
    public ShowMoneyDialog(CardHeadTailAfterEditEvent edit, List<Map<String, String>> listmap) {
        this.edit = edit;
        setLayout(new VerticalFlowLayout());
        setSize(new Dimension(500, 350));
        getContentPane().add(getOutputFileChooserPanel(listmap));
        getContentPane().add(getStartAsEndDate());
        getContentPane().add(getMoneyText());
        getContentPane().add(getButtonPanel(), "South");
    }
    
    private ButtonPanel getButtonPanel() {
        if (this.buttonPanel == null) {
            this.buttonPanel = new ButtonPanel(new FlowLayout(1, 2, 10));
            this.buttonPanel.remove(1);
            this.buttonPanel.addOKActionListener(new OKAction());
        }
        return this.buttonPanel;
    }

    private Component getOutputFileChooserPanel(List<Map<String, String>> listmap) {
        JPanel jp = new JPanel();
        // 字段说明标签
        StringBuffer description = new StringBuffer();
        for (int i = 0; i < listmap.size(); i++) {
            Map<String, String> map = listmap.get(i);
            //开始日期
            String begindate = map.get("begindate");
            //结束日期
            String glbdef7 = map.get("glbdef7");
            //金额
            UFDouble money = new UFDouble(String.valueOf(map.get("glbdef2")));
            description.append("合同周期:" + begindate + "," + glbdef7 + ",保底金额:" + money.multiply(10000).setScale(2, UFDouble.ROUND_HALF_UP) + "元<br>");
        }
        JLabel descriptionLabel = new JLabel("<html>" + description.toString() + "</html>");
        descriptionLabel.setPreferredSize(new Dimension(400, 200));
        descriptionLabel.setHorizontalAlignment(JLabel.CENTER); // 设置文本对齐方式
        jp.add(descriptionLabel);
        return jp;
    }

    private Component getStartAsEndDate() {
        JPanel jp = new JPanel();
        startDateChooser = new DateChooser(); // 创建时间选择器组件
        startDateChooser.setPreferredSize(new Dimension(350, 40));
        startDateChooser.getStartDate();
        jp.add(startDateChooser);
        
       
        return jp;
    }
    private Component getMoneyText() {
        JPanel jp = new JPanel();
        jp.add(new JLabel("本次保底收入金额:"));
        if (textField == null) {
            textField = new UITextField();
            textField.setPreferredSize(new Dimension(200, 20));
        }
        jp.add(textField);
        return jp;
    }

    private class OKAction implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent actionEvent) {
        	String startDate = startDateChooser.getStartDate();
            String endDate = startDateChooser.getEndDate();
            String money = textField.getText();
            if (startDate == null || endDate == null || money == null || "".equals(money)) {
                MessageDialog.showHintDlg(null, "提示", "请填写日期范围和本次保底收入金额！");
                return;
            }
            try {
                Double.parseDouble(money);
            } catch (Exception e) {
                MessageDialog.showHintDlg(null, "提示", "金额输入有误！");
                return;
            }
            //保底合同开始周期
            edit.getBillCardPanel().setHeadItem("bdcontract_period", startDate);
	   	 	//保底合同截止周期
            edit.getBillCardPanel().setHeadItem("def2", endDate);
            //保底金额
            edit.getBillCardPanel().setHeadItem("bdamount", money);
            //保底金额不可修改，用于后续保底金额判断
            edit.getBillCardPanel().setHeadItem("def11", money);
            closeOK();
            
            
            
        }

    }

}
