<template>
	<!-- 订单-售后订单 -->
	<div>
		<Card :bordered="false" dis-hover class="ivu-mt" :padding="0">
			<div class="new_card_pd">
				<!-- 筛选条件 -->
				<Form ref="pagination" inline :model="pagination" :label-width="labelWidth"
					:label-position="labelPosition" @submit.native.prevent>

					<FormItem label="选择员工：">
						<Select filterable clearable v-model="pagination.member_id" class="input-add">
							<Option v-for="(item, index) in ygList" :value="item.id" :key="index">{{ item.name }}(ID:{{item.uid}})
							</Option>
						</Select>
					</FormItem>
					<FormItem label="时间范围：">
						<DatePicker :editable="false" @on-change="onchangeTime" :value="timeVal" format="yyyy/MM/dd"
							type="daterange" placement="bottom-start" placeholder="自定义时间" class="input-add"
							:options="options"></DatePicker>
					</FormItem>
					<FormItem label="业绩类型:">
						<Select v-model="pagination.type" class="input-add">
							<Option v-for="(item, index) in num" :value="item.value" :key="index">{{ item.name }}
							</Option>
						</Select>
						<Button type="primary" @click="orderSearch()">查询</Button>
					</FormItem>
					<!-- <FormItem label="订单搜索：" label-for="title">
						<Input v-model="pagination.order_id" placeholder="请输入订单号" class="input-add mr14" />
						
					</FormItem> -->
				</Form>
			</div>
		</Card>

		<cards-data :cardLists="cardLists"></cards-data>
		<Card :bordered="false" dis-hover class="ivu-mt">
			<!-- 售后订单表格 -->
			<Table :columns="thead" :data="tbody" ref="table" :loading="loading" highlight-row no-userFrom-text="暂无数据"
				no-filtered-userFrom-text="暂无筛选结果">
				<template slot-scope="{ row }" slot="order_id">
					<span v-text="row.order_id" style="display: block"></span>
					<span v-show="row.is_del === 1 && row.delete_time == null" class="span-del">用户已删除</span>
				</template>
				<template slot-scope="{ row }" slot="nickname">
					<div>用户名：{{ row.real_name }}</div>
					<div>手机号：{{ row.user_phone }}</div>
				</template>
				<template slot-scope="{ row, index }" slot="kf">
					<span class="tabBox_pice">{{showName(row.work_member_id)}}</span>
				</template>
				<template slot-scope="{ row }" slot="info">
					<div class="tabBox" v-for="(val, i) in row.info" :key="i">
						<div class="tabBox_img" v-viewer>
							<img v-lazy="val.slider_image" />
						</div>
						<span class="tabBox_tit">{{ val.store_name }}</span>
						<span class="tabBox_pice">{{'￥' + val.pay_price}}</span>
					</div>
				</template>
				<template slot-scope="{ row, index }" slot="pay_price">
					<span class="tabBox_pice">{{'￥' + showPrice(row.info)}}</span>
				</template>
				<template slot-scope="{ row, index }" slot="reservation_time">
					<span class="tabBox_pice">{{showTime(row.reservation_time)}}</span>
				</template>
				<template slot-scope="{ row, index }" slot="store">
					<span class="tabBox_pice">{{row.store.name}}</span>
				</template>
				<template slot-scope="{ row }" slot="type">
					<span v-if="row.type == 1">销售业绩</span>
					<span v-if="row.type == 2">手工业绩</span>
				</template>
				<template slot-scope="{ row, index }" slot="add_time">
					<span class="tabBox_pice">{{showTime(row.add_time)}}</span>
				</template>
			</Table>
			<div class="acea-row row-right page">
				<Page :total="total" :current="pagination.page" show-elevator show-total @on-change="pageChange"
					:page-size="pagination.limit" />
			</div>
		</Card>
		<Modal v-model="modals" scrollable title="选择员工" class="order_box" :closable="false">
			<Form :label-width="80">
				<FormItem label="员工：" prop="chooseId">
					<Select v-model="chooseId" style="width: 250px">
						<Option v-for="item in ygList" :key="item.id" :value="item.id">{{ item.name }}</Option>
					</Select>
				</FormItem>
			</Form>
			<div slot="footer">
				<Button type="primary" @click="goCancal">提交</Button>
				<Button @click="cancal">取消</Button>
			</div>
		</Modal>
	</div>
</template>

