using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WindowsFormsApp1.Delivery;

namespace WindowsFormsApp1.납품관리
{
    public partial class Order_Send_Chk : Form
    {
        string[] set_date = { "", "" };
        string compidx;
        Helper_DB db = new Helper_DB();
        Order_input oi;
        public Order_Send_Chk(Order_input _oi)
        {
            InitializeComponent();
            oi = _oi;
            compidx = oi.compidx;
        }
        private void Order_Send_Chk_Load(object sender, EventArgs e)
        {
            db.DBcon();

            #region 일자설정
            int Year = DateTime.Now.Year;
            int Month = DateTime.Now.Month;
            int Day = DateTime.Now.Day - 1;
            Start_DatePicker.Value = new DateTime(Year, Month, Day);

            set_date[0] = Start_DatePicker.Value.ToString().Substring(0, 10);
            set_date[1] = End_DatePicker.Value.ToString().Substring(0, 10);
            #endregion

            #region Grid설정
            string Area = "`구분`, `팩스전송키`, `거래처명`, `날짜`, `시간`, `보낸이`, `받는이`, `전송파일명`, `전송결과`";
            string Fax_Key_tmp = db.Search_Date("Send_Order", Area, "날짜", set_date[0], set_date[1], compidx);
            string[] Fax_Key = Fax_Key_tmp.Split('|');
            input_Grid(Fax_Key);
            #endregion
        }
        private void input_Grid(string[] data)
        {
            switch(oi.cb_ordersend.SelectedIndex)
            {
                case 0:
                    dataGrid_Email.Enabled = false;
                    dataGrid_Email.Visible = false;
                    Fax_Grid(data);
                    break;

                case 1:
                    dataGrid_Fax.Enabled = false;
                    dataGrid_Fax.Visible = false;
                    Email_Grid(data);
                    break;

                default:
                    break;
            }
        }
        private void Fax_Grid(string[] data)
        {
            string[] grid = { "", "", "", "", "", 
                              "", "", "" };
            int len = 9;
            for(int a = 0; a < data.Length; a++)
            {
                if (a % len == 0) {
                    if (data[a] != "팩스") return;
                }
                if (a % len == 1) {
                    dataGrid_Fax.Rows.Add(Plus_Fax(data[a], grid));
                }
            }
        }
        private void Email_Grid(string[] data)
        {
            string[] grid = { "", "", "", "", "",
                              "" };
            int len = 9;
            for(int a= 0; a < data.Length; a++)
            {
                if (a % len == 0) {
                    if (data[a] != "메일") return;
                }
                if (a % len == 2) { grid[0] = data[a]; }
                if (a % len == 3) { grid[1] = data[a]; }
                if (a % len == 4) { grid[2] = data[a]; }
                if (a % len == 5) { grid[3] = data[a]; }
                if (a % len == 6) { grid[4] = data[a]; }
                if (a % len == 7) { grid[5] = data[a]; dataGrid_Email.Rows.Add(grid); }
            }
        }
        private string[] Plus_Fax(string faxkey, string[] grid)
        {
            Barobill_FAX fax = new Barobill_FAX();

            // 수신자회사명, 수신번호, 전송일시, 전송결과, 전송페이지수, 성공페이지수, 전송파일명
            string[] data = fax.Send_chk_BaroFax(faxkey);
            MessageBox.Show(faxkey);
            grid[0] = faxkey;
            grid[1] = data[0];
            grid[2] = data[1];
            grid[3] = data[2];
            grid[4] = data[3];
            grid[5] = data[4];
            grid[6] = data[5];
            grid[7] = data[6];

            return grid;
        }
    }
}
