<style lang="less">
	@import "@/styles/table-common.less";
</style>
<template>
	<div class="search">
		<Card>
			<Row>
				<Form ref="searchForm" :model="searchForm" inline :label-width="100">
					
					<FormItem label="" prop="name">
						<div style="display: flex;">
							<div style="width: 40px;">完成</div>
							<Input v-model="form.orderNumber" style="width: 60px;"></Input>
							<div style="width: 180px;">&nbsp;&nbsp;单后免单</div>
						</div>
					</FormItem>
					<FormItem label="免单金额" prop="name">
						<Input v-model="form.freeAmount"></Input>
					</FormItem>
					<FormItem style="margin-left: -35px" class="br">
						<Button v-if="isOpen" @click="add" type="primary">开启</Button>
						<Button v-else @click="add" type="primary">保存</Button>
						
						<Button @click="close">关闭免单</Button>
					</FormItem>
				</Form>
			</Row>
			<Table :loading="loading" border :columns="columns" :data="data" :size="tableSize" ref="table"
				sortable="custom" ></Table>
			<Row type="flex" justify="end" class="page">
				<Page :current="searchForm.pageNumber" :total="total" :page-size="searchForm.pageSize"
					@on-change="changePage" @on-page-size-change="changePageSize" :page-size-opts="[10, 20, 50]"
					size="small" show-total show-elevator show-sizer></Page>
			</Row>
		</Card>
		<Modal title="订单详情" v-model="Visiable" :mask-closable="false" :scrollabele="true" :width="1000">
			<logistics-address v-if="Visiable" @closePage="closePage" ref="dialog"></logistics-address>
			<div slot="footer" v-show="Visiable">
				<Button type="text" @click="Visiable = false">关闭</Button>
			</div>
		</Modal>
	</div>
</template>

<script>
	import {
		getMiandan,
		addMiandan,
		JiangLiList,
		closeMiandan
	} from "@/api/app";
	import {
		getOrderDetail
	} from "@/api/index";
	import logisticsAddress from "@/views/sys/order-manage/logisticsAddress";
	export default {
		name: "logiticsCompany",
		components: {
			logisticsAddress,
		},
		data() {
			return {
				Visiable: false,
				form: {
					orderNumber:'',
					freeAmount:'',
					regionId:JSON.parse(this.getStore("user")).departmentId
				},
				isOpen:false,
				searchForm: {
					regionId:JSON.parse(this.getStore("user")).departmentId,
					pageNum: 1,
					pageSize: 10,
					dealingsWay:'免单奖励'
				},
				columns: [{
						type: "index",
						width: 60,
						align: "center",
					},
					{
						title: "类型",
						key: "linkType",
						align: "center",
						render: (h, params) => {
							let re = ""
							if (params.row.linkType == 0) {
								re = "用户";
							} else if (params.row.type == 1) {
								re = "配送员";
							}
							return h(
								"div",
								re
							);
						}
					},
					{
						title: "交易方式/附言",
						key: "dealingsWay",
						align: "center",
					},
					{
						title: "金额",
						key: "amount",
						align: "center",
					},
					{
						title: "交易时间",
						key: "dealingsTime",
						align: "center",
					},
					{
						title: "操作",
						key: "action",
						width: 120,
						align: "center",
						render: (h, params) => {
							return h(
								"a", {
									on: {
										click: () => {
											this.showFreeOrderDetail(params.row);
										},
									},
								},
								"查看订单"
							);
						},
					}
				],
				data:[],
				total: 0,
			};
		},
		methods: {
			init() {
				this.getDataList();
				this.getJiangLiList();
			},
			closePage(){
				this.Visiable = false;
				this.getJiangLiList();
			},
			getJiangLiList(){
				JiangLiList(this.searchForm).then((res) => {
					if (res.success) {
						this.data = res.result.records
						this.total = res.result.total;
						if (this.data.length == 0 && this.searchForm.pageNum > 1) {
							this.searchForm.pageNum -= 1;
							this.getJiangLiList();
						}
					}
				});
			},
			getDataList() {
				getMiandan({
					regionId:JSON.parse(this.getStore("user")).departmentId
				}).then((res) => {
					if (res.success) {
						if(res.result == null){
							this.isOpen = true
						}else{
							this.form = res.result;
						}
					}
				});
			},
			showDetail(v) {
				for (let attr in v) {
					if (v[attr] == null) {
						v[attr] = "";
					}
				}
				let str = JSON.stringify(v);
				let data = JSON.parse(str);
				this.Visiable = true;
				this.$nextTick(() => {
					this.$refs.dialog.initRecharge(data);
				});
			},
			showFreeOrderDetail(row) {
				if (!row.linkId) {
					this.$Message.warning("当前记录没有关联订单");
					return;
				}
				getOrderDetail(row.linkId).then((res) => {
					if (res.success) {
						this.showDetail(res.result || {
							id: row.linkId
						});
					} else {
						this.$Message.error(res.message || "查询订单详情失败");
					}
				}).catch((err) => {
					this.$Message.error((err && err.message) || "查询订单详情失败");
				});
			},
			add() {
				addMiandan(this.form).then((res) => {
					if (res.success) {
						this.$Message.success("操作成功");
						this.getJiangLiList();
					}
				});
			},
			changePage(v) {
				this.searchForm.pageNum = v;
				this.getJiangLiList();
			},
			changePageSize(v) {
				this.searchForm.pageSize = v;
				this.getJiangLiList();
			},
			close() {
				this.$Modal.confirm({
					title: "确认关闭",
					content: "您确认要关闭免单优惠吗？",
					loading: true,
					onOk: () => {
						closeMiandan({
							regionId:JSON.parse(this.getStore("user")).departmentId
						}).then((res) => {
							
							if (res.success) {
								this.$Message.success("操作成功");
							}
						});
						this.$Modal.remove();
					},
				});
			},
		},
		mounted() {
			this.init();
		},
	};
</script>