<template>
	<!-- 营销-积分商品 -->
	<div>
		<Card :bordered="false" dis-hover class="ivu-mt" :padding="0">
			<div class="new_card_pd">
				<!-- 查询条件 -->
				<Form ref="tableFrom" inline :model="tableFrom" :label-width="labelWidth"
					:label-position="labelPosition" @submit.native.prevent>
					<FormItem label="用户搜索：" label-for="store_name">
						<Input class="input-add mr14" placeholder="请输入用户昵称/手机号" v-model="tableFrom.key" />
						<Button type="primary" @click="tableSearchs()">查询</Button>
					</FormItem>
				</Form>
			</div>
		</Card>
		<Card :bordered="false" dis-hover class="ivu-mt">
			<!-- 操作 -->
			<Button v-auth="['admin-erp-user_add']" type="primary" @click="add" class="mr10">添加用户</Button>
			<!-- 积分商品-表格 -->
			<Table :columns="columns1" :data="tableList" :loading="loading" highlight-row no-userFrom-text="暂无数据"
				no-filtered-userFrom-text="暂无筛选结果" class="ivu-mt">
				<template slot-scope="{ row, index }" slot="image">
					<viewer>
						<div class="tabBox_img">
							<img v-lazy="row.avatar" />
						</div>
					</viewer>
				</template>
				<template slot-scope="{ row, index }" slot="add_time">
					<span> {{ row.add_time | formatDate }}</span>
				</template>
				<template slot-scope="{ row, index }" slot="is_system">
					<span> {{ row.is_system ? '是': '否'}}</span>
				</template>
				<template slot-scope="{ row, index }" slot="is_manager">
					<span> {{ row.is_manager ? '是': '否'}}</span>
				</template>
				<template slot-scope="{ row, index }" slot="ware">
					<span> {{row.ware && row.ware.id ? row.ware.title: ''}}</span>
				</template>
				<template slot-scope="{ row, index }" slot="store">
					<span> {{row.store && row.store.id ? row.store.name: ''}}</span>
				</template>
				<template slot-scope="{ row, index }" slot="is_show">
					<i-switch v-model="row.is_show" :value="row.is_show" :true-value="1" :false-value="0"
						@on-change="onchangeIsShow(row)" size="large">
						<span slot="open">上架</span>
						<span slot="close">下架</span>
					</i-switch>
				</template>
				<template slot-scope="{ row, index }" slot="action">
					<a @click="look(row)">详情</a>
					<Divider type="vertical" />
					<a @click="edit(row)">编辑</a>
				</template>
			</Table>
			<div class="acea-row row-right page">
				<Page :total="total" :current="tableFrom.page" show-elevator show-total @on-change="pageChange"
					:page-size="tableFrom.limit" />
			</div>
		</Card>
		<userDetails ref="userDetails"></userDetails>
	</div>
</template>

