<template>
	<el-container>
		<el-header>
			<div class="right-panel">
				<el-date-picker
					v-model="query.mio__inout_date__gte"
					type="date"
					placeholder="开始日期"
					format="YYYY-MM-DD"
					value-format="YYYY-MM-DD"
					style="width: 150px;"
				/>
				<el-date-picker
					v-model="query.mio__inout_date__lte"
					type="date"
					format="YYYY-MM-DD"
					value-format="YYYY-MM-DD"
					placeholder="结束日期"
					style="width: 150px;"
				/>
				<el-input
					v-model="query.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
				:params="params"
				:query="query"
				:summary-method="getSummaries" 
				show-summary
			>
				<el-table-column type="index" width="50" />
				<el-table-column label="日期" prop="inout_date">
				</el-table-column>
				<el-table-column label="物料编号" show-overflow-tooltip>
					<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>
					<template #default="scope">
						<span v-if="scope.row.material_">{{ scope.row.material_.name }}</span>
					</template>
				</el-table-column>
				<el-table-column label="货位号">
					<template #default="scope">
						<span v-if="scope.row.material_">{{ scope.row.material_.bin_number_main }}</span>
					</template>
				</el-table-column>
				<el-table-column label="规格" prop="batch">
					<template #default="scope">
						<span v-if="scope.row.material_">{{ scope.row.material_.specification }}</span>
					</template>
				</el-table-column>
				<el-table-column label="品牌型号" prop="batch">
					<template #default="scope">
						<span v-if="scope.row.material_">{{ scope.row.material_.model }}</span>
					</template>
				</el-table-column>
				<el-table-column label="单位" prop="batch">
					<template #default="scope">
						<span v-if="scope.row.material_">{{ scope.row.material_.unit }}</span>
					</template>
				</el-table-column>
				<el-table-column label="入库数量" prop="count">
				</el-table-column>
				<el-table-column label="单价" prop="unit_price">
				</el-table-column>
				<el-table-column label="总金额" prop="price">
				</el-table-column>
				<el-table-column label="入库人">
					<template #default="scope">
						<span v-if="scope.row.mio_">{{ scope.row.mio_.submit_user_name }}</span>
					</template>
				</el-table-column>
				<el-table-column label="入库凭证号">
					<template #default="scope">
						<span v-if="scope.row.mio_">{{ scope.row.mio_.number }}</span>
					</template>
				</el-table-column>
				<el-table-column label="类别" prop="type">
					<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>
			</scTable>
		</el-main>
	</el-container>
</template>
<script>
export default {
	name: "mioitemlist",
	data() {
		return {
			apiObj: null,
			params: {with_mio:'yes',material__type:30,mio__state:20},
			selection: [],
			tableData:[],
			query: {
				search: "",
				mio__inout_date__gte: "",
				mio__inout_date__lte: "",
			},
			stateDict: {
				10: "创建中",
				20: "已提交",
			},
			typeDict: {
				do_out: "生产领料",
				sale_out: "销售发货",
				pur_in: "采购入库",
				do_in: "生产入库",
				other_in: "其他入库",
				other_out: "其他出库",
			},
			project_code:'',
		};
	},
	mounted() {
		this.checkTemplate = this.checkTemplate+"?t=" + new Date().getTime();
		this.project_code = this.$TOOL.data.get("BASE_INFO").base.base_code;
		this.params.mio = this.mioId;

		this.apiObj = this.$API.inm.mioitem.list;
	},
	methods: {
		handleCheckSuccess() {
			this.$refs.table.refresh();
			this.dialog.check = false;
		},
		handleQuery() {
			this.$refs.table.queryData(this.query);
		},
		resetQuery() {
			this.query = {};
		},
		
		//打印物料标签
		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;
						that.$API.wpm.prints.req(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;
						that.$API.wpm.prints.req(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 == 8|| index == 10) {
					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;
		},
	},
};
</script>
