<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">
						<Input v-model="form.orderNumber"></Input>
					</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>
	</div>
</template>

<script>
	import {
		getMiandan,
		addMiandan,
		closeMiandan
	} from "@/api/app";
	export default {
		name: "logiticsCompany",
		data() {
			return {
				form: {
					orderNumber:'',
					freeAmount:'',
					regionId:JSON.parse(this.getStore("user")).departmentId
				},
				isOpen:false,
				searchForm: {
					shopId:'',
					headUserId:'',
					status:'',
					productName:'',
					isFace:'',
					pageNumber: 1,
					pageSize: 10,
				},
				columns: [{
						type: "index",
						width: 60,
						align: "center",
					},
					{
						title: "时间",
						key: "creatTime",
						align: "center",
					},
					{
						title: "免单人数",
						key: "orderNumber",
						align: "center",
					},
					{
						title: "免单金额",
						key: "freeAmount",
						align: "center",
					}
				],
			};
		},
		methods: {
			init() {
				this.getDataList();
			},
			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;
						}
					}
				});
			},
			add() {
				addMiandan(this.form).then((res) => {
					if (res.success) {
						this.$Message.success("操作成功");
					}
				});
			},
			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>