<script>
	import userDetails from './userDetails.vue'
	import {
		mapState
	} from "vuex";
	import {
		integralProductListApi,
		integralIsShowApi,
		storeSeckillApi,
	} from "@/api/marketing";
	import {
		getUserList,
		getUserInfo
	} from "@/api/erp";
	import {
		formatDate
	} from "@/utils/validate";
	import timeOptions from "@/utils/timeOptions";
	import Setting from "@/setting";
	export default {
		name: "erpUser",
		components: {
			userDetails
		},
		filters: {
			formatDate(time) {
				if (time !== 0) {
					let date = new Date(time * 1000);
					return formatDate(date, "yyyy-MM-dd");
				}
			},
		},
		data() {
			return {
				roterPre: Setting.roterPre,
				loading: false,
				options: timeOptions,
				columns1: [{
						title: "ID",
						key: "id",
						width: 80,
					},
					{
						title: "账号",
						key: "account",
						minWidth: 90,
					},
					{
						title: "用户头像",
						slot: "image",
						minWidth: 90,
					},
					{
						title: "ERP用户名称",
						key: "staff_name",
						minWidth: 90,
					},
					{
						title: "手机号",
						key: "phone",
						minWidth: 90,
					},
					{
						title: "所属仓库",
						slot: "ware",
						minWidth: 80,
					},
					{
						title: "所属门店",
						slot: "store",
						minWidth: 80,
					},
					{
						title: "是否系统用户",
						slot: "is_system",
						minWidth: 80,
					},
					{
						title: "是否管理",
						slot: "is_manager",
						minWidth: 80,
					},
					// {
					// 	title: '仓库id',
					// 	key: 'wid',
					// 	minWidth: 80,
					// },
					// {
					// 	title: '客户id',
					// 	key: 'staff_id',
					// 	minWidth: 80,
					// },
					{
						title: "创建时间",
						slot: "add_time",
						minWidth: 130,
					},
					// {
					// 	title: "状态",
					// 	key: "status",
					// 	minWidth: 100,
					// },
					{
						title: "操作",
						slot: "action",
						fixed: "right",
						width: 200,
					},
				],
				tableList: [],
				timeVal: [],
				grid: {
					xl: 7,
					lg: 10,
					md: 12,
					sm: 24,
					xs: 24,
				},
				tableFrom: {
					// integral_time: "",
					// is_show: "",
					// store_name: "",
					key: '',
					page: 1,
					limit: 15,
				},
				total: 0,
			};
		},
		computed: {
			...mapState("admin/layout", ["isMobile"]),
			labelWidth() {
				return this.isMobile ? undefined : 96;
			},
			labelPosition() {
				return this.isMobile ? "top" : "right";
			},
		},
		created() {
			this.getList();
		},
		methods: {
			look(row) {
				// this.$refs.userDetails.modals = true;
				this.$refs.userDetails.getUserInfo(row.id);
			},
			// 添加
			add() {
				this.$router.push({
					path: this.roterPre + "/erp/user_add?id=0"
				});
			},
			orderList(row) {
				this.$router.push({
					path: this.roterPre + "/marketing/store_integral/order_list",
					query: {
						product_id: row.id,
					},
				});
			},
			// 导出
			exports() {
				let formValidate = this.tableFrom;
				let data = {
					start_status: formValidate.start_status,
					status: formValidate.status,
					store_name: formValidate.store_name,
				};
				storeSeckillApi(data)
					.then((res) => {
						location.href = res.data[0];
					})
					.catch((res) => {
						this.$Message.error(res.msg);
					});
			},
			// 编辑
			edit(row) {
				this.$router.push({
					path: this.roterPre + "/erp/user_add?id=" + row.id,
				});
			},
			// 一键复制
			copy(row) {
				this.$router.push({
					path: this.roterPre + "/marketing/store_integral/create/" + row.id + "/1",
				});
			},
			// 删除
			del(row, tit, num) {
				let delfromData = {
					title: tit,
					num: num,
					url: `marketing/integral/${row.id}`,
					method: "DELETE",
					ids: "",
				};
				this.$modalSure(delfromData)
					.then((res) => {
						this.$Message.success(res.msg);
						this.tableList.splice(num, 1);
						if (!this.tableList.length) {
							this.tableFrom.page =
								this.tableFrom.page == 1 ? 1 : this.tableFrom.page - 1;
						}
						this.getList();
					})
					.catch((res) => {
						this.$Message.error(res.msg);
					});
			},
			// 列表
			getList() {
				this.loading = true;
				// this.tableFrom.start_status = this.tableFrom.start_status || "";
				// this.tableFrom.is_show = this.tableFrom.is_show || "";
				getUserList(this.tableFrom)
					.then(async (res) => {
						let data = res.data;
						this.tableList = data.data;
						this.total = data.count;
						this.loading = false;
					})
					.catch((res) => {
						this.loading = false;
						this.$Message.error(res.msg);
					});
			},
			pageChange(index) {
				this.tableFrom.page = index;
				this.getList();
			},
			// 表格搜索
			tableSearchs() {
				this.tableFrom.page = 1;
				this.getList();
			},
			// 具体日期
			onchangeTime(e) {
				this.timeVal = e;
				this.tableFrom.integral_time = this.timeVal[0] ? this.timeVal.join("-") : "";
			},
			// 修改是否显示
			onchangeIsShow(row) {
				let data = {
					id: row.id,
					is_show: row.is_show,
				};
				integralIsShowApi(data)
					.then(async (res) => {
						this.$Message.success(res.msg);
					})
					.catch((res) => {
						this.$Message.error(res.msg);
					});
			},
		},
	};
</script>

<style scoped lang="stylus">
	.tabBox_img {
		width: 36px;
		height: 36px;
		border-radius: 4px;
		cursor: pointer;

		img {
			width: 100%;
			height: 100%;
		}
	}
</style>