var TemplateItems = {
    template: '<div style="height: 100%;">\
    				<div class="title-name c-f16">\
	    				<span class="c-vam">{{name}}</span>\
	    			</div>\
	    			<div v-show="!items.length" class="c-t-center" style="margin-top: 200px;">\
	    				<img src="../../../images/fanhui_icon.png" class="c-vam"/>请在左侧“服务项”列表中选择\
	    			</div>\
	    			<div class="template-items" v-show="items.length">\
	    				<div class="item-card ptb10 plr15 mt10" v-for="(o, i) in items" @click="toDetail(o)">\
		    				<div class="c-nowrap">{{o.specialistServiceItemDO.title}}</div>\
			    			<div class="c-row c-f14 mt5">\
						        <div class="c-50">\
						        	<span class="c-909090">医&emsp;&emsp;院:</span>\
						        	<span>{{o.hospitalName}}</span>\
						        </div>\
						        <div class="c-50">\
						        	<span class="c-909090">执行人员:</span>\
						        	<span class="c-909090">待选择</span>\
						        </div>\
						    </div>\
						    <div class="c-row c-f14 mt5">\
						        <div class="c-50">\
						        	<span class="c-909090">计划时间:</span>\
						        	<span class="c-909090">待选择</span>\
						        </div>\
						        <div class="c-50">\
						        	<span class="c-909090">费&emsp;&emsp;用:</span>\
						        	<span>{{o.expense}}元</span>\
						        </div>\
						    </div>\
						    <img src="../../../images/qingchu_icon.png" class="item-delete-btn" @click.stop="deleteItem(o, i)"/>\
	    				</div>\
	    			</div>\
	    			<div class="foot-btns">\
			    		<a class="delete-btn c-t-center mr50" @click="deleteTemplate">\
			    			删除模板\
			    		</a>\
			    		<a class="save-btn c-t-center" @click="saveTemplate">\
			    			保存模板\
			    		</a>\
		    		</div>\
    			</div>',
    props: [],
    data: function() {
        return {
        	name: '',
        	items: [],
        	isNew: null,
        	templateId: "",
        	item: null
        }
    },
    beforeRouteEnter: function (to, from, next) {
	    next(function(vm) {
	    	var query = vm.$route.query
	    	vm.name = query.name || '模板未命名',
	    	vm.templateId = query.templateId
	    	if(from.path != "/template-item-detail" || from.path == "/") {
	    		vm.items = []
	    	}
	    	
	    	if(vm.isNew == true || vm.isNew === "true") {
	    		vm.isNew = true
	    	} else {
	    		vm.isNew = false
	    	}
	    })
	},
	created: function() {
		var vm = this
		EventBus.$on("add-template-item", function(o) {
			vm.items.push(o)
		})
	},
    mounted: function() {
    	var vm = this
		
    },
    methods: {

    	deleteItem: function(o, i) {
    		var vm = this
    		o.enable = true
    		vm.items.splice(i, 1)
    	},
    	saveTemplate: function() {
    		var vm = this
    		vm.loading.showModal()
    		rehaAPI.createTemplateDetail({
    			doctor: storage.docInfo.code,
    			json: JSON.stringify({
    				templateId: vm.templateId,
    				hospitalServiceItemId: _.pluck(vm.items, 'id').join(',')
    			}),
    			type: vm.isNew?"create":"edit"
    		}).then(function(res) {
    			if(res.status == 200) {
    				vm.loading.close()
    				toastr && toastr.success("保存成功")
    				EventBus.$emit("refresh-index")
        			vm.$router.replace("/")
    			} else {
    				vm.loading.close()
    				toastr && toastr.error(res.msg)
    			}
    		}).catch(function(e) {
    			vm.loading.close()
    			console.error(e)
    		})
    	},
    	deleteTemplate: function() {
    		var vm = this
    		
    		var d = dialog({
    			width: 300,
				content: '确定删除该模板？',
				okValue: '确认',
				ok: function () {
					vm.loading.showModal()
					rehaAPI.deleteTemplate({
						templateId: vm.templateId
		            }).then(function(res) {
		            	vm.loading.close()
		            	if(res.status == 200) {
		            		toastr && toastr.success("删除成功")
		            	} else {
		            		toastr && toastr.error(res.msg)
		            	}
        				
        				EventBus.$emit("refresh-index")
        				vm.$router.replace("/")
		        	}).catch(function(e) {
		        		vm.loading.close()
		        		console.error(e)
		        	})
				},
				cancelValue: '取消',
				cancel: function () {}
			});
			d.showModal();
    	},
    	toDetail: function(o) {
    		var vm = this
    		vm.item = o
    		vm.$router.push({
    			path:"/template-item-detail",
    			query: {
    				templateId: vm.templateId,
    				itemId: o.id
    			}
    		})
    		
    	}
    },
//  watch: {
//	    '$route': function (to, from) {
//	    	var vm = this
////	    	if(to.path == "/template-item-detail" && (from.path != "/template-item-detail" || from.path == "/")) {
////	    		EventBus.$emit("show-item-detail", vm.item)
////	    	}
//			if(to.path == "/template-item-detail") {
//	    		EventBus.$emit("show-item-detail", vm.item)
//	    	}
//	    }
//	 }
}