#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# Support module generated by PAGE version 4.13
# In conjunction with Tcl version 8.6
#    Sep 26, 2018 01:26:12 PM


import sys

try:
    from Tkinter import *
except ImportError:
    from tkinter import *

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True

import re
import os
import sys
#import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from tkinter import simpledialog
import shutil
from pathlib import Path
import os.path
#import sheetfilefixermodule
#import pplt
#import getrect
import pyodbc
from operator import itemgetter
connection_string ="Driver={ODBC Driver 11 for SQL Server};SERVER=LANTEKDATA\\LANTEK;DATABASE=EUROMAC;UID=sa;"

def set_Tk_var():
    global work_order
    work_order = StringVar()
    global purchase_order
    purchase_order = StringVar()
    global required_date
    required_date = StringVar()
    global lantek_date
    lantek_date = StringVar()
    global customer
    customer = StringVar()
    global job_name
    job_name = StringVar()
    global job_desc
    job_desc = StringVar()
    global cust_tag
    cust_tag = StringVar()
    global designer
    designer = StringVar()
    designer.set("Player to be named l8r")
    global dir_name
    dir_name = StringVar()
    #dir_name.set("Have to select the program directory")
    global dwg_file_list
    dwg_file_list = StringVar()
    global cnc_file_list
    cnc_file_list = StringVar()
    global pdf_file_list
    pdf_file_list = StringVar()
    global sht_file_list
    sht_file_list = StringVar()
    global rip_list
    rip_list = StringVar()
    global all_file_list
    all_file_list = StringVar()
    global curr_file_list
    curr_file_list = StringVar()
    #cnc_file_list.clear()

def popup_entry(msg):
    entry_info = simpledialog.askstring(title = "Please Enter Information" ,prompt = msg)
    return entry_info

def find_work_order():
    print('lantekinfov2_support.find_work_order')

    work_order_entry = popup_entry("Enter Work Order Number")
    work_order.set(work_order_entry)
    print("WORK ORDER: ",work_order)

    SQL = """SELECT DIS_NEST_NEST_00000100.CNC FROM DIS_NEST_NEST_00000100 CROSS JOIN DIS_MMNN_MMOO_00000200 WHERE DIS_NEST_NEST_00000100.JobRef = DIS_MMNN_MMOO_00000200.JobRef AND  DIS_MMNN_MMOO_00000200.UData3='%s'""" %(work_order.get())
    SQL2 = """SELECT DIS_MMNN_MMOO_00000200.UData1 FROM DIS_MMNN_MMOO_00000200 WHERE DIS_MMNN_MMOO_00000200.UData3 ='%s'""" %(work_order.get())#Customer Name
    SQL3 = """SELECT DIS_MMNN_MMOO_00000200.UData2 FROM DIS_MMNN_MMOO_00000200 WHERE DIS_MMNN_MMOO_00000200.UData3 ='%s'""" %(work_order.get())#Purchase Order
    SQL4 = """SELECT DIS_MMNN_MMOO_00000200.Descrip FROM DIS_MMNN_MMOO_00000200 WHERE DIS_MMNN_MMOO_00000200.UData3 ='%s'""" %(work_order.get())#Job Description
    SQL5 = """SELECT DIS_MMNN_MMOO_00000200.JobName FROM DIS_MMNN_MMOO_00000200 WHERE DIS_MMNN_MMOO_00000200.UData3 ='%s'""" %(work_order.get())#Job Name
    SQL6 = """SELECT DIS_MMNN_MMOO_00000200.UData4 FROM DIS_MMNN_MMOO_00000200 WHERE DIS_MMNN_MMOO_00000200.UData3 ='%s'""" %(work_order.get())#Job Tag
    SQL7 = """SELECT DIS_MMNN_MMOO_00000200.UData5 FROM DIS_MMNN_MMOO_00000200 WHERE DIS_MMNN_MMOO_00000200.UData3 ='%s'""" %(work_order.get())#Required Date   
    SQL8 = """SELECT DIS_MMNN_MMOO_00000200.CrtDate FROM DIS_MMNN_MMOO_00000200 WHERE DIS_MMNN_MMOO_00000200.UData3 ='%s'""" %(work_order.get())#Lantek Create Date       #DIS_MMNN_MMOO_00000200.UData3 = work_order 
    print(SQL)
    print(SQL2)
    print(SQL3)
    print(SQL4)
    print(SQL5)
    print(SQL6)
    print(SQL7)
    print(SQL8)    
    connection = pyodbc.connect(connection_string)
    cur = connection.cursor()
    cur.execute(SQL)
    rows = cur.fetchall()
    cnt = 0
    datastr=""
    cnc_file_list=[]
    cnc_file_list.clear()
    for row in rows:
        for r in row:
            if r != None:
                print (cnt, row[0])
                cnt = cnt + 1
                datastr=str(r) + ".sht"
                print("ROW:",row, " Datastr: ", datastr)
                cnc_file_list.append(datastr)
