<template>
	<view>
		<view class="sm-box">
			<view class="card">
				<view class="title">注:</view>
				<view class="content">1.量化的需填写真实测量数据；</view>
				<view class="content">2.无量化检查项填写检查结果，结果判定：合格的填写“OK”，不合格的填“NG”；</view>
			</view>
			<view class="content-box-card">
				<view class="content-box-card-ul-two">
					<view class="content-box-card-list">
						<view class="content-box-card-title">物料编号:</view>
						<text>{{ CyInfo.product_number }}</text>
					</view>
					<view class="content-box-card-list">
						<view class="content-box-card-title">生产日期:</view>
						<text>{{ CyInfo.production }}</text>
					</view>
				</view>
				<view class="content-box-card-ul-two">
					<view class="content-box-card-list">
						<view class="content-box-card-title">机型:</view>
						<text>{{ CyInfo.product_size }}</text>
					</view>
					<view class="content-box-card-list">
						<view class="content-box-card-title">班次:</view>
						<picker @change="bindline" :value="froms.frequency" :range="line">
							<view class="lin-div">
								<text>{{ line[froms.frequency] }}</text>
								<image src="../../static/xiala.png"></image>
							</view>
						</picker>
					</view>
				</view>
				<view class="content-box-card-ul-two">
					<!-- <view class="content-box-card-list">
						<view class="content-box-card-title">时间段:</view>
						<text>2021-07-15</text>
					</view> -->
					<view class="content-box-card-list">
						<view class="content-box-card-title">检测员:</view>
						<text>{{ info ? info.name : '' }}</text>
					</view>
					<view class="content-box-card-list">
						<view class="content-box-card-title">生产数量:</view>
						<input type="number" v-model="froms.product_number" />
					</view>
				</view>
				<view class="content-box-card-ul-two">
					<view class="content-box-card-list">
						<view class="content-box-card-title">不合格数:</view>
						<input type="number" v-model="froms.no_number" />
					</view>

					<view class="content-box-card-list">
						<view class="content-box-card-title">报废数:</view>
						<input type="number" v-model="froms.scrap" />
					</view>
				</view>
				<view class="content-box-card-ul-two">
					<view class="content-box-card-list">
						<view class="content-box-card-title">抽样方式:</view>
						<picker @change="bindSamp" :value="froms.sampling" :range="samplingList">
							<view class="lin-div">
								<text>{{ samplingList[froms.sampling] }}</text>
								<image src="../../static/xiala.png"></image>
							</view>
						</picker>
					</view>

					<view class="content-box-card-list">
						<view class="content-box-card-title">抽样水准:</view>
						<input v-model="froms.method" />
					</view>
				</view>
			</view>
		</view>

		<view v-for="(item, index) in configSet" :key="index">
			<view class="big-title">{{ item.title }}</view>
			<view v-for="(value, idx) in item.check_conf_msg" :key="idx" class="cyxj-content" >
				<view class="cyxj-content-card" v-if="item.check_conf_msg[idx].status == 1">
					<view class="xx-card">
						<view class="xx-title">{{ value.title }}</view>
						<view class="xx-ul">
							<view class="xx-list" v-for="(va, i) in ifcomplete" :key="i" @tap="value.choice = va">
								<image :src="va == value.choice ? '../../static/xz.png' : '../../static/wxz.png'" mode=""></image>
								<text>{{ va }}</text>
							</view>
						</view>
						<view class="czyj-text" v-show="value.choice == 'NG'"><textarea v-model="value.content" placeholder="不良情况分析" /></view>
						<view class="xx-title" v-show="value.choice == 'NG'">不良处置情况</view>
						<picker :value="value.choiceIndex" v-show="value.choice == 'NG'" @change="pickerChange($event, index, idx)" :range="methList">
							<view class="slect-list">
								<text>{{ value.situation || '请选择' }}</text>
								<image src="../../static/xiala.png"></image>
							</view>
						</picker>
					</view>
				</view>
				<view class="cyxj-content-card" v-else>
					<view class="tx-ul">
						<view class="tx-title">{{ value.title }}</view>
						<input v-model="value.input" placeholder="请输入" />
					</view>
				</view>
			</view>
		</view>

		<view class="cyxj-content" style="margin-top: 24upx;">
			<view class="bzqk-card"><textarea v-model="froms.remake" placeholder="备注情况说明" /></view>
			<view class="submit-btn">
				<button type="primary" v-if="CyInfo.check == 0" @tap="submits">提交并发起审核</button>
				<button type="primary" v-if="CyInfo.check == 1" @tap="applys()">审核</button>
			</view>
		</view>
	</view>
