<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="timeArray">
        <el-date-picker
				v-model="queryParams.timeArray"
				format="yyyy-MM-dd"
				type="daterange"
				range-separator="至"
				start-placeholder="开始日期"
				end-placeholder="结束日期"
        clearable
        @keyup.enter.native="handleQuery"
        >
				</el-date-picker>
			</el-form-item>
      <el-form-item label="产线名称" prop="sapName">
        <el-input
          v-model="queryParams.sapName"
          placeholder="请输入产线名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
			<el-form-item label="工作中心">
				 <el-select v-model="queryParams.workCenter" filterable placeholder="请选择工作中心" clearable>
					<el-option
					v-for="item in options"
					:key="item.factoryCode"
					:label="item.factoryName"
					:value="item.factoryCode"
          @keyup.enter.native="handleQuery"
          >
					</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:dailyReportNew:export']"
				>导出</el-button>
			</el-col>
      <el-col :span="20">
      <div style="color:firebrick;font-size: small; text-align: center;">产量=规格*实际产量，订单完成率=实际产量/计划产量，标准工时=产量*规格/产线标准效率，产线效率=标准工时/实际工时，
		总工时=实际用人*实际工时，<br/>标准人均效率=实际产量/标准用人/标准工时，实际人均效率=实际产量/总工时，人均效率达成率=实际人均效率/标准人均效率</div>
      </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'}"
		>
      <el-table-column prop="factoryCode"  label="工厂" width="90"/>
      <el-table-column prop="workshopCode" label="车间编码" width="90"/>
      <el-table-column prop="workshopName" label="车间名称" width="90"/>
      <el-table-column prop="productDate" label="日期" width="100"/>
      <el-table-column prop="sapCode" label="产线编码" width="90"/>
      <el-table-column prop="sapName" label="产线名称" width="160"/>
			<el-table-column prop="equipmentName" label="组别" width="90"/>
			<el-table-column prop="prodType" label="品类" width="80"/>
			<el-table-column prop="teamLeaderName" label="组长" width="100"/>
            <el-table-column prop="workorderCode" label="工单号" width="150"/>
			<el-table-column prop="productCode"	label="产品编码" width="120" :formatter="productCodeFormate"/>
			<el-table-column prop="productName" label="产品名称" width="200"/>
      		<el-table-column prop="spec" label="规格"	width="100"/>
			<el-table-column prop="unit" label="单位(件)"	width="80"/>
			<el-table-column prop="quantitySplit" label="计划产量(件)" width="80"/>
			<el-table-column prop="quantityFeedback" label="实际产量(件)" width="80"/>
			<el-table-column prop="quantityAct" label="产量(PC)" width="80"/>
			<el-table-column prop="completeRate" label="订单完成率(%)" width="80"/>
			<el-table-column prop="efficiency" label="产线标准效率(PC/H)" width="100"/>
			<el-table-column prop="manStandard" label="标准用人" width="80"/>
			<el-table-column prop="useMan" label="实际用人" width="80"/>
			<el-table-column
				prop="workTimeStandard"
		 	  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
				prop="totalWorkTime"
				label="总工时"
				width="80">
			</el-table-column>
      <el-table-column
				prop="manAvgStandard"
				label="标准人均效率"
				width="100">
			</el-table-column>
			<el-table-column
				prop="manAvgActual"
				label="实际人均效率"
				width="100">
			</el-table-column>
			<el-table-column
				prop="manAvgDo"
				label="人均效率达成率"
				width="120">
			</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 { getDailyReportNew,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: {
        timeArray: [moment(new Date().toLocaleDateString().replace(/\//g, "-")).format('YYYY-MM-DD'),
        moment(new Date().toLocaleDateString().replace(/\//g, "-")).format('YYYY-MM-DD')],
				productDateStart: null,
        productDateEnd: null,
        sapName: null,
				pageNum: 1,
				pageSize: 10,
				workCenter: null,
				factoryCode: null
			},
			// 表单参数
			form: {},
			// 表单校验
			rules: {},
			options: null
		};
	},
	created() {
		this.getList();
		this.getWorkcenterList();
	},
	methods: {
		indexMethod(index){
		   return index+1 ;
		},
		/** 查询备料单列表 */
		getList() {
			this.loading = true;
      if(this.queryParams.timeArray.length>0){
				this.queryParams.productDateStart = moment(this.queryParams.timeArray[0]).format('YYYY-MM-DD');
				this.queryParams.productDateEnd = moment(this.queryParams.timeArray[1]).format('YYYY-MM-DD');
			}

			getDailyReportNew(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");
		},
    productCodeFormate(row, column, cellValue){
			if(cellValue != null){
				return cellValue.slice(7,18);  //返回值
			}
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},

		/** 导出按钮操作 */
		handleExport() {
			this.download('mes/reportWork/getDailyReportNewExport', {
				...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>
