<template>
	<el-container>
		<el-header>
			<div class="right-panel">
				<el-date-picker
					v-model="params.mio__inout_date__gte"
					type="date"
					placeholder="开始日期"
					format="YYYY-MM-DD"
					value-format="YYYY-MM-DD"
					style="width: 150px;"
				/>
				<el-date-picker
					v-model="params.mio__inout_date__lte"
					type="date"
					format="YYYY-MM-DD"
					value-format="YYYY-MM-DD"
					placeholder="结束日期"
					style="width: 150px;"
				/>
				<xtSelect
					:apiObj="apiObjm"
					v-model="params.material"
					v-model:obj="selectObj"
					:labelField="'full_name'"
					style="width: 150px;"
					:params="mquery"
					@change="selectMaterialChange"
				>
					<el-table-column label="物料" prop="full_name"></el-table-column>
				</xtSelect>
				<el-select
					v-model="params.mio__type"
					clearable
					style="width: 120px; margin-left: 2px"
					placeholder="出入库类型"
					@change="handleQuery"
				>
					<el-option
						v-for="item in cateOptions"
						:key="item.id"
						:label="item.name"
						:value="item.id"
					></el-option>
				</el-select>
				<el-input
					v-model="params.search"
					placeholder="名称"
					clearable
					style="width: 150px;"
				></el-input>
				<el-button
					type="primary"
					icon="el-icon-search"
					@click="handleQuery"
				></el-button>
			</div>
		</el-header>
		<el-main style="padding: 0;">
			<scTable
				ref="table"
				:apiObj="apiObj"
				row-key="id"
				stripe
				border
				:params="params"
				:summary-method="getSummaries" 
				show-summary
				:hExportCols = "hExportCols"
				:hExportName="'辅料出入库记录'"
				style="height: 100%;"
			>
				<el-table-column type="index" width="50" />
				<el-table-column label="类型" width="80">
					<template #default="scope">
						<span v-if="scope.row.mio_">{{typeDict[scope.row.mio_.type] }}</span>
					</template>
				</el-table-column>
				<el-table-column label="日期" prop="inout_date" sortable width="90">
				</el-table-column>
				<el-table-column label="物料编号" show-overflow-tooltip min-width="80"
					:filters="nameFilters1"
					:filter-method="filterName1"
					filter-placement="bottom-end">
					<template #default="scope">
						<span v-if="scope.row.material_">{{ scope.row.material_.number }}</span>
					</template>
				</el-table-column>
				<el-table-column label="名称" show-overflow-tooltip min-width="80"
					:filters="nameFilters2"
					:filter-method="filterName2"
					filter-placement="bottom-end">
					<template #default="scope">
						<span v-if="scope.row.material_">{{ scope.row.material_.name }}</span>
					</template>
				</el-table-column>
				<el-table-column label="货位号"
					:filters="nameFilters3"
					:filter-method="filterName3"
					filter-placement="bottom-end">
					<template #default="scope">
						<span v-if="scope.row.material_">{{ scope.row.material_.bin_number_main }}</span>
					</template>
				</el-table-column>
				<el-table-column label="规格"
					:filters="nameFilters4"
					:filter-method="filterName4"
					filter-placement="bottom-end">
					<template #default="scope">
						<span v-if="scope.row.material_">{{ scope.row.material_.specification }}</span>
					</template>
				</el-table-column>
				<el-table-column label="品牌型号"
					:filters="nameFilters5"
					:filter-method="filterName5"
					filter-placement="bottom-end">
					<template #default="scope">
						<span v-if="scope.row.material_">{{ scope.row.material_.model }}</span>
					</template>
				</el-table-column>
				<el-table-column label="单位" prop="batch" width="70"
					:filters="nameFilters6"
					:filter-method="filterName6"
					filter-placement="bottom-end">
					<template #default="scope">
						<span v-if="scope.row.material_">{{ scope.row.material_.unit }}</span>
					</template>
				</el-table-column>
				<el-table-column label="入库数量" prop="count" sortable>
				</el-table-column>
				<el-table-column label="单价" prop="unit_price" sortable>
				</el-table-column>
				<el-table-column label="总金额" prop="price" sortable>
				</el-table-column>
				<el-table-column label="入库人"
					:filters="nameFilters7"
					:filter-method="filterName7"
					filter-placement="bottom-end">
					<template #default="scope">
						<span v-if="scope.row.mio_">{{ scope.row.mio_.submit_user_name }}</span>
					</template>
				</el-table-column>
				<el-table-column label="入库凭证号"
					:filters="nameFilters8"
					:filter-method="filterName8"
					filter-placement="bottom-end">
					<template #default="scope">
						<span v-if="scope.row.mio_">{{ scope.row.mio_.number }}</span>
					</template>
				</el-table-column>
				<el-table-column label="类别" prop="type"
					:filters="nameFilters9"
					:filter-method="filterName9"
					filter-placement="bottom-end">
					<template #default="scope">
						<span v-if="scope.row.mio_">{{ typeDict[scope.row.mio_.type] }}</span>
					</template>
				</el-table-column>
				<el-table-column label="供应商" prop="supplier_name">
				</el-table-column>
				<el-table-column
					label="操作"
					fixed="right"
					width="80"
				>
					<template #default="scope">
						<el-button
							size="small"
							type="danger"
							@click="revertAndDel(scope.row)"
							v-if="scope.row.mio_&&scope.row.mio_.state == 20"
						>撤消</el-button>
					</template>
				</el-table-column>
			</scTable>
		</el-main>
	</el-container>
