#encoding=utf-8
import time
from datetime import datetime


from genshi.builder import tag

from trac.core import *
from trac.web.chrome import add_stylesheet

def edittitle(_self, req):
    req.perm.assert_permission('LIST_MANAGE_EDIT')
    data = {}
    db = _self.env.get_db_cnx()
    error = ""

    add_stylesheet(req, 'listmanage/css/listmanage.css')

    actionName = req.args.get('actionName')
    when = datetime.now()
    when_ts = time.mktime(when.timetuple())

    cursor = db.cursor()
    list_id = req.args.get('list_id')

    if actionName:
        #タイトルの登録
        if actionName=='regist':
            try:
                list_id = req.args.get('list_id')
                title = req.args.get('title')
                description = req.args.get('description')
                parent_list_id = req.args.get('parent_list_id')
                wiki_name = req.args.get('wiki_name')
                mode = req.args.get('mode')
                if title:
                    try:
                        sql = ''
                        if mode == 'add':
                            sql = (("""insert into list_title values('%s','%s','%s',ifnull((select max(order_no)+1 from list_title),1),'%s','%s',null)""")
                                    % (list_id, title, description, parent_list_id, wiki_name))
                        else:
                            sql = (("""update list_title set title='%s',description='%s',parent_list_id='%s',wiki_name='%s' where list_id='%s'""")
                                    % (title, description, parent_list_id, wiki_name, list_id))
                        _self.log.debug('%s' % sql)
                        cursor.execute(sql)
                    except Exception,e:
                        _self.log.debug('%s' % e)
                        raise e

                    db.commit()
            except Exception,e:
                _self.log.debug('%s' % e)
                raise e

            req.redirect(_self.env.href.listmanage())

    #リスト情報検索
    title = ""
    description = ""
    order_no = ""
    parent_list_id = ""
    wiki_name = ""
    mode = actionName
    try:
        if actionName == 'update':
            sql = ("""SELECT title,description,order_no,parent_list_id,wiki_name FROM list_title where list_id = '%s'""" % (list_id))
            _self.log.debug('%s' % sql)
            cursor.execute(sql)
            row = cursor.fetchone()

            title = row[0]
            description = row[1]
            order_no = row[2]
            parent_list_id = row[3]
            wiki_name = row[4]
        elif actionName == 'add':
            list_id=""
            title = ""
            description = ""
            order_no = ""
            parent_list_id = ""
            wiki_name = ""

    except Exception,e:
        _self.log.debug('%s' % e)
        raise e

    data = {"list_id":list_id,"title":title,'description':description,'order_no':order_no,'parent_list_id':parent_list_id,'wiki_name':wiki_name,'error':error,'mode':mode}

    db.close()
    return 'listtitle.html', data, None


