<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="user_time">
            <DatePicker
                :editable="false"
                @on-change="onchangeTime"
                :value="timeVal"
                format="yyyy/MM/dd"
                type="datetimerange"
                placement="bottom-start"
                placeholder="自定义时间"
                class="mr20 input-add"
                :options="options"
            ></DatePicker>
          </FormItem> -->
					<!-- <FormItem label="上架状态：">
            <Select
                placeholder="请选择"
                clearable
                class="input-add"
                v-model="tableFrom.is_show"
            >
              <Option value="1">上架</Option>
              <Option value="0">下架</Option>
            </Select>
          </FormItem> -->
					<FormItem label="仓库：" prop="pid" label-for="pid">
						<Select v-model="tableFrom.wid" @on-change="userSearchss" clearable class="input-add">
							<Option v-for="item in data1" :value="item.id" :key="item.id">
								{{ item.title }}
							</Option>
						</Select>
					</FormItem>
					<FormItem label="审核人：" v-if="tableFrom.wid">
						<Select v-model="tableFrom.auth_uid" clearable filterable @on-change="userSearchs"
							class="input-add">
							<Option v-for="item in authList" :value="item.id" :key="item.id">{{ item.staff_name }}
							</Option>
						</Select>
					</FormItem>
					<FormItem label="供应商：" prop="pid" label-for="pid">
						<Select v-model="tableFrom.supplier_id" @on-change="userSearchs" clearable class="input-add">
							<Option v-for="item in supplierList" :value="item.id" :key="item.id">
								{{ item.supplier_name }}
							</Option>
						</Select>
					</FormItem>
					<FormItem label="门店：">
						<Select v-model="tableFrom.store_id" clearable filterable @on-change="userSearchs"
							class="input-add">
							<Option v-for="item in staffData" :value="item.id" :key="item.id">{{ item.name }}
							</Option>
						</Select>
					</FormItem>
					<FormItem label="状态：" label-for="status">
						<Select v-model="tableFrom.status" placeholder="请选择" clearable @on-change="userSearchs"
							class="input-add">
							<Option value="-3">全部</Option>
							<Option value="0">待审核</Option>
							<Option value="1">通过</Option>
							<Option value="2">部分完成</Option>
							<Option value="3">完成</Option>
							<Option value="-1">拒绝</Option>
						</Select>
					</FormItem>
					<FormItem label="关键字搜索：" label-for="key">
						<Input class="input-add mr14" placeholder="请输入要货单或采购单" v-model="tableFrom.key" />
						<Button type="primary" @click="tableSearchs()">查询</Button>
					</FormItem>
					<!-- <Button
            v-auth="['marketing-store_seckill-create']"
            type="primary"
            icon="md-add"
            @click="addMore"
            class="mr10"
            >批量添加积分商品</Button
          > -->
					<!--<Button v-auth="['export-storeSeckill']" class="export" icon="ios-share-outline" @click="exports">导出</Button>-->
				</Form>
			</div>
		</Card>
		<Card :bordered="false" dis-hover class="ivu-mt">
			<!-- 操作 -->
			<Button v-auth="['admin-erp-add_position']" type="primary" @click="add" class="mr10">添加采购单</Button>
			<Button v-auth="['admin-erp-printing']" type="primary" @click="printing('purchase')"
				class="mr10">打印采购单</Button>
			<!-- 积分商品-表格 -->
			<Table :columns="columns1" :data="tableList" :loading="loading" highlight-row no-userFrom-text="暂无数据"
				no-filtered-userFrom-text="暂无筛选结果" class="ivu-mt" @on-selection-change="checkItem">
				<template slot-scope="{ row, index }" slot="auser">
					<div v-if="row.auser">{{row.auser.staff_name}}</div>
					<div v-if="row.auth_time">{{row.auth_time |formatDate }}</div>
					<!-- <div>名称：{{row.auser.staff_name}}</div>
					<div>管理员ID：{{row.auser.admin_id}}</div>
					<div>ID:{{row.auser.id}}</div>
					<div>手机：{{row.auser.phone}}</div> -->
				</template>
				<template slot-scope="{ row, index }" slot="supplier">
					<div>{{row.supplier.supplier_name}}</div>
					<!-- <div>名称：{{row.supplier.supplier_name}}</div>
					<div>ID:{{row.supplier.id}}</div>
					<div>手机：{{row.supplier.phone}}</div> -->
				</template>
				<template slot-scope="{ row, index }" slot="store">
					<div>{{row.store.name}}</div>
					<!-- <div>名称：{{row.store.name}}</div>
					<div>ID:{{row.store.id}}</div>
					<div>手机：{{row.store.phone}}</div> -->
				</template>
				<template slot-scope="{ row, index }" slot="cuser">
					<div>{{row.cuser.staff_name}}</div>
					<div>{{row.create_time |formatDate }}</div>
					<!-- <div>名称：{{row.cuser.staff_name}}</div>
					<div>管理员ID：{{row.cuser.admin_id}}</div>
					<div>ID:{{row.cuser.id}}</div>
					<div>手机：{{row.cuser.phone}}</div> -->
				</template>
				<template slot-scope="{ row, index }" slot="ware">
					<div>{{row.ware.title}}</div>
					<!-- <div>名称：{{row.ware.title}}</div>
					<div>ID:{{row.ware.id}}</div>
					<div>手机：{{row.ware.phone}}</div>
					<div>地址：{{row.ware.detailed_address}}</div> -->
				</template>
				<template slot-scope="{ row, index }" slot="puser">
					<div>{{row.puser.staff_name}}</div>
					<!-- <div>名称：{{row.ware.title}}</div>
					<div>ID:{{row.ware.id}}</div>
					<div>手机：{{row.ware.phone}}</div>
					<div>地址：{{row.ware.detailed_address}}</div> -->
				</template>
				<template slot-scope="{ row, index }" slot="status">
					<Tag color="default" v-if="row.status == 0">待审核</Tag>
					<Tag color="primary" v-if="row.status == 1">通过</Tag>
					<Tag color="success" v-if="row.status == 3">完成</Tag>
					<Tag color="error" v-if="row.status == -1">拒绝</Tag>
					<Tag color="warning" v-if="row.status == 2">部分完成</Tag>
				</template>
				<template slot-scope="{ row, index }" slot="action">
					<a @click="look(row)">详情</a>
					<!-- 				<Divider type="vertical" v-if="row.status == 0" />
					<a @click="edit(row)" v-if="row.status == 0">编辑</a> -->
					<Divider type="vertical" v-if="row.status == 0" />
					<a @click="pass(row)" v-if="row.status == 0">审核</a>
					<Divider type="vertical" v-if="row.status == 1" />
					<a @click="end(row)" v-if="row.status == 1">完成</a>
					<Divider type="vertical" v-if="row.status <1" />
					<a @click="del(row, '删除采购单', index)" v-if="row.status <1">删除</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>
		<Modal v-model="modalpass" scrollable title="请修改内容" class="order_box" :closable="false" :mask-closable="false">
			<Form ref="remarks" :model="remarks" :label-width="80" @submit.native.prevent>
				<FormItem label="采购人：">
					<Select v-model="remarks.purchasing_person_uid" clearable filterable @on-change="userSearchses"
						class="input-add">
						<Option v-for="item in authLists" :value="item.id" :key="item.id">{{ item.staff_name }}
						</Option>
					</Select>
				</FormItem>
				<FormItem label="审核人：">
					<Select v-model="remarks.auth_uid" clearable filterable @on-change="userSearchse" class="input-add">
						<Option v-for="item in authLists" :value="item.id" :key="item.id">{{ item.staff_name }}
						</Option>
					</Select>
				</FormItem>
				<FormItem label="状态：">
					<Select v-model="remarks.status" clearable filterable class="input-add">
						<Option :value="1">通过</Option>
						<Option :value="-1">拒绝</Option>
						<!-- <Option :value="2" >部分完成</Option>
				<Option :value="3" >完成</Option> -->
					</Select>
				</FormItem>
			</Form>
			<div slot="footer">
				<Button type="primary" @click="putRemark()">提交</Button>
				<Button @click="cancel()">取消</Button>
			</div>
		</Modal>
		<purchaseDetail ref="purchaseDetail"></purchaseDetail>
		<Modal v-model="modalend" scrollable title="完成采购单" class="order_box" :closable="false" :mask-closable="false"
			width="1090">
			<Form ref="endobj" :model="endobj" :label-width="80" @submit.native.prevent>
				<FormItem label="审核人：">
					<Select v-model="endobj.purchasing_person_uid" clearable filterable @on-change="userSearchsess"
						class="input-add">
						<Option v-for="item in authLists" :value="item.id" :key="item.id">{{ item.staff_name }}
						</Option>
					</Select>
				</FormItem>
			</Form>
			<Table :columns="endobj.columns" :data="list" :loading="endobj.loading" highlight-row
				no-userFrom-text="暂无数据" no-filtered-userFrom-text="暂无筛选结果" class="ivu-mt">
				<template slot-scope="{ row, index }" slot="pur_num">
					<Input v-model="list[index].pur_num" placeholder="请输入采购数量" v-width="'120'" type="number" />
				</template>
				<template slot-scope="{ row, index }" slot="total_price">
					<Input v-model="list[index].total_price" placeholder="请输入净重" v-width="'120'" type="number" />
				</template>
				<template slot-scope="{ row, index }" slot="net_weight">
					<Input v-model="list[index].net_weight" placeholder="请输入净重" v-width="'120'" type="number" />
				</template>
				<template slot-scope="{ row, index }" slot="tare_weight">
					<Input v-model="list[index].tare_weight" placeholder="请输入净重" v-width="'120'" type="number" />
				</template>

			</Table>
			<div slot="footer">
				<Button type="primary" @click="putRemark1()">提交</Button>
				<Button @click="cancel1()">取消</Button>
			</div>
		</Modal>
	</div>
