#!/usr/bin/python
# coding=utf-8

from intra import *

import datetime
import time


def unf():
    unf1 = unf_sort(-30,-7)
    dr1 = unf1['dr']
    resident1 = unf1['resident']
    division1 = unf1['division']

    unf2 = unf_sort(-365,-31)
    dr2 = unf2['dr']
    resident2 = unf2['resident']
    division2 = unf2['division']

    length = max(len(dr1),
                 len(resident1),
                 len(dr2),
                 len(resident2))

    result=[]

    result.append(['主治醫師','科別','份數','住院醫師','份數','主治醫師','科別','份數','住院醫師','份數'])

    d1 = 0
    r1 = 0
    d2 = 0
    r2 = 0
    for i in range(length):
        r = []
        if i < len(dr1):
            r.append(dr1[i][0])
            if vs.has_key(dr1[i][0]):
                r.append(vs[dr1[i][0]])
            else:
                r.append('')
            r.append(dr1[i][1])
            d1 += dr1[i][1]
        else:
            r.append('')
            r.append('')
            r.append('')
        if i < len(resident1):
            r.append(resident1[i][0])
            r.append(resident1[i][1])
            r1 += resident1[i][1]
        else:
            r.append('')
            r.append('')
        if i < len(dr2):
            r.append(dr2[i][0])
            if vs.has_key(dr2[i][0]):
                r.append(vs[dr2[i][0]])
            else:
                r.append('')
            r.append(dr2[i][1])
            d2 += dr2[i][1]
        else:
            r.append('')
            r.append('')
            r.append('')
        if i < len(resident2):
            r.append(resident2[i][0])
            r.append(resident2[i][1])
            r2 += resident2[i][1]
        else:
            r.append('')
            r.append('')

        result.append(r)

#    output = open('/home/xfr/mysite/site_media/unf.html','w')
    output = open('/SharedDocs/html/media.ntuh.net/unf.html','w')

    print >> output, """
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<title>外科部病歷未完成</title>
</head>
<body>
"""

    print >> output, "<table><tbody align='center'>"
    print >> output, "<tr><td colspan='10'>%s</td></tr>" % time.asctime()
    print >> output, "<tr><td colspan='5'>%s</td><td colspan='5'>%s</td></tr>" % ('7至30日','超過30日')
    for r in result:
        print >> output ,"<tr>"
        for c in r:
            print >> output, "<td>%s</td>" % c
        print >> output ,"</tr>"

    print >> output, "<tr><td>---</td></tr>"
    print >> output, "<tr>"
    print >> output, "<td>總計</td><td></td><td>%i</td>" % d1
    print >> output, "<td>總計</td><td>%i</td>" % r1
    print >> output, "<td>總計</td><td></td><td>%i</td>" % d2
    print >> output, "<td>總計</td><td>%i</td>" % r2
    print >> output, "</tr>"

    print >> output, "</tbody></table>"
    print >> output, "<hr/>"
    print >> output, "<table><tbody align='center'>"


    div1 = dict(division1)
    div2 = dict(division2)

    for div in div1.keys():
        if not div2.has_key(div):
            div2[div] = 0

    for div in div2.keys():
        if not div1.has_key(div):
            div1[div] = 0

    div3 = {}
    for div in div1.keys():
        div3[div] = div1[div] + div2[div]


#    print div1
#    print div2
#    print div3

    division_sort = sorted(list(div3), key=lambda x: -div3[x])
 #   print division_sort

    print >> output, "<tr><td></td>"
    for div in division_sort:
        print >> output, "<td>%s</td>" % div
    print >> output, "</tr>"

    print >> output, "<tr><td>7至30日</td>"
    for div in division_sort:
        print >> output, "<td>%s</td>" % div1[div]
    print >> output, "</tr>"

    print >> output, "<tr><td>超過30日</td>"
    for div in division_sort:
        print >> output, "<td>%s</td>" % div2[div]
    print >> output, "</tr>"

    print >> output, "<tr><td>合計</td>"
    for div in division_sort:
        print >> output, "<td>%s</td>" % div3[div]
    print >> output, "</tr>"


    print >> output, "</tbody></table>"

    print >> output, "</body></html>"



    output.close()

    return result

def unf_month():
    day = datetime.date.today().day
    EndDate = datetime.date.today() + datetime.timedelta(days=-day)

    unf1 = unf_sort(-365,-day)
    dr1 = unf1['dr']
    resident1 = unf1['resident']
    division1 = unf1['division']

    length = max(len(dr1),
                 len(resident1))

    result=[]

    result.append(['主治醫師','科別','份數','住院醫師','份數'])

    d1 = 0
    r1 = 0
    for i in range(length):
        r = []
        if i < len(dr1):
            r.append(dr1[i][0])
            if vs.has_key(dr1[i][0]):
                r.append(vs[dr1[i][0]])
            else:
                r.append('')
            r.append(dr1[i][1])
            d1 += dr1[i][1]
        else:
            r.append('')
            r.append('')
            r.append('')
        if i < len(resident1):
            r.append(resident1[i][0])
            r.append(resident1[i][1])
            r1 += resident1[i][1]
        else:
            r.append('')
            r.append('')

        result.append(r)

#    output = open('/home/xfr/mysite/site_media/unf.html','w')
    output = open('/SharedDocs/html/media.ntuh.net/unf_month.html','w')

    print >> output, """
<html>
<head>
<meta http-equiv=Content-Type content="text/html; charset=utf-8">
<title>上月外科部病歷未完成</title>
</head>
<body>
"""

    print >> output, "<table><tbody align='center'>"
    print >> output, "<tr><td colspan='5'>%s</td></tr>" % time.asctime()
    print >> output, "<tr><td colspan='5'>%s前</td></tr>" % EndDate
    for r in result:
        print >> output ,"<tr>"
        for c in r:
            print >> output, "<td>%s</td>" % c
        print >> output ,"</tr>"

    print >> output, "<tr><td>---</td></tr>"
    print >> output, "<tr>"
    print >> output, "<td>總計</td><td></td><td>%i</td>" % d1
    print >> output, "<td>總計</td><td>%i</td>" % r1
    print >> output, "</tr>"
    print >> output, "</tbody></table>"

    print >> output, "<hr/>"
    print >> output, "<table><tbody align='center'>"


    div1 = dict(division1)

#    print div1
#    print div2
#    print div3

    division_sort = sorted(list(div1), key=lambda x: -div1[x])
 #   print division_sort

    print >> output, "<tr><td></td>"
    for div in division_sort:
        print >> output, "<td>%s</td>" % div
    print >> output, "</tr>"

    print >> output, "<tr><td>合計</td>"
    for div in division_sort:
        print >> output, "<td>%s</td>" % div1[div]
    print >> output, "</tr>"


    print >> output, "</tbody></table>"

    print >> output, "</body></html>"

    output.close()

    return result


unf()
unf_month()
