<template>
	<div class="order-wrapper">
		<!-- <breadcrumb :item-name="['订单管理', '订单列表']"></breadcrumb> -->
		<el-card>
			<!-- 搜索区域 -->
			<el-row>
				<el-col>
					<div class="flex" style="justify-content: flex-end;padding-bottom: 20px;">
						<el-button type="primary" @click="oneWork">一键处理订单</el-button>
						<el-button type="primary" icon="el-icon-refresh-right" @click="getList('reload')">刷新</el-button>
					</div>

				</el-col>


				<!-- <el-col :span="8">
					<el-input placeholder="请输入内容" v-model="queryInfo.query" class="input-with-select">
						<el-button slot="append" icon="el-icon-search" @click="getList()"></el-button>
					</el-input>
				</el-col> -->
			</el-row>
			<!-- 订单表格区域 -->
			<el-table :data="list" border style="width: 100%" max-height="620">
				<!-- <el-table-column fixed="left" type="selection" width="55">
				</el-table-column> -->
				<el-table-column prop="id" label="id" width="80">
				</el-table-column>
				<el-table-column prop="order_id" label="订单编号" width="200">
				</el-table-column>
				<el-table-column prop="sys_address" label="是否系统默认" width="170">
					<template slot-scope="scope">
						<span
							:style="{'color': (scope.row.sys_address == 1 || scope.row.sys_phone == 1 )? '': '#67c23a'}">{{(scope.row.sys_address == 1 || scope.row.sys_phone == 1 )? '是': '否'}}</span>
					</template>
				</el-table-column>
				<el-table-column prop="status" label="订单状态" width="170">
					<template slot-scope="scope">
						{{scope.row.status| showStatus}}
					</template>
				</el-table-column>
				<el-table-column prop="consignee_name" label="收货人" width="200">
				</el-table-column>
				<el-table-column prop="consignee_phone" label="收货手机号" width="200">
				</el-table-column>
				<el-table-column prop="consignee_address" label="收货地址" width="350">
				</el-table-column>
				<el-table-column prop="pay_price" label="支付金额(元)" width="150">
				</el-table-column>
				<el-table-column prop="commission" label="手续费(元)" width="150">
				</el-table-column>
				<el-table-column prop="compensation" label="赔付金额(元)" width="150">
					<template slot-scope="scope">
						{{scope.row.status == 5 ? scope.row.compensation: ''}}
					</template>
				</el-table-column>
				<!-- <el-table-column prop="token" label="Token" width="350">
				</el-table-column> -->
				<!-- <el-table-column prop="status" label="是否已使用">
					<template slot-scope="scope">
						<el-tag type="success" v-if="scope.row.status == 0">待使用</el-tag>
						<el-tag type="info" v-if="scope.row.status == 1">已使用</el-tag>
					</template>
				</el-table-column> -->
				<el-table-column prop="createtime" label="创建时间" width="300">
					<template slot-scope="scope">
						{{showTime(scope.row.createtime)}}
					</template>
				</el-table-column>
				<el-table-column fixed="right" label="操作" width="150">
					<template slot-scope="scope">
						<el-button type="text" size="small" v-if="scope.row.status == 2"
							@click="handleOrder('是否确认收货？',scope.row)">确认收货</el-button>
						<el-button type="text" size="small" v-if="scope.row.status == 4" @click="handleOrder('是否确认申请理赔？',scope.row)">申请理赔</el-button>
						<!-- <el-button type="text" size="small" >结算</el-button> -->
						<el-button type="text" size="small" v-if="scope.row.status == 1"
							@click="handleOrder('是否确认申请退款？',scope.row)">申请退款</el-button>
							
						<el-button type="text" size="small" v-if="scope.row.status == 2"
							@click="getExpressDelivery(scope.row)">查看物流</el-button>
					</template>
				</el-table-column>
			</el-table>
			<!-- 分页区域 -->
			<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
				:current-page="queryInfo.page" :page-sizes="[10, 20, 50, 100]" :page-size="queryInfo.pagesize"
				layout="total, sizes, prev, pager, next, jumper" :total="total">
			</el-pagination>
		</el-card>
		<!-- 修改地址对话框 -->
		<el-dialog title="购买Token" :visible.sync="dialogTableVisible">
			<el-form :model="form">
				<el-form-item label="购买数量" :label-width="formLabelWidth">
					<el-input v-model="form.num" type="number"></el-input>
				</el-form-item>
				<el-form-item label="交易密码" :label-width="formLabelWidth">
					<el-input v-model="form.transaction" type="password"></el-input>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="dialogTableVisible = false">取 消</el-button>
				<el-button type="primary" @click="goBuy">确 定</el-button>
			</div>
		</el-dialog>
		<el-dialog
		  title="物流进度"
		  :visible.sync="logProgDialogVisible"
		  width="30%"
		>
		  <el-timeline>
		    <el-timeline-item
		      v-for="(activity, index) in logProgInfo"
		      :key="index"
		      :timestamp="activity.time"
		    >
		      {{ activity.context }}
		    </el-timeline-item>
		  </el-timeline>
		</el-dialog>
	</div>
