<template>
<el-container>
	<el-header>
		<div class="left-panel">
			<el-input v-model="search.keyword" placeholder="关键词" style="width:150px; margin-right: 10px;" clearable />
			<el-input v-model="search.store" placeholder="下单店铺" style="width:150px; margin-right: 10px;" clearable />
			<el-input v-model="search.username" placeholder="购买人" style="width:150px; margin-right: 10px;" clearable />
			<el-select v-model="search.pay_status" placeholder="支付状态" style="width:150px; margin-right: 10px;" clearable>
				<el-option :label="item.title" :value="item.value" v-for="(item, index) in [{title: '未支付', value: 0}, {title: '已支付', value: 1}]" :key="index"></el-option>
			</el-select>
			<el-select v-model="search.status" placeholder="订单状态" style="width:150px; margin-right: 10px;" clearable>
				<el-option :label="item.title" :value="item.value" v-for="(item, index) in [{title: '待付款', value: 0}, {title: '待发货', value: 1}, {title: '待收货', value: 2}, {title: '已签收', value: 3}, {title: '退货', value: 4}]" :key="index"></el-option>
			</el-select>
			<el-date-picker v-model="search.date" type="daterange" start-placeholder="开始时间" end-placeholder="结束时间" format="YYYY-MM-DD" value-format="YYYY-MM-DD" style="width: 200px; margin-right: 10px;" />
			<el-button-group style="margin-left: 10px;">
				<el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
			</el-button-group>
		</div>
		<div class="right-panel">
			<el-button-group>
				<el-button type="danger" icon="el-icon-plus" @click="add">添加</el-button>
			</el-button-group>
		</div>
	</el-header>
	<el-main class="nopadding">
		<scTable tableName="member" ref="table" :apiObj="list.apiObj" :column="list.column" row-key="id" @selection-change="selectionChange" border stripe>
			<el-table-column type="selection" width="50"></el-table-column>
			<template #status="scope">
				<el-tag :type="status[scope.row.status]?.type">{{ status[scope.row.status]?.label }}</el-tag>
			</template>
			<template #username="scope">
				{{scope.row.member?.nickname}}（{{ scope.row.member?.username }}）
			</template>
			<template #delivery_info="scope">
				{{scope.row.delivery_info?.name}}（{{ scope.row.delivery_info?.mobile }}）
			</template>
			<el-table-column label="操作" fixed="right" align="center" width="140">
				<template #default="scope">
					<el-button-group>
						<el-button type="primary" @click="table_show(scope.row, scope.$index)">详情</el-button>
						<!-- <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
							<template #reference>
								<el-button type="danger">删除</el-button>
							</template>
						</el-popconfirm> -->
					</el-button-group>
				</template>
			</el-table-column>
		</scTable>
	</el-main>
	<save v-if="dialog.save" ref="saveBox" @success="upsearch" @closed="dialog.save=false" />
	<detail v-if="dialog.detail" ref="detailBox" @closed="dialog.detail=false" />
</el-container>
</template>

<script>
import save from './save.vue'
import detail from './detail.vue'

export default{
	name: 'order.list',
	components:{
		save,
		detail
	},
	data(){
		return {
			dialog: {search: false, import: false, print: false},

			list: {
				apiObj: this.$API.order.lists.list,
				column: [
					{prop: 'id', label: 'ID', width: 80},
					{prop: 'order_no', label: '订单编号', width: 240},
					{prop: 'username', label: '购买人', width: 240, showOverflowTooltip: true},
					{prop: 'amount', label: '订单总金额', width: 120},
					{prop: 'pay_time', label: '支付时间', width: 140},
					{prop: 'delivery_info', label: '收货人', width: 240, showOverflowTooltip: true},
					{prop: 'status', label: '状态', width: 120},
					{prop: 'created_at', label: '添加时间', width: 140},
					{prop: 'updated_at', label: '更新时间', width: 140},
				],
			},
			selection: [],
			search: {},
			status: {
				'-1': {label: '已取消', type: 'danger'},
				0: {label: '待付款', type: 'primary'},
				1: {label: '待发货', type: 'warning'},
				2: {label: '待收货', type: 'info'},
				3: {label: '已签收', type: 'success'},
				4: {label: '退货', type: 'warning'},
				5: {label: '已退款', type: 'info'},
				6: {label: '已关闭', type: 'warning'},
			},
		}
	},
	methods:{
		upsearch(){
			this.$refs.table.reload(this.search);
		},
		moreUpsearch(search){
			this.search = search;
			this.upsearch();
		},
		moreSearch(){
			this.dialog.search = true
			this.$nextTick(() => {
				this.$refs.searchBox.open().setData(this.search)
			})
		},
		//表格选择后回调事件
		selectionChange(selection){
			this.selection = selection;
		},
		batch_del(){

		},
		table_show(row){
			this.dialog.detail = true
			this.$nextTick(() => {
				this.$refs.detailBox.open().setData(row)
			})
		},
		add(){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveBox.open()
			})
		},
		table_edit(row){
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveBox.open('edit').setData(row)
			})
		},
		async table_del(row){
			var reqData = {id: row.id}
			var res = await this.$API.order.lists.delete.post(reqData);
			if(res.code == 1){
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.upsearch()
				this.$message.success("删除成功")
			}else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		}
	}
}
</script>