<script>
	import {
		mapState
	} from "vuex";
	import {
		member_update
	} from "@/api/order";
	import {
		getYgList
	} from "@/api/store"
	import {
		getKpi
	} from "@/api/agent"
	import timeOptions from "@/utils/timeOptions";
	import cardsData from "@/components/cards/cards";
	export default {
		components: {
			cardsData
		},
		data() {
			return {
				cardLists: [],
				modals: false,
				order_id: '',
				ygList: [],
				chooseId: 0,
				openErp: false,
				thead: [{
						title: "id",
						align: "id",
						key: "id",
						minWidth: 50,
					}, {
						title: "服务订单号",
						align: "order_id",
						key: "order_id",
						minWidth: 150,
					},
					{
						title: "员工",
						align: "name",
						key: "name",
						minWidth: 80,
					},
					{
						title: "员工手机",
						key: "mobile",
						minWidth: 100,
					},
					{
						title: "业绩值变动",
						key: "performance",
						minWidth: 70,
					},

					{
						title: "业绩值分类",
						slot: "type",
						minWidth: 70,
					},
					{
						title: "详情",
						key: "content",
						minWidth: 150,
					},
					{
						title: "业绩变动后",
						key: "balance",
						minWidth: 70,
					},
					{
						title: "更新时间",
						key: "create_time",
						minWidth: 130,
					},
				],
				tbody: [],
				num: [{
						name: '全部',
						value: 0
					}, {
						name: '销售业绩',
						value: 1
					},
					{
						name: '手工业绩',
						value: 2
					},
				],
				orderDatalist: null,
				loading: false,
				FromData: null,
				total: 0,
				orderId: 0,
				animal: 1,
				pagination: {
					type: 0,
					member_id: 0,
					page: 1,
					limit: 15,
					order_id: "",
					create_time: "",
					status: 0
				},
				options: timeOptions,
				timeVal: [],
				modal: false,
				qrcode: null,
				name: "",
				spin: false,
				rowActive: {},
			};
		},
		computed: {
			...mapState("order", ["orderChartType"]),
			// ...mapState("admin/layout", ["isMobile"]),
			labelWidth() {
				return this.isMobile ? undefined : 96;
			},
			labelPosition() {
				return this.isMobile ? "top" : "right";
			},
		},
		created() {
			this.getYgList()
			this.getOrderList();
		},
		methods: {
			showName(id) {
				let yg = this.ygList.find(item => {
					return item.id == id
				})
				return yg ? yg.name : ''
			},
			getYgList() {
				getYgList({
					page: 1,
					limit: 1000
				}).then(res => {
					this.ygList = res.data.list
				})
			},
			showTime(time) {
				var date = new Date(time * 1000);
				var year = date.getFullYear(); // 获取年份
				var month = ("0" + (date.getMonth() + 1)).slice(-2); // 获取月份（注意月份从 0 开始，需要加 1）
				var day = ("0" + date.getDate()).slice(-2); // 获取日期
				var hours = ("0" + date.getHours()).slice(-2); // 获取小时
				var minutes = ("0" + date.getMinutes()).slice(-2); // 获取分钟
				var seconds = ("0" + date.getSeconds()).slice(-2); // 获取秒钟
				var dateString = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds; // 自定义时间格式
				return dateString
			},
			showPrice(list) {
				let price = 0;
				list.forEach(item => {
					price += item.pay_price * 1
				})
				return price.toFixed(2)
			},
			// 具体日期搜索()；
			onchangeTime(e) {
				console.log(e, 'eeeeeee')
				this.pagination.page = 1;
				this.timeVal = e;
				this.pagination.create_time = this.timeVal[0] ? this.timeVal.join("-") : "";
			},
			cancal() {
				this.modals = false
				this.remark = ''
			},
			//goCancal
			goCancal() {
				if (this.chooseId == '') {
					return this.$Message.error('请选择员工')
				}
				this.modals = false
				member_update({
					id: this.order_id,
					work_member_id: this.chooseId
				}).then(res => {
					this.$Message.success(res.msg)
					this.getOrderList()
				}).catch(err => {
					this.$Message.error(err.msg)
				})

			},
			// 订单列表
			getOrderList() {
				this.loading = true;
				getKpi(this.pagination)
					.then((res) => {
						this.loading = false;
						const {
							count,
							list,
							num
						} = res.data;
						this.total = count;
						this.tbody = list;
						this.cardLists = [{
							col: 6,
							count: res.data.sale_sum,
							name: "销售总业绩",
							className: "md-basket",
						},
						{
							col: 6,
							count: res.data.craft_sum,
							name: "手工总业绩",
							className: "md-pricetags",
						}];
						// this.num = num;
						list.forEach((item) => {
							if (item.id == this.orderId) {
								this.rowActive = item;
							}
						});
					})
					.catch((err) => {
						this.loading = false;
						this.$Message.error(err.msg);
					});
			},
			// 分页
			pageChange(index) {
				this.pagination.page = index;
				this.getOrderList();
			},
			// 订单搜索
			orderSearch() {
				this.pagination.page = 1;
				this.getOrderList();
			},
		},
	};
</script>

<style lang="stylus" scoped>
	.span-del {
		color: #ed4014;
		display: block
	}

	.code {
		position: relative;
	}

	.QRpic {
		width: 180px;
		height: 259px;

		img {
			width: 100%;
			height: 100%;
		}
	}

	.tabBox {
		width: 100%;
		height: 100%;
		display: flex;
		align-items: center;

		.tabBox_img {
			width: 30px;
			height: 30px;

			img {
				width: 100%;
				height: 100%;
			}
		}

		.tabBox_tit {
			width: 245px;
			height: 30px;
			line-height: 30px;
			font-size: 12px !important;
			margin: 0 2px 0 10px;
			letter-spacing: 1px;
			box-sizing: border-box;
		}
	}

	.tabBox+.tabBox {
		margin-top: 5px;
	}

	.pictrue-box {
		display: flex;
		align-item: center;
	}

	.pictrue {
		width: 25px;
		height: 25px;
	}
</style>