<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="yearMonthDate">
				<el-date-picker
				v-model="queryParams.yearMonthDate"
				type="month"
				placeholder="选择月">
				</el-date-picker>
			</el-form-item>
			<el-form-item label="白坯名称" prop="materialName">
				<el-input
					v-model="queryParams.materialName"
					placeholder="请输入白坯名称"
					clearable
					@keyup.enter.native="handleQuery"
				/>
			</el-form-item>

			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button
					type="warning"
					plain
					icon="el-icon-download"
					size="mini"
					@click="handleExport"
				>导出</el-button>
				<span style="color:firebrick;font-size: small; text-align: center;padding-left:30px">说明：空白处表示当日未生产。</span>
			</el-form-item>
		</el-form>
		<el-tabs v-model="activeName" type="card" @tab-click="handleClick">
			<el-tab-pane label="图表" name="first">
			    <el-form ref="form" :model="form" label-width="80px">
					<el-row>
						<el-col :span="24" style="text-align:center">
							<h2>{{ parseTime(queryParams.yearMonthDate,'{y}-{m}')}}  白 坯 不 良 排 列 图</h2>
						</el-col>
					</el-row>
					<el-descriptions title="" border :column="3" :contentStyle="content_style" :label-style="label_style">
						<el-descriptions-item label="产品名称" class="my-description-item1">白坯</el-descriptions-item>
						<el-descriptions-item label="抽样数" class="my-description-item1">{{form.sampleQty}}</el-descriptions-item>

						<el-descriptions-item label="生产车间" class="my-description-item1">白坯车间</el-descriptions-item>
						<el-descriptions-item label="不良品数" class="my-description-item1">{{form.noOkQty}}</el-descriptions-item>

						<el-descriptions-item label="不良率" class="my-description-item1">{{form.noOkRate}}%</el-descriptions-item>
						<el-descriptions-item label="合格率" class="my-description-item1">{{form.okRate}}%</el-descriptions-item>
					</el-descriptions>
				</el-form>
				<div id="echartss" style="width:1200px;height:450px;"></div>
			</el-tab-pane>
			<el-tab-pane label="报表" name="second">
				<el-table v-loading="loading" :data="itemProList" border>
					<el-table-column label="序号" type="index" align="center" :index="indexMethod" fixed/>
					<el-table-column label="项目" align="center" prop="dataType" min-width="100" fixed/>
						<template v-for="(column, index) in showTitles">
							<el-table-column align="center" min-width="120" :prop="column.id" :key="column.id" :label="column.titleName"/>
						</template>
				</el-table>
			</el-tab-pane>
		</el-tabs>
	</div>
</template>

