<style lang="less">
	@import "@/styles/table-common.less";
</style>
<template>
	<div class="search">
		<Card>
			<Row>
				<Form ref="searchForm" v-for="(item,index) in form.rules" :key="index" :model="searchForm" inline :label-width="100">
					<FormItem label="名次" prop="name">
						<Input v-model="item.orderNum"></Input>
					</FormItem>
					<FormItem label="奖励金额" prop="name">
						<Input v-model="item.reward"></Input>
					</FormItem>
					<Icon type="ios-remove-circle-outline" @click="del(index)" size="30" color="red" />
				</Form>
				
			</Row>
			<Row>
				<Form ref="searchForm" :model="searchForm" inline :label-width="100">
					<FormItem style="margin-left: -35px" class="br">
						<Button @click="add">新增</Button>
						<Button @click="close">关闭免单</Button>
						<Button @click="submit" type="primary">保存并开启</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 {
		addJiangLi,
		selectJiangLi,
		closeJiangLi
	} from "@/api/app";
	export default {
		name: "logiticsCompany",
		data() {
			return {
				form: {
					rules:[],
					regionId:JSON.parse(this.getStore("user")).departmentId
				},
				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() {
				selectJiangLi({
					regionId:JSON.parse(this.getStore("user")).departmentId
				}).then((res) => {
					if (res.success) {
						if(res.result == null){
							this.form.rules = [{
								orderNum:"",
								reward:""
							}]
						}else{
							this.form = res.result;
							this.form.rules = JSON.parse(this.form.rules)
						}
					}
				});
			},
			submit() {
				this.form.rules = JSON.stringify(this.form.rules)
				addJiangLi(this.form).then((res) => {
					if (res.success) {
						this.$Message.success("操作成功");
						this.getDataList()
					}
				});
			},
			add(){
				let rules = {
					orderNum:"",
					reward:""
				}
				this.form.rules.push(rules)
			},
			del(i){
				this.form.rules.splice(i, 1);
			},
			close() {
				this.$Modal.confirm({
					title: "确认关闭",
					content: "您确认要关闭配送员奖励吗？",
					loading: true,
					onOk: () => {
						closeJiangLi({
							regionId:JSON.parse(this.getStore("user")).departmentId
						}).then((res) => {
							if (res.success) {
								this.$Message.success("操作成功");
								this.getDataList()
							}
						});
						this.$Modal.remove();
					},
				});
			},
		},
		mounted() {
			this.init();
		},
	};
</script>