</template>

<script>
	import purchaseDetail from './purchaseDetail.vue'
	import {
		mapState
	} from "vuex";
	import {
		integralProductListApi,
		integralIsShowApi,
		storeSeckillApi,
	} from "@/api/marketing";
	import {
		getPositionList,
		getUserInfo,
		getPurchaselist,
		getGodownList,
		getUserList,
		sevePurchase,
		savePurchase,
		getPurchaseDetail
	} from "@/api/erp";
	import {
		formatDate
	} from "@/utils/validate";
	import timeOptions from "@/utils/timeOptions";
	import Setting from "@/setting";
	import {
		getSupplierList
	} from "@/api/supplier";
	import {
		staffListInfo
	} from "@/api/store";
	import {
		toJSONSchema
	} from 'mockjs';
	export default {
		name: "purchaseList",
		components: {
			purchaseDetail
		},
		filters: {
			formatDate(time) {
				if (time !== 0) {
					let date = new Date(time * 1000);
					return formatDate(date, "yyyy-MM-dd");
				}
			},
		},
		data() {
			return {
				list: [],
				endobj: {
					id: 0,
					purchasing_person_admin_id: "",
					purchasing_person_uid: "",
					loading: false,
					columns: [{
							title: "ID",
							key: "id",
							width: 80,
						},
						{
							title: "商品名称",
							key: "product_name",
							width: 200,
						},
						{
							title: "预采购数量",
							key: "product_num",
							width: 90,
						},
						{
							title: "单位",
							key: "unit_name",
							width: 80,
						},
						{
							title: "实际采购数量",
							slot: "pur_num",
							width: 150,
						},
						{
							title: "总价格",
							slot: "total_price",
							width: 150,
						},

						{
							title: "净重",
							slot: "net_weight",
							width: 150,
						},
						{
							title: "皮重",
							slot: "tare_weight",
							width: 150,
						},
					]
				},
				modalend: false,
				modalpass: false,
				remarks: {
					id: 0,
					status: '',
					auth_uid: "",
					auth_admin_id: "",
					purchasing_person_admin_id: "",
					purchasing_person_uid: ""
				},
				authLists: [],
				data1: [],
				staffData: [],
				supplierList: [],
				roterPre: Setting.roterPre,
				loading: false,
				options: timeOptions,
				columns1: [
					{
							type: 'selection',
							width: 60,
							align: 'center'
						}, {
						title: "ID",
						key: "id",
						width: 80,
					},

					{
						title: "采购单号",
						key: "purchase_order_id",
						minWidth: 200,
					},
					{
						title: "要货单号",
						key: "want_order_id",
						minWidth: 200,
					},
					{
						title: "采购仓库",
						slot: "ware",
						minWidth: 200,
					},
					{
						title: "采购门店",
						slot: "store",
						minWidth: 200,
					},
					{
						title: "供应商",
						slot: "supplier",
						minWidth: 200,
					},
					{
						title: "订单数量",
						key: "order_num",
						minWidth: 90,
					},
					{
						title: "订单金额",
						key: "order_price",
						minWidth: 90,
					},
					{
						title: "运费",
						key: "freight",
						minWidth: 90,
					},
					{
						title: "创建人",
						slot: "cuser",
						minWidth: 200,
					},
					{
						title: "审核人",
						slot: "auser",
						minWidth: 200,
					},
					{
						title: "采购人",
						slot: "puser",
						minWidth: 200,
					},
					{
						title: "审核状态",
						slot: "status",
						minWidth: 200,
					},
					{
						title: "操作",
						slot: "action",
						fixed: "right",
						width: 200,
					},
				],
				tableList: [],
				timeVal: [],
				grid: {
					xl: 7,
					lg: 10,
					md: 12,
					sm: 24,
					xs: 24,
				},
				tableFrom: {
					supplier_id: "",
					status: '-3',
					auth_uid: "",
					// integral_time: "",
					// is_show: "",
					// store_name: "",
					key: '',
					page: 1,
					limit: 10,
				},
				total: 0,
				checKList: []
			};
		},
		computed: {
			...mapState("admin/layout", ["isMobile"]),
			labelWidth() {
				return this.isMobile ? undefined : 96;
			},
			labelPosition() {
				return this.isMobile ? "top" : "right";
			},
		},
		created() {
			this.getList();

		},
		mounted() {
			this.getSupplierList()
			this.staffList()
			this.getGodownList()
		},
		methods: {
			printing(name) {
				if (this.checKList.length == 0) {
					return this.$Message.error('请选择要打印的要货单')
				}
				let id = this.checKList.map(e => {
					return e.id
				})
				let routeData = this.$router.resolve(({
					path: this.roterPre + '/erp/printing',
					query: {
						type: name,
						id: id.join(','),
					}
				}))
				window.open(routeData.href, '_blank')

			},
			checkItem(e) {
				this.checKList = e
			},
			end(row) {
				// console.log(row);
				this.endobj.id = row.id
				this.endobj.purchase_order_id = row.purchase_order_id
				getPurchaseDetail(row.purchase_order_id).then(res => {
					this.list = res.data.info.map(item => {
						let obj = {}
						obj.is_weigh = item.is_weigh
						obj.net_weight = item.net_weight
						obj.product_id = item.product_id;
						obj.pur_num = item.pur_num ? item.pur_num * 1 : 0
						obj.rate = item.unit_type == 1 ? item.p_unit_rate_1 : item.unit_type == 2 ? item
							.p_unit_rate_2 : 1
						obj.shelf_life = item.shelf_list
						obj.tare_weight = item.tare_weight
						obj.total_price = item.total_price
						obj.id = item.id
						obj.product_name = item.product_name
						obj.product_num = item.product_num
						obj.unit_name = item.unit_name
						obj.shelf_life = item.shelf_life
						console.log(obj);
						return obj
					})
					if (row.wid) {
						getUserList({
							wid: row.wid
						}).then(res => {
							this.authLists = res.data.data
						})
					} else {
						getUserList({
							store_id: row.store_id
						}).then(res => {
							this.authLists = res.data.data
						})
					}

					this.modalend = true
				})

			},
			putRemark() {
				sevePurchase(this.remarks.id, this.remarks).then(res => {
					this.cancel()
					this.getList();
				})
			},
			putRemark1() {
				// console.log(this.list);
				let arr = this.list.map(item => {
					if (item.is_weigh == 0) {
						item.pur_weigh = item.pur_num * 1 * item.rate
					} else {
						item.pur_weigh = item.tare_weight * 1 + item.net_weight * 1
					}
					return item
				})
				console.log('arr', arr);
				savePurchase(this.endobj.id, {
					info: arr,
					purchasing_person_admin_id: this.endobj.purchasing_person_admin_id,
					purchasing_person_uid: this.endobj.purchasing_person_uid,
					is_system: 0,
				}).then(res => {
					this.modalend = false
					this.getList()
					this.cancel1()
					this.$Message.success(res.msg);
				}).catch(err => {
					this.$Message.error(err.msg);
				})
			},
			look(row) {
				this.$refs.purchaseDetail.getInfo(row.id);
			},
			cancel() {
				this.remarks = {
					id: 0,
					status: '',
					auth_uid: "",
					auth_admin_id: "",
				}
				this.modalpass = false
			},
			cancel1() {
				this.modalend = false
				this.endobj.id = 0
				this.endobj.purchasing_person_admin_id = ""
				this.endobj.purchasing_person_uid = ""
				this.list = []
			},
			userSearchse(e) {
				let auth = this.authLists.find(item => item.id == e)
				this.remarks.auth_admin_id = auth.admin_id
			},
			userSearchses(e) {
				let auth = this.authLists.find(item => item.id == e)
				this.remarks.purchasing_person_admin_id = auth.admin_id
			},
			userSearchsess(e) {
				let auth = this.authLists.find(item => item.id == e)
				this.endobj.purchasing_person_admin_id = auth.admin_id
			},
			pass(row) {
				let qdata = {}
				if (row.wid) {
					qdata.wid = row.wid
				} else {
					qdata.store_id = row.store_id
				}
				this.authLists = []
				this.remarks.id = row.id
				getUserList(qdata).then(res => {
					this.authLists = res.data.data
					this.modalpass = true;
				})
			},
			getUserList() {
				getUserList({
					wid: this.tableFrom.wid
				}).then(res => {
					// console.log(res);
					this.authList = res.data.data
				})
			},
			getGodownList() {
				getGodownList().then(res => {
					console.log(res);
					this.data1 = res.data.data
				})
			},
			userSearchs(e) {
				console.log('zhix');
				this.tableFrom.page = 1;

				this.getList();
			},
			userSearchss(e) {
				this.tableFrom.auth_uid = ""
				this.authList = []
				this.getUserList()
				this.tableFrom.page = 1;
				this.getList();
			},
			staffList() {
				staffListInfo()
					.then((res) => {
						this.staffData = res.data;
					})
					.catch((err) => {
						this.$Message.error(err.msg);
					});
			},
			//获取供应商列表；
			getSupplierList() {
				getSupplierList()
					.then(async (res) => {
						this.supplierList = res.data;
					})
					.catch((res) => {
						this.$Message.error(res.msg);
					});
			},
			// 添加
			add() {
				this.$router.push({
					path: this.roterPre + "/erp/purchase_add?id=0"
				});
			},
			addMore() {
				this.$router.push({
					path: this.roterPre + "/marketing/store_integral/add_store_integral",
				});
			},
			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/add_position?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: `/erp/purchase/del/${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.tableList = []
				// this.tableFrom.start_status = this.tableFrom.start_status || "";
				// this.tableFrom.is_show = this.tableFrom.is_show || "";
				getPurchaselist(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>