<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">
			<el-form-item label="订单编号" prop="orderCode">
				<el-input v-model="queryParams.orderCode" placeholder="请输入订单编号" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="商品编号" prop="goodsId">
				<el-input v-model="queryParams.goodsId" placeholder="请输入商品编号" clearable @keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item label="游戏账号" prop="gameAccount">
				<el-input v-model="queryParams.gameAccount" placeholder="请输入游戏账号" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>



		<el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<!-- <el-table-column label="订单来源" align="center" prop="orderSource" /> -->
			<el-table-column label="订单来源" align="center" prop="orderSource">
				<template slot-scope="scope">
					<div>
						<span v-if="scope.row.orderSource=='order_source_3'">H5</span>
						<span v-if="scope.row.orderSource=='order_source_2'">PC</span>
						<span v-if="scope.row.orderSource=='order_source_4'">App</span>
						<span v-if="scope.row.orderSource=='order_source_7'">线下出售</span>
					</div>
				</template>
			</el-table-column>
			<el-table-column label="订单编号" height="20px" prop="orderCode" />
			<el-table-column prop="createTime" show-overflow-tooltip align="center" height="20px" label="订单时间">
			</el-table-column>
			<el-table-column label="商品名称" show-overflow-tooltip align="center" height="20px" prop="goodsName" />
			<el-table-column label="商品编号" height="20px" prop="goodsId" />
			<el-table-column v-if="queryParams.shopId=='e9e38ddf4ea04a28bcf025458df119bd'" label="联系方式" height="20px"
				prop="userName" />
			<el-table-column label="游戏账号售价" align="center" prop="price" />
			<el-table-column label="总价" align="center" prop="totalPrice" />
			<!-- <el-table-column label="支付方式" align="center" prop="payType" /> -->
			<el-table-column label="支付方式" align="center" prop="payType">
				<template slot-scope="scope">
					<div>
						<span v-if="scope.row.payType=='pay_type_0'">微信</span>
						<span v-if="scope.row.payType=='pay_type_1'">支付宝</span>
						<span v-if="scope.row.payType=='pay_type_4'">线下</span>
					</div>
				</template>
			</el-table-column>
			<!-- <el-table-column label="交易状态" align="center" prop="transactionStatus" /> -->
			<el-table-column label="交易状态" align="center" prop="transactionStatus">
				<template slot-scope="scope">
					<div>
						<span v-if="scope.row.transactionStatus=='transaction_status_0'">待支付</span>
						<span v-if="scope.row.transactionStatus=='transaction_status_4'">支付成功</span>
						<span v-if="scope.row.transactionStatus=='transaction_status_5'">交易失败</span>
						<span v-if="scope.row.transactionStatus=='transaction_status_6'">退款中</span>
						<span v-if="scope.row.transactionStatus=='transaction_status_7'">退款成功</span>
					</div>
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改订单信息对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px">
				<el-form-item label="订单编号" prop="orderCode">
					<el-input v-model="form.orderCode" placeholder="请输入订单编号" />
				</el-form-item>
				<el-form-item label="结束时间" prop="finishTime">
					<el-date-picker clearable v-model="form.finishTime" type="date" value-format="yyyy-MM-dd"
						placeholder="请选择结束时间">
					</el-date-picker>
				</el-form-item>
				<el-form-item label="商品id" prop="goodsId">
					<el-input v-model="form.goodsId" placeholder="请输入商品id" />
				</el-form-item>
				<el-form-item label="商品名称" prop="goodsName">
					<el-input v-model="form.goodsName" placeholder="请输入商品名称" />
				</el-form-item>
				<el-form-item label="单价" prop="price">
					<el-input v-model="form.price" placeholder="请输入单价" />
				</el-form-item>
				<el-form-item label="数量" prop="num">
					<el-input v-model="form.num" placeholder="请输入数量" />
				</el-form-item>
				<el-form-item label="总价" prop="totalPrice">
					<el-input v-model="form.totalPrice" placeholder="请输入总价" />
				</el-form-item>
				<el-form-item label="买货人id" prop="byUserId">
					<el-input v-model="form.byUserId" placeholder="请输入买货人id" />
				</el-form-item>
				<el-form-item label="买货人电话" prop="byUserPhone">
					<el-input v-model="form.byUserPhone" placeholder="请输入买货人电话" />
				</el-form-item>
				<el-form-item label="订单备注">
					<editor v-model="form.content" :min-height="192" />
				</el-form-item>
				<el-form-item label="订单来源" prop="orderSource">
					<el-input v-model="form.orderSource" placeholder="请输入订单来源" />
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm">确 定</el-button>
				<el-button @click="cancel">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listOrder,
		getOrder,
		delOrder,
		addOrder,
		refund,
		sell,
		updateOrder,
		getInitGoodsManager
	} from "@/api/data/order";
	import {
		updateGoods,
	} from "@/api/data/goods";

	export default {
		name: "Order",
		dicts: ['account_source', 'is_multiple', 'by_stages', 'ranking', 'game_platform_mapping', 'identified_name',
			'account_type', 'binding_tap',
			'binding_psn',
			'account_type_wg', 'healthy_status', 'main_industry'
		],
		// dicts: ['pay_type', 'transaction_status', 'settlement_status', 'marketing_type', 'order_source', 'order_type'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 订单信息表格数据
				orderList: [],
				gameList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					orderCode: null,
					finishTime: null,
					goodsId: null,
					goodsName: null,
					price: null,
					num: null,
					totalPrice: null,
					payType: null,
					byUserId: null,
					byUserPhone: null,
					transactionStatus: null,
					content: null,
					settlementStatus: null,
					marketingType: null,
					orderSource: null,
					orderType: null,
					// goodsStatus: 'goods_status_3',
					goodsStatus:null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {}
			};
		},
		created() {
			this.form.shopId = this.$route.query.shopId
			this.queryParams.shopId = this.$route.query.shopId
			this.getList();
			this.initGoodsManager();
		},
		methods: {
			initGoodsManager() {
				getInitGoodsManager().then(res => {
					this.pictureServerPath = res.pictureServerPath
					this.gameList = res.gameList
				})
			},
			/** 查询订单信息列表 */
			getList() {
				this.loading = true;
				listOrder(this.queryParams).then(response => {
					this.orderList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					orderCode: null,
					createTime: null,
					finishTime: null,
					goodsId: null,
					goodsName: null,
					price: null,
					num: null,
					totalPrice: null,
					payType: null,
					byUserId: null,
					byUserPhone: null,
					transactionStatus: "0",
					content: null,
					settlementStatus: "0",
					marketingType: null,
					orderSource: null,
					orderType: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加订单信息";
			},
			/** 修改按钮操作 */
			handleUpdate(row) {
				this.reset();
				const id = row.id || this.ids
				getOrder(id).then(response => {
					this.form = response.data;
					this.open = true;
					this.title = "修改订单信息";
				});
			},
			/** 提交按钮 */
			submitForm() {
				this.$refs["form"].validate(valid => {
					if (valid) {
						if (this.form.id != null) {
							updateOrder(this.form).then(response => {
								this.$modal.msgSuccess("修改成功");
								this.open = false;
								this.getList();
							});
						} else {
							addOrder(this.form).then(response => {
								this.$modal.msgSuccess("新增成功");
								this.open = false;
								this.getList();
							});
						}
					}
				});
			},
			//出售按钮
			handleSend(row) {
					this.$modal.confirm('此商品[' + row.goodsId + ']确定是否已经出售？').then(function() {}).then(() => {
						let query = {
							orderCode:row.orderCode,
							id: row.goodsId,
						}
						sell(query).then(res => {
							this.$modal.msgSuccess("出售成功");
							this.getList();
						})
					
					}).catch(() => {})
			},
			// handleSendtk(row){
			// 	// let id = Cookies.get('id')
			// 	let query = {
			// 		id: row.goodsId,
			// 		orderCode:row.orderCode,
			// 		remark: '不想要了!'
			// 	}
			// 	refund(query).then(res => {
			// 		// console.log(res)
			// 		this.$message({
			// 			type: 'success',
			// 			message: '退款成功!'
			// 		})
			// 		this.getList()
			// 	})
			// },
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.goodsId || this.ids;
				this.$modal.confirm('是否确认删除订单信息编号为"' + ids + '"的数据项？').then(function() {
					return delOrder(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('system/order/export', {
					...this.queryParams
				}, `order_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>