<script>
import {getDefectItemTitle,getDefectItemData,getDefectItemLine} from "@/api/quality/qcTable";
import moment from 'moment';
import * as echarts from "echarts";
import {parseTime} from "../../../../utils/ruoyi";
export default {
	name: "BpDefectItem",
	data() {
		return {
			// 遮罩层
			loading: true,
			// 选中数组
			ids: [],
			// 非单个禁用
			single: true,
			// 非多个禁用
			multiple: true,
			// 显示搜索条件
			showSearch: true,
			// 备料单表格数据
			itemProList: [],
 			activeName: 'first',
            showTitles:[],//列表展示的小时段

			// 弹出层标题
			title: "",
			// 是否显示弹出层
			open: false,
			// 查询参数
			queryParams: {
				yearMonthDate:null,
				yearMonth:null,
				productDateArray: [],
				pageNum: 1,
				pageSize: 10,
				workorderCode: null,
				workorderName: null,
				parentOrder: null,
				orderId: null,
				orderCode: null,
				productId: null,
				productCode: null,
				prodType: null,
				productName: null,
				productSpc: null,
				productDate: null,
				equTypeCode: "equ_type_hf"
			},
			// 表单参数
			form: {},
			// 班次list
            workShift: [],
			equipmentTypeOption:[
				{equipmentTypeCode:"equ_type_cxj",equipmentTypeName:"成型机"},
				{equipmentTypeCode:"equ_type_hf",equipmentTypeName:"烘房"}
			],
			spanArr: [],
            position: 0,
			content_style: {
				'text-align': 'center',
				'min-width': '200px',
				'word-break': 'break-all'
			},
			label_style: {
				'color': '#000',
				'text-align': 'center',
				'font-weight': '600',
				'height': '40px',
				'background-color': '#d6dbe1',
				'min-width': '100px',
				'word-break': 'keep-all'
			},
		};
	},
	created() {
        this.getDate();
		this.getList();
	},
	methods: {
    parseTime,
        /**获取默认查询时间段**/
		getDate() {
			let start = this.Fungetdate (0);
			this.queryParams.yearMonthDate = start;
		},
		Fungetdate (num) {
			var dd = new Date();
			dd.setDate(dd.getDate() + num);
			var y = dd.getFullYear();
			var m = dd.getMonth() + 1;//获取当前月份的日期
			return y + "-" + m ;
		},

		/** 查询设备小时产量列表 */
		getList() {

			if(this.queryParams.yearMonthDate!=null){
				this.queryParams.yearMonth = moment(this.queryParams.yearMonthDate).format('YYYY-MM');
			}else{
				this.$message.error("请填写月份");
				return
			}

			this.spanArr = [];
			this.position = 0;

			this.loading = true;
            //获取Table表头
			getDefectItemTitle(this.queryParams).then(response => {
				this.showTitles = [];
				for(let i=0;i<=response.length-1;i++){
					var pobj={};
					pobj.id="item"+i;
					pobj.titleName = response[i];
					this.showTitles.push(pobj)
				}
			});
            //获取Table数据
			getDefectItemData(this.queryParams).then(response => {
				this.itemProList = response;
			    this.getTbody();
				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");
			this.spanArr = [];
			this.position = 0;
		},
		/** 搜索按钮操作 */
		handleQuery() {
			this.queryParams.pageNum = 1;
			this.getList();
		},
		/** 重置按钮操作 */
		resetQuery() {
			this.resetForm("queryForm");
			this.handleQuery();
		},

		/** 导出按钮操作 */
		handleExport() {
			this.download('quality/staticTable/getDefectItemDataExport', {
				...this.queryParams
			}, `bpDefectItem_${new Date().getTime()}.xlsx`)
		},


		handleClick(){
			this.$nextTick(()=>{
				this.$refs.tables.doLayout()//对table进行重新布局
			})
		},
		async getTbody(){
			this.loading=true
      let _this = this
			getDefectItemLine(this.queryParams).then(response => {
        console.log(response.data)
        _this.form = response.data;
				_this.getEcharts(response.data.xData,response.data.yDataLeft,response.data.yDataRight)
			});
		},
		getEcharts(xData,yDataLeft,yDataRight){
			var chartDom = document.getElementById('echartss');
			var myChart = echarts.init(chartDom);
			var option;
			option = {
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    crossStyle: {
                        color: '#999'
                    }
                }
            },
            legend: {
                data: ['不良率', '累计不良率']
            },
            xAxis: {
                type: 'category',
                data: xData//['断片', '断裂', '变形', '干料', '杂质']
            },
            yAxis: [
                {
                    type: 'value',
                    name: '不良率(%)',
                    min: 0,
                    max: 100,
                    interval: 5,
                    axisLabel: {
                        formatter: '{value} %'
                    }
                },
                {
                    type: 'value',
                    name: '累计不良率',
                    min: 0,
                    //max: 100,
                    interval: 20,
                    axisLabel: {
                        formatter: '{value} %'
                    }
                }
            ],
            series: [
                {
                    name: '不良率',
                    type: 'bar',
                    data: yDataLeft, //[15, 10, 8, 5, 2],
					label: {
						show: true, // 开启显示
						position: 'top', // 显示位置，可以是'top', 'bottom', 'left', 'right'等
						formatter: '{c}%' // {c} 表示系列中的当前数据值
					},
                },
                {
                    name: '累计不良率',
                    type: 'line',
                    yAxisIndex: 1,
                    data: yDataRight //[15, 25, 33, 38, 40]
                }
            ]
        };

				option && myChart.setOption(option);
		},

		// 生成表头序号
		indexMethod(index) {
			return index + 1;
		},
	}
};
</script>
<style scoped>
.my-description-item1 {
  width: 200px; /* 或者你想要的任何宽度 */
}
</style>