#    cur.close()
    cur.execute(SQL2)
    row2= cur.fetchone()
    customer.set(row2[0])
    print("SQL 2-Udata1: ",customer.get())
#    cur.close()
    cur.execute(SQL3)
    row3= cur.fetchone()
    purchase_order.set(row3[0])
    print("SQL 3-Udata2: ",purchase_order.get())
#    cur.close()
    cur.execute(SQL4)
    row4= cur.fetchone()
    job_desc.set(row4[0])
    print("SQL 4-Descrip: ",job_desc.get())
    w.Lantek_Description_STxt.replace('1.0',END,job_desc.get())
    cur.execute(SQL5)
    row5= cur.fetchone()
    job_name.set(row5[0])
    print("SQL 5-JobName: ",job_name.get())
    cur.execute(SQL6)
    row6= cur.fetchone()
    cust_tag.set(row6[0])
    print("SQL 6-Job Tag: ",cust_tag.get())
    cur.execute(SQL7)
    row7= cur.fetchone()
    required_date.set(row7[0])
    print("SQL 7-Required Date: ",required_date.get())
    cur.execute(SQL8)
    row8= cur.fetchone()
    lantek_date.set(row8[0])
    print("SQL 8-Lantek Date: ",lantek_date.get())

    cur.close()
    connection.close()

def find_purchase_order():
    print('lantekinfov2_support.find_purchase_order')

    purchase_order_entry = popup_entry("Enter Purchase Order Number / Code")
    purchase_order.set(purchase_order_entry)
    print("PURCHASE ORDER: ",purchase_order)

    SQL = """SELECT DIS_NEST_NEST_00000100.CNC FROM DIS_NEST_NEST_00000100 CROSS JOIN DIS_MMNN_MMOO_00000200 WHERE DIS_NEST_NEST_00000100.JobRef = DIS_MMNN_MMOO_00000200.JobRef AND  DIS_MMNN_MMOO_00000200.UData2='%s'""" %(purchase_order.get())
    SQL2 = """SELECT DIS_MMNN_MMOO_00000200.UData1 FROM DIS_MMNN_MMOO_00000200 WHERE DIS_MMNN_MMOO_00000200.UData2 ='%s'""" %(purchase_order.get())#Customer Name
    SQL3 = """SELECT DIS_MMNN_MMOO_00000200.UData3 FROM DIS_MMNN_MMOO_00000200 WHERE DIS_MMNN_MMOO_00000200.UData2 ='%s'""" %(purchase_order.get())#Work Order
    SQL4 = """SELECT DIS_MMNN_MMOO_00000200.Descrip FROM DIS_MMNN_MMOO_00000200 WHERE DIS_MMNN_MMOO_00000200.UData2 ='%s'""" %(purchase_order.get())#Job Description
    SQL5 = """SELECT DIS_MMNN_MMOO_00000200.JobName FROM DIS_MMNN_MMOO_00000200 WHERE DIS_MMNN_MMOO_00000200.UData2 ='%s'""" %(purchase_order.get())#Job Name
    SQL6 = """SELECT DIS_MMNN_MMOO_00000200.UData4 FROM DIS_MMNN_MMOO_00000200 WHERE DIS_MMNN_MMOO_00000200.UData2 ='%s'""" %(purchase_order.get())#Job Tag
    SQL7 = """SELECT DIS_MMNN_MMOO_00000200.UData5 FROM DIS_MMNN_MMOO_00000200 WHERE DIS_MMNN_MMOO_00000200.UData2 ='%s'""" %(purchase_order.get())#Required Date   
    SQL8 = """SELECT DIS_MMNN_MMOO_00000200.CrtDate FROM DIS_MMNN_MMOO_00000200 WHERE DIS_MMNN_MMOO_00000200.UData2 ='%s'""" %(purchase_order.get())#Lantek Create Date       #DIS_MMNN_MMOO_00000200.UData3 = work_order 
    print(SQL)
    print(SQL2)
    print(SQL3)
    print(SQL4)
    print(SQL5)
    print(SQL6)
    print(SQL7)
    print(SQL8)    
    connection = pyodbc.connect(connection_string)
    cur = connection.cursor()
    cur.execute(SQL)
    rows = cur.fetchall()
    cnt = 0
    datastr=""
    cnc_file_list=[]
    cnc_file_list.clear()
    for row in rows:
        for r in row:
            if r != None:
                print (cnt, row[0])
                cnt = cnt + 1
                datastr=str(r) + ".sht"
                print("ROW:",row, " Datastr: ", datastr)
                cnc_file_list.append(datastr)
