# -*- coding: utf-8 -*-
from flask import Blueprint, render_template,request
from flask_login import login_required,current_user
from models.datacenter import Datacenter
from models.log import Log
from views.log import addlog
from views.functions import responsejson
import json,re
from application import app,db
from views.functions import visible
import datetime

datacenter = Blueprint('datacenter', __name__)

@datacenter.route("/")
@login_required
def index():
    show = visible()
    dcinfo=Datacenter.query.filter(Datacenter.deleted==0).order_by(Datacenter.id).all()
    return render_template("idc/datacenter.html",dcinfo=dcinfo,show=show)

@datacenter.route("/add",methods=['GET', 'POST'])
@login_required
def add():
    code=0
    msg="添加机房成功"
    if request.method == 'POST':
        name=request.form['name']
        short_name=request.form['short_name']
        address=request.form['address']
        idc_label=request.form['idc_label']
        content=request.form['content']
        if len(name)<=0:
            code=1;
            msg="请输入机房名称"
            return responsejson(code,msg)
        if len(short_name)<=0:
            code=1;
            msg="请输入机房简称"
            return responsejson(code,msg)
        if len(address)<=0:
            code=1;
            msg="请输入机房地址"
            return responsejson(code,msg)
        hasIn=Datacenter.query.filter(Datacenter.name==name).all()
        if hasIn:
            code=1;
            msg="此机房名称已经有了"
            return responsejson(code,msg)
        #idc_label默认值为SH-IDC,后面如果有需要把这个地方打开
        idc_label="TJ-IDC"
        dctarget=Datacenter(name=name,short_name=short_name,address=address,idc_label=idc_label,content=content)
        db.session.add(dctarget)
        db.session.commit()
        logmsg="添加机房,id:%d,机房名称:%s"%(dctarget.id,name)
        addlog(logmsg,1)
    else:
        code=1
        msg="添加机房失败"
    return responsejson(code,msg)

@datacenter.route("/<int:id>",methods=['GET', 'POST'])
@login_required
def detail(id):
    jsonval={}
    intreg=re.compile('^\d*$')
    if not intreg.match(str(id)):
        return app.response_class(json.dumps(jsonval), mimetype='application/json')
    info=Datacenter.query.filter(Datacenter.id==id).first()
    if info :
        jsonval['id']=info.id
        jsonval['name']=info.name
        jsonval['short_name']=info.short_name
        jsonval['address']=info.address
        jsonval['content']=info.content
        jsonval['idc_label']=info.idc_label
    return app.response_class(json.dumps(jsonval), mimetype='application/json')

@datacenter.route("/modify/<int:id>",methods=['GET', 'POST'])
@login_required
def modify(id):
    logmsg=""
    code=0
    msg="修改机房成功"
    intreg=re.compile('^\d*$')
    if not intreg.match(str(id)):
        code=1
        msg="修改机房失败:id有问题"
        return responsejson(code,msg)
    info=Datacenter.query.filter(Datacenter.id==id).first()
    name=request.form['name']
    short_name=request.form['short_name']
    address=request.form['address']
    content=request.form['content']
    idc_label=request.form['idc_label']
    if info.name == name and info.short_name == short_name and info.address == address and info.content == content:
        return responsejson(code, "内容无修改")
    else:
        if info.name!=name:
            logmsg=logmsg+"name:%s 更改为 %s"%(info.name,name)+","
            info.name=name
        if info.short_name!=short_name:
            logmsg=logmsg+"short_name:%s 更改为 %s"%(info.short_name,short_name)+","
            info.short_name=short_name
        if info.address!=address:
            logmsg=logmsg+"address:%s 更改为 %s"%(info.address,address)+","
            info.address=address
        if info.content!=content:
            logmsg=logmsg+"content:%s 更改为 %s"%(info.content,content)+","
            info.content=content
        if info.idc_label!=idc_label:
            logmsg=logmsg+"idc_label:%s 更改为 %s"%(info.idc_label,idc_label)+","
            info.idc_label=idc_label
        info.updated=datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        db.session.commit()
        logmsg="修改机房信息,id:%d,%s"%(id,logmsg)
        addlog(logmsg,1)
        return responsejson(code,msg)

@datacenter.route("/delete/<int:id>",methods=['GET', 'POST'])
@login_required
def delete(id):
    logmsg=""
    import re
    intreg=re.compile('^[1-9]\d*$')
    if not intreg.match(str(id)):
        return responsejson(1,"删除机房成功")
    info=Datacenter.query.filter(Datacenter.id==id).first()
    db.session.delete(info)
    db.session.commit()
    logmsg="删除机房,机房id:%d"%(id)
    addlog(logmsg,1)
    return responsejson(0,"删除机房成功")