</template>
<script>
export default {
	name: "mioitemlist",
	data() {
		return {
			params: {
				with_mio:'yes',
				material__type:40,
				mio__state:20,
				mio__type:'',
				mio__inout_date__gte:'',
				mio__inout_date__lte:'',
				material:'',
				search:'',
			},
			mquery:{
				page: 0,
				type__in: "40, 50, 60, 70",
				is_hidden: false,
				is_assemb: false,
			},
			selection: [],
			nameFilters1: [],
			nameFilters2: [],
			nameFilters3: [],
			nameFilters4: [],
			nameFilters5: [],
			nameFilters6: [],
			nameFilters7: [],
			nameFilters8: [],
			nameFilters9: [],
			stateDict: {
				10: "创建中",
				20: "已提交",
			},
			typeDict: {
				pur_in: "采购入库",
				do_out: "生产领料",
				borrow_out: "领用出库",
				return_in: "退还入库",
				pur_out: "采购退货",
				other_in: "其他入库",
			},
			cateOptions: [
				{ id: "pur_in", name: "采购入库" },
				{ id: "do_out", name: "生产领料" },
				{ id: "borrow_out", name: "领用出库" },
				{ id: "return_in", name: "退还入库" },
				{ id: "pur_out", name: "采购退货"},
				{ id: "other_in", name: "其他入库"},
			],
			selectObj: {},
			apiObj:this.$API.inm.mioitem.list,
			apiObjm:this.$API.mtm.material.list,
			hExportCols:[{
				header: "类型",
				key: "mio_.type",
				type_dict:{
					do_out: "生产领料",
					sale_out: "销售发货",
					pur_in: "采购入库",
					do_in: "生产入库",
					other_in: "其他入库",
					other_out: "其他出库",
				},
				is_notquery: true
			}, {
			    header: "日期",
				key: "inout_date"
			}, {
				header: "物料编号",
				key: "material_.number"
			}, {
				header: "名称",
				key: "material_.name"
			}, {
				header: "货位号",
				key: "material_.bin_number_main"
			}, {
				header: "规格",
				key: "material_.specification"
			}, {
				header: "品牌型号",
				key: "material_.model"
			}, {
				header: "单位",
				key: "material_.unit"
			}, {
			    header: "入库数量",
				key: "count"
			}, {
			    header: "单价",
				key: "unit_price"
			}, {
			    header: "总金额",
				key: "price",
				is_notquery: true,
			}, {
			    header: "入库人",
				key: "mio_.submit_user_name",
				is_notquery: true,
			}, {
			    header: "入库凭证号",
				key: "mio_.number",
				is_notquery: true,
			}, {
			    header: "供应商",
				key: "supplier_name",
				is_notquery: true,
			}
			]
		};
	},
	mounted() {
		this.$nextTick(() => {
			this.getFilters();
		});
	},
	methods: {
		getFilters(){
			let that = this;
			let datas = [];
			datas = that.$refs.table.tableData;
			console.log( datas);
			if(datas.length&&datas.length>0){
				let numberList=[],nameList = [],batchlist=[],specificationList=[],modelList=[],unitList=[],userList=[],mioNumberList=[],typeList=[];
				datas.forEach((ite) => {
					if (numberList.indexOf(ite.material_.number) > -1) {} else {
						numberList.push(ite.material_.number);
						let obj = {};
						obj.text = ite.material_.number;
						obj.value = ite.material_.number;
						that.nameFilters1.push(obj);
					}
					if (nameList.indexOf(ite.material_.name) > -1) {} else {
						nameList.push(ite.material_.name);
						let obj = {};
						obj.text = ite.material_.name;
						obj.value = ite.material_.name;
						that.nameFilters2.push(obj);
					}
					if (batchlist.indexOf(ite.material_.bin_number_main) > -1) {} else {
						batchlist.push(ite.material_.bin_number_main);
						let obj = {};
						obj.text = ite.material_.bin_number_main;
						obj.value = ite.material_.bin_number_main;
						that.nameFilters3.push(obj);
					}
					if (specificationList.indexOf(ite.material_.specification) > -1) {} else {
						specificationList.push(ite.material_.specification);
						let obj = {};
						obj.text = ite.material_.specification;
						obj.value = ite.material_.specification;
						that.nameFilters4.push(obj);
					}
					if (modelList.indexOf(ite.material_.model) > -1) {} else {
						modelList.push(ite.material_.model);
						let obj = {};
						obj.text = ite.material_.model;
						obj.value = ite.material_.model;
						that.nameFilters5.push(obj);
					}
					if (unitList.indexOf(ite.material_.unit) > -1) {} else {
						unitList.push(ite.material_.unit);
						let obj = {};
						obj.text = ite.material_.unit;
						obj.value = ite.material_.unit;
						that.nameFilters6.push(obj);
					}
					if (userList.indexOf(ite.mio_.submit_user_name) > -1) {} else {
						userList.push(ite.mio_.submit_user_name);
						let obj = {};
						obj.text = ite.mio_.submit_user_name;
						obj.value = ite.mio_.submit_user_name;
						that.nameFilters7.push(obj);
					}
					if (mioNumberList.indexOf(ite.mio_.number) > -1) {} else {
						mioNumberList.push(ite.mio_.number);
						let obj = {};
						obj.text = ite.mio_.number;
						obj.value = ite.mio_.number;
						that.nameFilters8.push(obj);
					}
					if (typeList.indexOf(ite.mio_.type) > -1) {} else {
						typeList.push(ite.mio_.type);
						let obj = {};
						obj.text = that.typeDict[ite.mio_.type];
						obj.value = that.typeDict[ite.mio_.type];
						that.nameFilters9.push(obj);
					}
				})
			}else{
				setTimeout(function(){
					that.getFilters();
				},500);
			}
		},
		handleCheckSuccess() {
			this.$refs.table.refresh();
			this.dialog.check = false;
		},
		handleQuery() {
			let that = this;
			that.$refs.table.queryData(that.params);
		},
		selectMaterialChange() {
			var that = this;
			that.params.material = that.selectObj.id;
		},
		//打签
		printMaterial(row,type){
			let that = this;
			if(that.printer_name!==''&&that.printer_name!==null&&that.printer_name!==undefined){
				if(type=='mioitem'){
					let params = {};
					params.tid = row.id;
					params.extra_data={count:row.count};
					params.label_template_name = '库存标签模板';
					that.$API.cm.labelmat.fromMioitem.req(params).then((res) => {
						let obj = {};
						obj.printer_commands = res.commands;
						obj.printer_name = that.printer_name;
						let printer_ip=localStorage.getItem("printer_ip")||'127.0.0.1';
that.$API.wpm.prints.req(printer_ip, obj).then((response) => {
							that.$message.success("打印成功");
						});
					})
				}else{
					let params = {};
					let name = that.printMaterialName.split('|')[0];
					params.label_template_name = '单件打印模板';
					params.data = {number:row.number,name:name};
					that.$API.cm.labeltemplate.commands.req(params).then((res) => {
						let obj = {};
						obj.printer_commands = res.commands;
						obj.printer_name = that.printer_name;
						let printer_ip=localStorage.getItem("printer_ip")||'127.0.0.1';
that.$API.wpm.prints.req(printer_ip, obj).then((response) => {
							that.$message.success("打印成功");
						});
					});
				}
			}else{
				that.printSetting();
			}
		},
		getSummaries({ columns, data }) {
			const sums = [];
			columns.forEach((column, index) => {
				if (index === 0) {
					sums[index] = "合计";
					return;
				}
				if (index == 9|| index == 11) {
					const values = data.map((item) =>
						Number(item[column.property])
					);
					if (!values.every((value) => Number.isNaN(value))) {
						sums[index] = values.reduce((prev, curr) => {
							const value = Number(curr); //Number转换为数值
							let sum = Number(
								Number(prev) + Number(curr)
							).toFixed(2); //toFixed(2)数据项保留两位小数
							if (!isNaN(value)) {
								return sum;
							} else {
								return prev;
							}
						}, 0);
					} 
				}
			});
			return sums;
		},
		filterName1(value, row) {
            return row.物料编号 === value;
        },
		filterName2(value, row) {
            return row.名称 === value;
        },
		filterName3(value, row) {
            return row.货位号 === value;
        },
		filterName4(value, row) {
            return row.规格 === value;
        },
		filterName5(value, row) {
            return row.品牌型号 === value;
        },
		filterName6(value, row) {
            return row.单位 === value;
        },
		filterName7(value, row) {
            return row.单价 === value;
        },
		filterName8(value, row) {
            return row.入库人 === value;
        },
		filterName9(value, row) {
            return row.	入库凭证号 === value;
        },
		revertAndDel(row){
			let that = this;
			that.$confirm('此操作将撤回并删除该记录, 是否继续?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(res=>{
				that.$API.inm.mioitem.revertDel.req(row.id).then(res=>{
					that.$message.success("撤回并删除成功");
					that.getList(1);
				})
			})
		},
	},
};
</script>
