<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="98px">
            <el-form-item size="small" label="工单日期: ">
              <el-date-picker
                v-model="queryParams.productDate"
				value-format="yyyy-MM-dd"
                type="date"
                placeholder="工单日期"
              >
              </el-date-picker>
            </el-form-item>

			<el-form-item label="工作中心">
				 <el-select v-model="queryParams.workCenter" filterable placeholder="请选择工作中心">
					<el-option
					v-for="item in options"
					:key="item.factoryCode"
					:label="item.factoryName"
					:value="item.factoryCode">
					</el-option>
				</el-select>
			</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"
		 :header-cell-style="{'text-align':'center'}"
         :cell-style="{'text-align':'center'}"
          show-summary
		>
				<el-table-column
					prop="teamDesc"
					label="组别"
					width="90">
				</el-table-column>
				<el-table-column
					prop="prodType"
					label="分类"
					width="80">
				</el-table-column>
				<el-table-column
					prop="teamLeaderName"
					label="组长"
					width="100">
				</el-table-column>
				<el-table-column
					prop="productCode"
					label="产品编码"
					width="120">
				</el-table-column>
				<el-table-column
					prop="spec"
					label="规格"
					width="100">
				</el-table-column>
				<el-table-column
					prop="productName"
					label="产品名称"
					width="150">
				</el-table-column>
				<el-table-column
					prop="unit"
					label="单位"
					width="80">
				</el-table-column>
				<el-table-column label="本日产量">
					<el-table-column
						prop="quantity"
						label="计划"
						width="80">
					</el-table-column>
					<el-table-column
						prop="quantityFeedback"
						label="实际"
						width="80">
					</el-table-column>
					<el-table-column
						prop="quantityAct"
						label="盒量"
						width="80">
					</el-table-column>
					<el-table-column
						prop="completeRate"
						label="完成率"
						width="80">
					</el-table-column>
				</el-table-column>
				<el-table-column
					prop="iei"
					label="标准效率
                          （PC/H）"
					width="80">
				</el-table-column>
				<el-table-column label="用人">
					<el-table-column
						prop="manStandar"
						label="标准"
						width="80">
					</el-table-column>
					<el-table-column
						prop="useMan"
						label="实际"
						width="80">
					</el-table-column>
				</el-table-column>
				<el-table-column label="工时">
					<el-table-column
						prop="workTimeStandar"
						label="标准"
						width="80">
					</el-table-column>
					<el-table-column
						prop="workTime"
						label="实际"
						width="80">
					</el-table-column>
					<el-table-column
						prop="productivity"
						label="产线效率"
						width="80">
					</el-table-column>
				</el-table-column>
				<el-table-column label="人均效率">
                    <el-table-column
						prop="manAvgStandar"
						label="标准"
						width="80">
					</el-table-column>
					<el-table-column
						prop="manAvgActual"
						label="实际"
						width="80">
					</el-table-column>
					<el-table-column
						prop="manAvgDo"
						label="达成"
						width="80">
					</el-table-column>
				</el-table-column>
				<el-table-column
						prop="actManHour"
						label="实际人工时"
						width="80">
				</el-table-column>
				<el-table-column
						prop="name"
						label="人均效率未达成原因分析"
						width="150" v-if="false">
				</el-table-column>
		</el-table>
		<pagination
			v-show="total>0"
			:total="total"
			:page.sync="queryParams.pageNum"
			:limit.sync="queryParams.pageSize"
			@pagination="getList"
		/>


	</div>
</template>

<script>
import { getDailyReport,getWorkcenterList} 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: {
				productDate: new Date().toLocaleDateString().replace(/\//g, "-"),
				pageNum: 1,
				pageSize: 10,
				workCenter: null,
				factoryCode: null
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {},
			options: null
		};
	},
	created() {
		this.getList();
		this.getWorkcenterList();
	},
	methods: {
		indexMethod(index){
		   return index+1 ;
		},
		/** 查询备料单列表 */
		getList() {
			if(this.queryParams.productDate == null){
               this.$modal.msgSuccess("日期不能为空");
			   return
			}

			this.loading = true;
			getDailyReport(this.queryParams).then(response => {
				this.prepareList = response.rows;
				this.total = response.total;
				this.loading = false;
			});
		},
		getWorkcenterList(){
			
            getWorkcenterList().then(data => {
				this.options = data;
			});
			this.queryParams.workCenter = this.options[0].factoryCode;
		},
		// 取消按钮
		cancel() {
			this.open = false;
			this.reset();
		},
		// 表单重置
		reset() {
			this.form = {
				
			};
			this.resetForm("form");
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},

		/** 导出按钮操作 */
		handleExport() {
			this.download('mes/reportWork/getDailyReportExport', {
				...this.queryParams
			}, `dailyReport${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(index<6){
                      sums[index] = '';
					}
				} else {
					sums[index] = '';//N/A
				}
			});

			return sums;
		}
	}
};
</script>
