package web

import (
	"fmt"
	"net/http"
	"qfw/util"
	. "tools"
)

func init() {
	//静态服务
	Route.PathPrefix("/static/").Handler(http.FileServer(http.Dir("./res")))
	//登录
	Route.HandleFunc("/", Index)
	Route.HandleFunc("/login", Login).Methods("POST") //登录
	//任务管理
	Route.HandleFunc("/admin/task", Main)                                //
	Route.HandleFunc("/admin/task/save", Save).Methods("POST")           //保存任务
	Route.HandleFunc("/admin/task/delete", DDelete).Methods("POST")      //删除任务
	Route.HandleFunc("/admin/task/run", TTRun).Methods("POST")           //启动任务
	Route.HandleFunc("/admin/task/stop", TTStop).Methods("POST")         //停止任务
	Route.HandleFunc("/admin/task/pause", TTPause).Methods("POST")       //暂停任务
	Route.HandleFunc("/admin/task/test", TTset).Methods("POST")          //测试任务
	Route.HandleFunc("/admin/task/csvstatus", Csvstatus).Methods("POST") //查询测试状态
	Route.HandleFunc("/admin/task/csvdown", CsvDown)                     //下移
	Route.HandleFunc("/admin/task/updateRun", UpdateRun)                 //更新运行
	Route.HandleFunc("/admin/task/updatestatus", updatestatus)           //更新运行修改状态
	Route.HandleFunc("/admin/task/updatetaskrule", UpdateTaskRule)       //更新运行修改状态

	//分类管理
	Route.HandleFunc("/admin/class", class)                              //
	Route.HandleFunc("/admin/class/save", classsave).Methods("POST")     //保存分类
	Route.HandleFunc("/admin/class/delete", classdelete).Methods("POST") //删除分类
	Route.HandleFunc("/admin/class/getCList", getCList).Methods("POST")  //获取class
	//规则管理
	Route.HandleFunc("/admin/rule", rule)                              //规则页面
	Route.HandleFunc("/admin/rule/save", rulesave).Methods("POST")     //保存规则
	Route.HandleFunc("/admin/rule/delete", ruledelete).Methods("POST") //删除规则
	Route.HandleFunc("/admin/rule/run", rulerun).Methods("POST")       //测试规则
	Route.HandleFunc("/admin/rule/getList", getList).Methods("POST")   //获取rule
	Route.HandleFunc("/admin/rule/shift", Shift).Methods("POST")       //改变位置

	//分布式页面
	Route.HandleFunc("/admin/distributed", distributed)
	Route.HandleFunc("/admin/distributed/deployOneInstance", deployOneInstance).Methods("POST")   //部署单个实例
	Route.HandleFunc("/admin/distributed/executeOneInstance", executeOneInstance).Methods("POST") //执行单个实例
	Route.HandleFunc("/admin/distributed/releaseOneInstance", releaseOneInstance).Methods("POST") //释放单个实例
	Route.HandleFunc("/admin/distributed/deleteOneInstance", deleteOneInstance).Methods("POST")   //删除单个实例
	Route.HandleFunc("/admin/distributed/applyInstance", applyInstance).Methods("POST")           //申请实例
	Route.HandleFunc("/admin/distributed/updateInstance", updateInstance).Methods("POST")         //更新实例
	Route.HandleFunc("/admin/distributed/createTask", createTask).Methods("POST")                 //生成任务
	Route.HandleFunc("/admin/distributed/deployAllInstance", deployAllInstance).Methods("POST")   //部署所有实例
	Route.HandleFunc("/admin/distributed/executeAllInstance", executeAllInstance).Methods("POST") //执行所有实例
	Route.HandleFunc("/admin/distributed/settingInstance", settingInstance).Methods("POST")       //

}

func Index(w http.ResponseWriter, r *http.Request) {
	ExecuteTpl("index.html", nil, w)
}

var SN = "session-name"

func Login(w http.ResponseWriter, r *http.Request) {
	defer Catch()
	s_name := r.FormValue("s_name")
	s_pwd := r.FormValue("s_pwd")
	//查库
	if s_name != "" && s_pwd != "" {
		res, b := MgoClass.FindOneByField("rc_user", fmt.Sprintf(`{"s_name":"%s","s_pwd":"%s"}`, s_name, util.GetMd5String(s_pwd)), `{"_id":1}`)
		if b {
			if res != nil && *res != nil {
				session, _ := Store.Get(r, SN)
				session.Values["s_name"] = s_name
				session.Save(r, w)
				http.Redirect(w, r, "/admin/task", http.StatusFound)
				return
			}
		}
	}
	http.Redirect(w, r, "/", http.StatusFound)
}