</template>

<script>
import { wjCheckAdd, bad, getCheckConfSet } from '../../api/user.js';
import { wjCheckDetail, wjCheckSure } from '../../api/index.js';
export default {
	data() {
		return {
			samplingList: ['正常', '加量', '减量'],
			line: ['夜班', '白班'],
			ifcomplete: ['OK', 'NG'],
			ifcompleteIds: 0,
			methList: [],
			methIds: 0,
			planId: 0,
			CyInfo: {},
			info: {},
			froms: {
				item: [],
				plan_id: '',
				model: '',
				product_number: 0,
				no_number: 0,
				scrap: 0,
				sampling: 0,
				method: '',
				frequency: 0, //班次
				remake: '' //，备注
			},
			configSet: []
		};
	},
	onLoad(e) {
		this.pId = e.plan_id;
		this.froms.plan_id = this.pId;
		this.CyInfo = JSON.parse(decodeURIComponent(e.planInfo));
		this.froms.model = this.CyInfo.product_size;
		this.getInfo();
		bad({}).then(res => {
			for (let b in res) {
				this.methList.push(res[b]);
			}
		});
	},
	methods: {
		matchingConfig(value) {
			value.choice = 'OK';
			value.content = '';
			value.situation = '';
			value.input = '';
			let CheckPtMsg = this.info.CheckPtMsg ? this.info.CheckPtMsg : [];
			for (let b in CheckPtMsg) {
				if (CheckPtMsg[b].check_conf_id == value.id) {
					let json = JSON.parse(CheckPtMsg[b].json);
					value.choice = json.choice;
					value.content = json.content;
					value.situation = json.situation;
					value.input = json.input;
					break;
				}
			}

			return value;
		},
		getInfo() {
			wjCheckDetail({
				plan_id: this.pId
			}).then(res => {
				this.info = res;
				if (res) {
					this.froms.plan_id = this.pId;
					this.froms.product_number = res.product_number;
					this.froms.model = res.model;
					this.froms.no_number = res.no_number;
					this.froms.scrap = res.scrap;
					this.froms.frequency = res.frequency;
					this.froms.remake = res.remake;
					this.froms.sampling = res.sampling;
					this.froms.method = res.method;
				}
				getCheckConfSet({
					type: 4,
					product_id: this.CyInfo.product_id
				}).then(res => {
					this.configSet = res.map(item => {
						item.check_conf_msg = item.check_conf_msg.map(value => {
							return this.matchingConfig(value);
						});
						return item;
					});
				});
			});
		},
		bindline: function(e) {
			this.froms.frequency = e.target.value;
		},
		pickerChange(e, index, idx) {
			this.configSet[index].check_conf_msg[idx].situation = this.methList[e.detail.value];
		},
		bindSamp(e) {
			this.froms.sampling = e.target.value;
		},
		submits() {
			for (let b in this.configSet) {
				for (let i in this.configSet[b].check_conf_msg) {
					let items = this.configSet[b].check_conf_msg[i];
					if (this.configSet[b].status == 1) {
						if (items.choice == 'NG' && (!items.content || !items.situation)) {
							this.$functions.error('请完整填写信息');
							return false;
						}
					} else {
						if (!items.input) {
							this.$functions.error('请完整填写信息');
							return false;
						}
					}

					this.froms.item.push({
						check_conf_id: items.id,
						json: {
							choice: items.choice,
							content: items.content,
							situation: items.situation,
							input: items.input
						}
					});
				}
			}

			let params = JSON.parse(JSON.stringify(this.froms));
			params.sampling = params.sampling + 1;
			wjCheckAdd(params).then(res => {
				this.$functions.success('提交成功').then(() => {
					uni.navigateBack({
						delta: -1
					});
				});
			});
		},
		applys() {
			this.$functions.confirm('是否确定操作？').then(res => {
				ptCheckSure({
					plan_id: this.info.plan_id,
					play_id: this.info.play_id
				}).then(res => {
					this.$functions.success('操作成功').then(() => {
						uni.navigateBack({});
					});
				});
			});
		}
	}
};
</script>

<style lang="scss">
@import './index.scss';
</style>