</template>

<script>
	import {
		createCode,
		getCodeList,
		createToken,
		getTokenList,
		getExpressDelivery
	} from '@/request/agent.js'
	import {
		getOrderList,
		batchOrder
	} from '@/request/user.js'
	import {
		showTime
	} from '@/assets/js/tools.js'
	import {
		Switch
	} from 'element-ui'
	export default {
		name: 'Order',
		data() {
			return {
				logProgInfo: [],
				logProgDialogVisible: false,
				loading: false,
				formLabelWidth: '80px',
				form: {
					num: '',
					transaction: ''
				},
				dialogTableVisible: false,
				queryInfo: {
					keyword: '',
					page: 1,
					limit: 10
				},
				list: [],
				total: 0,
				dataloading: false,
				sqtkitem: {},
			}
		},
		created() {
			this.getList()
		},
		computed: {

		},
		filters: {
			showStatus(val) {
				let str = ''
				if (val) {
					switch (val) {
						case 0:
							str = '待支付'
							break;
						case 1:
							str = '待发货'
							break;
						case 2:
							str = '待收货'
							break;
						case 3:
							str = '待收货'
							break;
						case 4:
							str = '已完成'
							break;
						case 5:
							str = '已退款'
							break;
						case 5:
							str = '已结算'
							break;
						default:
							break;
					}
				}
				return str
			}
		},
		methods: {
			showTime,
			getExpressDelivery(item) {
				getExpressDelivery({
					code: 'yuantong',
					sn: item.delivery_sn
				}).then(res => {
					console.log(res);
					this.logProgInfo = res.data.data
					this.logProgDialogVisible = true
				})
			},
			handleOrder(tit, item) {
				this.$confirm(tit, '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					callback: action => {
						if(action == 'confirm') {
							batchOrder({
								id: item.id,
								status: item.status
							}).then(res => {
								// console.log(res);
								this.$msg.success('已提交，请等待审核处理')
							})
						}
					}
				});
			},
			sqlp(item) {

			},
			oneWork() {
				batchOrder({
					id: 0
				}).then(res => {
					this.$msg.success(res.msg)
					this.getList()
				})
			},
			handleSelectionChange(val) {
				console.log(val);
			},
			openBuy() {
				this.dialogTableVisible = true
			},
			goBuy() {
				let that = this
				if (that.loading) {
					return
				}
				if (!that.form.num) {
					return that.$msg.error('请输入购买数量')
				}
				if (!that.form.transaction) {
					return that.$msg.error('请输入交易密码')
				}
				that.loading = true
				createToken(that.form).then(res => {
					console.log(res);
					that.$msg.success('购买成功')
					that.dialogTableVisible = false
					that.loading = false
					that.getList('reload')
				}).catch(err => {
					that.loading = false
				})
			},
			getList(type) {
				let that = this
				if (that.dataloading) {
					return
				}
				if (type == 'reload') {
					that.queryInfo = {
						keyword: '',
						page: 1,
						limit: 10
					}
				}
				that.dataloading = true
				getOrderList(that.queryInfo).then(res => {
					that.total = res.data.count
					that.list = res.data.data
					that.dataloading = false
				}).catch(err => {
					that.dataloading = false
				})
			},
			handleSizeChange(newPageSize) {
				this.queryInfo.limit = newPageSize
				this.queryInfo.page = 1
				this.getList()
			},
			handleCurrentChange(newPageNum) {
				this.queryInfo.page = newPageNum
				this.getList()
			},
			qrsh() {

			}
		},

	}
</script>

<style scoped>
</style>