<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
		    <el-form-item label="报工日期" prop="productDateArray">
				<el-date-picker
					v-model="queryParams.productDateArray"
					value-format="yyyy-MM-dd"
					type="daterange"
					range-separator="至"
					start-placeholder="开始日期"
					end-placeholder="结束日期">
				</el-date-picker>
			</el-form-item>

			<el-form-item label="产品编号" prop="productCode">
				<el-input
					v-model="queryParams.productCode"
					placeholder="请输入产品编码"
					clearable
				/>
			</el-form-item>
			<el-form-item label="产品名称" prop="productName">
				<el-input
					v-model="queryParams.productName"
					placeholder="请输入产品名称"
					clearable
				/>
			</el-form-item>
			<el-form-item label="规格型号" prop="productSpc">
				<el-input
					v-model="queryParams.productSpc"
					placeholder="请输入规格型号"
					clearable
				/>
			</el-form-item>
			<el-form-item label="工厂名称" prop="factoryName">
				<el-input
					v-model="queryParams.factoryName"
					placeholder="请输入工厂名称"
					clearable
				/>
			</el-form-item>
			<el-form-item label="车间名称" prop="carName">
				<el-input
					v-model="queryParams.carName"
					placeholder="请输入车间名称"
					clearable
				/>
			</el-form-item>
			<el-form-item label="机台名称" prop="machineName">
				<el-input
					v-model="queryParams.machineName"
					placeholder="请输入机台名称"
					clearable
				/>
			</el-form-item>
			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-row :gutter="10" class="mb8">
			<el-col :span="1.5">
				<el-button
					type="warning"
					plain
					icon="el-icon-download"
					size="mini"
					@click="handleExport"
					v-hasPermi="['mes:prepare:export']"
				>导出</el-button>
			</el-col>
			<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
		</el-row>

		<el-table v-loading="loading" :data="prepareList"
		 :summary-method="getSummaries"
          show-summary
		>
			<el-table-column type="selection" width="55" align="center" />
			<!-- 序号 -->
            <el-table-column type="index" align="center" :index="indexMethod" label="序号"/>
            <el-table-column label="工厂编码" align="center" prop="factoryCode" />
			<el-table-column label="工厂名称" align="center" prop="factoryName" />
			<el-table-column label="车间编码" align="center" prop="carCode" />
			<el-table-column label="车间名称" align="center" prop="carName" />
			<el-table-column label="机台编码" align="center" prop="machineCode" />
			<el-table-column label="机台名称" align="center" prop="machineName" />
			<el-table-column label="产品编码" align="center" prop="productCode" width="120"/>
			<el-table-column label="产品名称" align="center" prop="productName" width="150"/>
			<el-table-column label="规格型号" align="center" prop="productSpc" />
			<el-table-column label="派工数量" align="center" prop="quantity" />
			<el-table-column label="报工数量" align="center" prop="quantityFeedback" />
			<el-table-column label="单位" align="center" prop="unit" />
		</el-table>

		<pagination
			v-show="total>0"
			:total="total"
			:page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize"
			@pagination="getList"
		/>


	</div>
</template>

<script>
import { getProductionList} from "@/api/mes/reportWork";
import moment from 'moment';
export default {
	name: "Prepare",
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 总条数
			total: 0,
			// 备料单表格数据
			prepareList: [],
			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				productDateArray: [new Date(),new Date()],
				pageNum: 1,
				pageSize: 10,
				workorderCode: null,
				workorderName: null,
				parentOrder: null,
				orderId: null,
				orderCode: null,
				productId: null,
				productCode: null,
				prodType: null,
				productName: null,
				productSpc: null,
				wetDetailPlanId: null,
				productDate: null,
				shiftId: null,
				ancestors: null,
				status: null,
				attr1: null,
				attr2: null,
				attr3: null,
				attr4: null,
				factoryCode: null
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {}
		};
	},
	created() {
		this.getList();
	},
	methods: {
		indexMethod(index){
		   return index+1 ;
		},
		/** 查询备料单列表 */
		getList() {

			if(this.queryParams.productDateArray!=null){
				this.queryParams.productDateStart = moment(this.queryParams.productDateArray[0]).format('YYYY-MM-DD');
				this.queryParams.productDateEnd = moment(this.queryParams.productDateArray[1]).format('YYYY-MM-DD');
			}

			this.loading = true;
			getProductionList(this.queryParams).then(response => {
				this.prepareList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				prepareId: null,
				workorderCode: null,
				workorderName: null,
				parentOrder: null,
				orderId: null,
				orderCode: null,
				productId: null,
				productCode: null,
				prodType: null,
				productName: null,
				productSpc: null,
				wetDetailPlanId: null,
				productDate: null,
				shiftId: null,
				ancestors: null,
				status: null,
				remark: null,
				attr1: null,
				attr2: null,
				attr3: null,
				attr4: null,
				createBy: null,
				createTime: null,
				updateBy: null,
				updateTime: null,
				factoryCode: null
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},

		/** 导出按钮操作 */
		handleExport() {
			this.download('mes/reportWork/productionExport', {
				...this.queryParams
			}, `prepare_${new Date().getTime()}.xlsx`)
		},
		getSummaries(param) {
			const { columns, data } = param;
			const sums = [];
			columns.forEach((column, index) => {
				if (index === 0) {
					sums[index] = '合计';
					return;
				}
				const values = data.map(item => Number(item[column.property]));
				if (!values.every(value => isNaN(value))) {
					sums[index] = values.reduce((prev, curr) => {
					const value = Number(curr);
					if (!isNaN(value)) {
						return prev + curr;
					} else {
						return prev;
					}
					}, 0);
					sums[index] += '';
					if(sums[index]  > 1000000){
                      sums[index] = '';
					}
				} else {
					sums[index] = '';//N/A
				}
			});

			return sums;
		}
	}
};
</script>