#    cur.close()
    cur.execute(SQL2)
    row2= cur.fetchone()
    customer.set(row2[0])
    print("SQL 2-Udata1: ",customer.get())
#    cur.close()
    cur.execute(SQL3)
    row3= cur.fetchone()
    work_order.set(row3[0])
    print("SQL 3-Udata3: ",work_order.get())
#    cur.close()
    cur.execute(SQL4)
    row4= cur.fetchone()
    job_desc.set(row4[0])
    print("SQL 4-Descrip: ",job_desc.get())
    w.Lantek_Description_STxt.replace('1.0',END,job_desc.get())
    cur.execute(SQL5)
    row5= cur.fetchone()
    job_name.set(row5[0])
    print("SQL 5-JobName: ",job_name.get())
    cur.execute(SQL6)
    row6= cur.fetchone()
    cust_tag.set(row6[0])
    print("SQL 6-Job Tag: ",cust_tag.get())
    cur.execute(SQL7)
    row7= cur.fetchone()
    required_date.set(row7[0])
    print("SQL 7-Required Date: ",required_date.get())
    cur.execute(SQL8)
    row8= cur.fetchone()
    lantek_date.set(row8[0])
    print("SQL 8-Lantek Date: ",lantek_date.get())

    cur.close()
    connection.close()


def open_explorer():
    try:
        thisdir=dir_name.get()
    except:
        thisdir="P:\Bx1000\EUROMAC 2015"
    print("Open Explorer to",thisdir)
    try:
        expath=os.path.realpath(thisdir)
        os.startfile(expath)
    except:
        print("Failed file open test")
    status.set("Program Status: Explorer Window should be Open")



def close():
    print('lantekinfov2_support.close')
    sys.stdout.flush()

#def find_work_order():
#    print('lantekinfov2_support.find_work_order')
#    sys.stdout.flush()

def open_dir():
    print('lantekinfov2_support.open_dir')

    print("select_a_dir ver 6")
    dir_path = (filedialog.askdirectory(title = "Select Job's TopPunch SHT file DIRECTORY"))
    temp_file_list = os.listdir(dir_path)
    sht_files=[i for i in temp_file_list if i.endswith('.sht')]
    rip_files=[i for i in temp_file_list if i.endswith('.RIP')]
    pdf_files=[i for i in temp_file_list if i.endswith('.pdf')] 
    dwg_files=[i for i in temp_file_list if i.endswith('.dwg')]

    print("RIP- ",rip_files,"\n\n")
    all_file_list.set(temp_file_list)
    rip_list.set(rip_files)
    dir_name.set(dir_path)
    w.program_dir.replace('1.0',END,dir_name.get())
    curr_file_list.set(all_file_list)
    w.File_ListBox.configure(listvariable=curr_file_list.get())

    print("Dir NAme",dir_name.get())
  

def open_file():
    print('lantekinfov2_support.open_file')
    sys.stdout.flush()

def init(top, gui, *args, **kwargs):
    global w, top_level, root
    w = gui
    top_level = top
    root = top

def destroy_window():
    # Function which closes the window.
    global top_level
    top_level.destroy()
    top_level = None

if __name__ == '__main__':
    import lantekinfov2
    lantekinfov2.vp_start_gui()


