mui.init({
	beforeback: function() { 
		//获得父页面的webview
		var list = plus.webview.currentWebview().opener(); //触发父页面的自定义事件(refresh),从而进行刷新
		list.reload();
		//返回true,继续页面关闭逻辑
		return true;
	}
});

mui.plusReady(function(){
	window.self = plus.webview.currentWebview();
	window.Id = self.noteId;
	var $createTime = $("#createTime");
	var $name = $("#name");
	var $cc = $("#cur_cash");
	var $cdc = $("#credit_cash");
	var $rd = $("#return_date");
	if(Id <= 0) {
		$createTime.text("现在");
		$(".footer .add").attr('disabled', true);
	}
	$(".footer").on("tap", '.save',function(){
		$.ajax({
			url: qlgUrl("app/note/creditSave"),
			method: "POST",
			data: {
				id: Id,
				name: $name.val(),
				cur_cash: $cc.val(),
				credit_cash: $cdc.val(),
				return_date: $rd.val(),
			},
			dataType: "json",
			success: function(res){
				if(res.status != 1){
					mui.alert(res.msg);
				}else{
					mui.back();
				}
			},
			error: function(err){
				mui.alert("请求失败");
				self.back();
			}
		})
	})
	$(".footer").on("tap", '.add',function(){
		app.triggerShowAdd();
	})
	const app = new Vue({
		el: '#content',
		data() {
			return {
				listData: [
				],
				newData: {
					type: 1,
					amount: 0,
				},
				showAdd: false,
			}
		},
		methods: {
			delete2(index) {
				if(!confirm('确定要删除么?'))return ;
				$.ajax({
					url: qlgUrl("app/note/creditDeleteInfo"),
					method: "POST",
					data: {
						id: this.listData[index].id,
						credit_id: this.listData[index].credit_id,
					},
					dataType: "json",
					success: (res) => {
						if(res.status != 1){
							mui.alert(res.msg);
						}
						this.loadData();
					},
					error: (err) => {
						mui.alert("请求失败");
						this.loadData();
					}
				})
			},
			loadData() {
				$.ajax({
					url: qlgUrl("app/note/creditDetail"),
					method: "GET",
					data: {
						id: window.Id,
					},
					dataType: "json",
					success: function(res){
						if(res.status == 1){
							$createTime.text(res.data.create_time);
							$name.val(res.data.name);
							$cdc.val(res.data.credit_cash);
							$cc.val(res.data.cur_cash);
							$cc.data('val',res.data.cur_cash);
							$rd.val(res.data.return_date)
						}else{
							alert(res.msg);
						}
					},
					error: function(err){
						alert("请求失败");
					},
				});
				$.ajax({
					url: qlgUrl("app/note/creditDetailInfo"),
					method: "GET",
					data: {
						id: Id
					},
					dataType: 'json',
					success: (res) => {
						if(res.status == 1){
							this.listData = res.data;
							localStorage.setItem("LOCAL_CREDIT_"+Id, JSON.stringify(res.data));
						}else{
							mui.alert(res.msg);
						}
					},
					error: function(err){
						mui.alert("请求失败");
						self.back();
					}
				});
			},
			addNewData() {
				$.ajax({
					url: qlgUrl("app/note/creditAdd"),
					method: "POST",
					data: {
						id: Id,
						...this.newData,
					},
					dataType: "json",
					success: (res) => {
						this.newData = {
							type: 1,
							amount: 0,
						}
						this.showAdd = false;
						if(res.status != 1){
							mui.alert(res.msg);
						}
						this.loadData();
					},
					error: function(err){
						this.loadData();
					}
				})
			},
			triggerShowAdd() {
				this.showAdd = !this.showAdd;
			},
		},
		created() {
			if (Id <= 0) return;
			var localData = localStorage.getItem("LOCAL_CREDIT_"+Id);
			this.loadData();
			if(localData){
				try{
					JSON.parse(localData);
					this.listData = JSON.parse(localData);
				} catch(e) {
					console.log(e);
					this.listData = [];
				}
			}
		},
	})
})
