const _Http = getApp().globalData.http;

Page({
	data: {
		loading: true,
		active: "新建",
		"content": {
			nocache: true,
			"pageNumber": 1,
			"pageTotal": 1,
			"pageSize": 20,
			"isManage": 0, //默认是1，表示管理端，0表示非管理端，根据用户类型自动区分业务员还是经销商
			"isExport": 0,
			total: 0,
			"where": {
				"condition": "",
			},
			sort: []
		},
		filter: [{
			label: "品牌",
			index: null,
			showName: "brandname", //显示字段
			valueKey: "brandname", //返回Key
			selectKey: "brandname", //传参 代表选着字段 不传参返回整个选择对象
			value: "", //选中值
			list: []
		}, {
			label: "领域",
			index: null,
			showName: "tradefield", //显示字段
			valueKey: "tradefield", //返回Key
			selectKey: "tradefield", //传参 代表选着字段 不传参返回整个选择对象
			value: "", //选中值
			list: []
		}],
		createShow: false,
		types: [{
			name: "按(套)计算"
		}, {
			name: "按(个)计算"
		}]
	},
	onLoad(options) {
		this.getList(true)
	},
	/* 处理筛选 */
	handleFilter({
		detail
	}) {
		console.log(detail)
	},
	getList(init = false) {
		if (init.detail != undefined) init = init.detail;
		let content = this.data.content;
		if (init) content.pageNumber = 1;
		if (content.pageNumber > content.pageTotal) return;
		content.where.status = this.data.active;
		_Http.basic({
			"id": 20230114105002,
			"version": 1,
			content
		}).then(res => {
			console.log("工具借用列表", res)
			this.selectComponent('#ListBox').RefreshToComplete();
			this.setData({
				list: res.pageNumber == 1 ? res.data : this.data.list.concat(res.data),
				"content.pageNumber": res.pageNumber + 1,
				"content.pageTotal": res.pageTotal,
				"content.sort": res.sort,
				"content.total": res.total,
				loading: false
			})
		})
	},
	/* 去新增 */
	openAction() {
		this.setData({
			createShow: true
		})
	},
	createBor({
		detail
	}) {
		this.onCancel();
		if (detail.name == '按(套)计算') {
			wx.navigateTo({
				url: `/select/contract/index?params=${JSON.stringify({
                    "id": 20230114092502,
                    "content": {
                        sys_enterpriseid:2,
                        nocache: true,
                        "pageNumber": 1,
                        "pageTotal": 1,
                        "total": null,
                        "pageSize": 20,
                        "where": {
                            "condition": ""
                        }
                    }
                })}&radio=false`
			})
			getApp().globalData.handleSelect = this.handleCreated.bind(this);
		} else {
			_Http.basic({
				"id": 20230116092702,
				"content": {
					"sa_orderid": 0
				}
			}).then(res => {
				console.log("按个", res)
				if (res.msg != '成功') return wx.showToast({
					title: res.msg,
					icon: "none"
				});
				wx.navigateTo({
					url: '/packageA/borrow/detail?id=' + res.data.sa_orderid,
				})
			})
		}
	},
	handleCreated(e) {
		wx.showModal({
			title: '提示',
			content: `是否确认选择“${e.item.billno}”创建借用单`,
			complete: ({
				confirm
			}) => {
				if (confirm) _Http.basic({
					"id": 20230116092702,
					"content": {
						"sa_contractid": e.id[0],
						"sa_orderid": 0
					}
				}).then(res => {
					console.log("按套", res)
					if (res.msg != '成功') return wx.showToast({
						title: res.msg,
						icon: "none"
					});
					_Http.basic({
						"id": 20230116142602,
						"content": {
							"sa_orderid": res.data.sa_orderid,
							"sys_enterpriseid": res.data.sys_enterpriseid,
							"sa_contractid": e.id[0]
						}
					}, false).then(s => {
						console.log("复制合同商品", s)
						wx.redirectTo({
							url: '/packageA/borrow/detail?id=' + res.data.sa_orderid,
						})
					})
				})
			}
		})
	},
	onCancel() {
		this.setData({
			createShow: false
		})
	},
	/* 搜索 */
	onSearch({
		detail
	}) {
		this.setData({
			"content.where.condition": detail
		});
		this.getList(true)
	},
	/* 切换tabs */
	tabsChange(e) {
		let status = "";
		switch (e.detail.title) {
			case '全部':
				status = "";
				break;
			case '待确认':
				status = "交期待确认";
				break;
			default:
				status = e.detail.title
				break;
		}
		this.setData({
			active: status
		});
		this.getList(true);
	},
	onReady() {
		this.setListHeight()
	},
	/* 设置页面高度 */
	setListHeight() {
		this.selectComponent("#ListBox").setHeight(".total", this);
	},
})