<template>
	<div>
		<!-- 		<el-card class="box-card" style="height: 100%;">
 -->
		<!-- <div slot="header" class="clearfix"> -->
		<el-breadcrumb separator-class="el-icon-arrow-right">
			<el-breadcrumb-item>首页</el-breadcrumb-item>
			<!-- <el-breadcrumb-item :to="{ path: '/jc/setTaskcd'}">例行检测</el-breadcrumb-item> -->
			<!-- <el-breadcrumb-item>承担单位</el-breadcrumb-item>
			<el-breadcrumb-item :to="{ path: '/jc/routineMonitor' }">例行检测</el-breadcrumb-item>
			<el-breadcrumb-item>执行任务</el-breadcrumb-item>
			 :to="{ path: '/jc/setTaskcd' }"
			<el-breadcrumb-item>抽样</el-breadcrumb-item> -->
			<el-breadcrumb-item>填报抽样单</el-breadcrumb-item>
		</el-breadcrumb>
  	<!-- <el-descriptions-item label="联系地址" :contentStyle="{'text-align': 'right'}">
	  江苏省苏州市吴中区吴中大道 1188 号
	</el-descriptions-item> -->
<el-descriptions  title="农产品质量安全抽样单" :column="2" border style="margin-top:20px" :contentStyle="{'text-align': left}">
	<el-descriptions-item label="*样品名称" prop="cyd_data.sample_name" class="labelItem" size="mini" required >
		<!-- <el-col :span="12"> -->
		<el-select v-model="cyd_data.sample_name" clearable style="width:60%">
				<el-option v-for="item in sampleList"
				:key="item.product_id" 
				:label="item.product_name"
				:value="item.product_name">
				</el-option>
		</el-select>
		<!-- </el-col> -->
	</el-descriptions-item>
	<el-descriptions-item label="样品编码" prop="cyd_data.sample_id" class="labelItem" style="" size="mini">
		<el-input v-model="cyd_data.sample_id" disabled>
			<el-button slot="append" @click="createCode">生成</el-button>
		</el-input>
	</el-descriptions-item>
	<el-descriptions-item label="商标" prop="cyd_data.brand" class="labelItem" size="mini">
		<el-input v-model="cyd_data.brand"></el-input>
	</el-descriptions-item>
	<el-descriptions-item label="包装" prop="cyd_data.ispack" class="labelItem" style="" size="mini">
		<el-radio v-model="cyd_data.ispack" label="有">有</el-radio>
		<el-radio v-model="cyd_data.ispack" label="无">无</el-radio>
	</el-descriptions-item>
	<el-descriptions-item label="型号规格" prop="cyd_data.type" class="labelItem" size="mini">
		<el-input v-model="cyd_data.type"></el-input>
	</el-descriptions-item>
	<el-descriptions-item label="执行标准" prop="cyd_data.standard" class="labelItem" style="" size="mini">
		<el-input v-model="cyd_data.standard"></el-input>
	</el-descriptions-item>
	<el-descriptions-item label="*生产日期或批号" prop="cyd_data.production_date" class="labelItem" size="mini" required>
		<el-input v-model="cyd_data.production_date"></el-input>
	</el-descriptions-item>
	<el-descriptions-item label="产地" prop="cyd_data.place" class="labelItem" style="" size="mini">
		<el-cascader  :options="options" v-model="selectedOptions" @change="handleChange" style="width:60%"></el-cascader>
	</el-descriptions-item>
	<el-descriptions-item label="*产品认证情况" prop="cyd_data.certicfie" class="labelItem" size="mini" required>
		<el-radio v-model="cyd_data.certicfie" label="无公害农产品">无公害农产品</el-radio>
							<el-radio v-model="cyd_data.certicfie" label="绿色食品">绿色食品</el-radio>
							<el-radio v-model="cyd_data.certicfie" label="有机食品">有机食品</el-radio>
							<el-radio v-model="cyd_data.certicfie" label="其他">其他</el-radio>
	</el-descriptions-item>
	<el-descriptions-item label="证书编号" prop="cyd_data.certifie_num" class="labelItem" style="" size="mini">
		<el-input v-model="cyd_data.certifie_num"></el-input>
	</el-descriptions-item>
	<el-descriptions-item label="*抽样数量" prop="cyd_data.sample_num" class="labelItem" size="mini" required>
		<el-input v-model="cyd_data.sample_num" style="width:180px"></el-input>
		<a> * 单位 </a><el-input v-model="cyd_data.sample_num_unit" clearable="true" style="width:180px"></el-input>
	</el-descriptions-item>
	<el-descriptions-item label="抽样基数" prop="cyd_data.sample_base" class="labelItem" size="mini" required>
		<el-input v-model="cyd_data.sample_base" style="width:180px"></el-input>
		<a> * 单位 </a><el-input v-model="cyd_data.sample_base_unit" clearable="true" style="width:180px"></el-input>
	</el-descriptions-item>
	<el-descriptions-item label="*抽样场所" prop="cyd_data.sample_ground" class="labelItem" size="mini" required >
							<el-radio v-model="cyd_data.sample_ground" label="生产基地/企业">生产基地/企业</el-radio>
							<el-radio v-model="cyd_data.sample_ground" label="屠宰场">屠宰场</el-radio>
							<el-radio v-model="cyd_data.sample_ground" label="农贸市场">农贸市场</el-radio>
							<el-radio v-model="cyd_data.sample_ground" label="批发市场">批发市场</el-radio>
							<el-radio v-model="cyd_data.sample_ground" label="超市">超市</el-radio>
							<el-radio v-model="cyd_data.sample_ground" label="其他">其他</el-radio>
	</el-descriptions-item>
	</el-descriptions>
	<!-- 受检单位情况 -->
	<el-descriptions :column="1" border style="" >
		<el-descriptions-item label="受检单位情况" >
			<el-descriptions  :column="1" border  size="mini" style="width:100%">
				<el-descriptions-item label="*单位名称" prop="unit_test.unit_name" class="labelItem" style="" size="mini" required>
									<el-input v-model="unit_test.unit_name"></el-input>
				</el-descriptions-item>
				<el-descriptions-item label="*法定代表人" prop="unit_test.represent_name" class="labelItem" style="" size="mini" required>
									<el-input v-model="unit_test.represent_name"></el-input>
				</el-descriptions-item>
				<el-descriptions-item label="*受检人" prop="unit_test.user_name" class="labelItem" style="" size="mini" required>
									<el-input v-model="unit_test.user_name"></el-input>
				</el-descriptions-item>
				<el-descriptions-item label="*联系电话" prop="unit_test.phone" class="labelItem" style="" size="mini" required>
									<el-input v-model="unit_test.phone"></el-input>
				</el-descriptions-item>
				<el-descriptions-item label="*通讯地址" prop="unit_test.address" class="labelItem" style="" size="mini" required>
									<el-input v-model="unit_test.address"></el-input>
				</el-descriptions-item>
				<el-descriptions-item label="传真" prop="unit_test.fax" class="labelItem" style="" size="mini">
							<el-input v-model="unit_test.fax"></el-input>
				</el-descriptions-item>
			</el-descriptions>
		</el-descriptions-item>
	</el-descriptions>
		<!-- 生产单位情况 -->
		<el-descriptions :column="1" border style="" >
			<el-descriptions-item label="生产单位情况" >
				<el-descriptions  :column="1" border size="mini" style="width:100%" >
			<el-descriptions-item label="*单位名称" prop="unit_production.production_name" class="labelItem" style="" size="mini" required>
				<el-input v-model="unit_production.production_name"></el-input>
			</el-descriptions-item>
			<el-descriptions-item label="*联系人" prop="unit_production.user_name" class="labelItem" style="" size="mini" required>
				<el-input v-model="unit_production.user_name"></el-input>
			</el-descriptions-item>
			<el-descriptions-item label="*联系电话" prop="unit_production.phone" class="labelItem" style="" size="mini" required>
				<el-input v-model="unit_production.phone"></el-input>
			</el-descriptions-item>
			<el-descriptions-item label="*通讯地址" prop="unit_production.address" class="labelItem" style="" size="mini" required>
				<el-input v-model="unit_production.address"></el-input>
			</el-descriptions-item>
			<el-descriptions-item label="邮编" prop="unit_production.postcode" class="labelItem" style="" size="mini">
				<el-input v-model="unit_production.postcode"></el-input>
			</el-descriptions-item>

			<el-descriptions-item label="传真" prop="unit_production.fax" class="labelItem" style="" size="mini">
				<el-input v-model="unit_production.fax"></el-input>
			</el-descriptions-item>
			<el-descriptions-item label="生产方式" prop="unit_production.production_class" class="labelItem" style=""  required>
				<el-radio v-model="unit_production.production_class" label="自产">自产</el-radio>
				<el-radio v-model="unit_production.production_class" label="收购">收购</el-radio>
			</el-descriptions-item>
			</el-descriptions>
			</el-descriptions-item>
		</el-descriptions>


		<!-- 抽样单位情况 -->
		<el-descriptions :column="1" border style="">
			<el-descriptions-item label="抽样单位情况">
				<el-descriptions  :column="1" border size="mini" style="width:100%">
			<el-descriptions-item label="*单位名称" prop="unit_sample.unit_name" class="labelItem" style="" size="mini" required>
									<el-input v-model="unit_sample.unit_name" disabled></el-input>
			</el-descriptions-item>
			<el-descriptions-item label="*联系人" prop="unit_sample.unit_user" class="labelItem" style="" size="mini" required>
									<el-input v-model="unit_sample.unit_user"></el-input>
			</el-descriptions-item>
			<el-descriptions-item label="*联系电话" prop="unit_sample.phone" class="labelItem" style="" size="mini" required>
									<el-input v-model="unit_sample.phone"></el-input>
			</el-descriptions-item>
			<el-descriptions-item label="*通讯地址" prop="unit_sample.address" class="labelItem" style="" size="mini" required>
									<el-input v-model="unit_sample.address"></el-input>
			</el-descriptions-item>
			<el-descriptions-item label="邮编" prop="unit_sample.postcode" class="labelItem" style="" size="mini">
									<el-input v-model="unit_sample.postcode"></el-input>
			</el-descriptions-item>
			<el-descriptions-item label="传真" prop="unit_sample.fax" class="labelItem" style="" size="mini">
									<el-input v-model="unit_sample.fax"></el-input>
			</el-descriptions-item>
			<el-descriptions-item label="Email" prop="unit_sample.email" class="labelItem" style="" size="mini">
									<el-input v-model="unit_sample.email"></el-input>
			</el-descriptions-item>
			</el-descriptions>
			</el-descriptions-item>
		</el-descriptions>

		<el-descriptions  :column="2" border>
			<el-descriptions-item label="检测任务依据" prop="cyd_data.base" class="labelItem" style="" size="mini" required>
								<el-input v-model="cyd_data.base"></el-input>
			</el-descriptions-item>
			<el-descriptions-item  label="抽样时间" prop="cyd_data.sample_date" class="labelItem" style="" size="mini" required>
								<el-date-picker v-model="cyd_data.sample_date" type="date" value-format="yyyy-MM-dd">
								</el-date-picker>
			</el-descriptions-item>
			<el-descriptions-item  label="被抽检单位签字盖章附件" prop="cyd_data.file_url" class="labelItem" style="" size="mini">
								<el-upload class="upload-demo" :file-list="fileList" drag :headers="myHeaders"
									action="http://syjcapi.aielab.net/api/v1/ossUpload" :on-success="uploadSuccess" :limit="1">
									<i class="el-icon-upload"></i>
									<div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
								</el-upload>
			</el-descriptions-item>
		</el-descriptions>
<!-- 底部按钮 -->
		<el-form>
				<el-form-item class="btn">
					<el-button type="primary" @click="back()">返回任务详情</el-button>
					<el-button @click="resetForm()">重置</el-button>
					<el-button @click="onSubmit">保存，继续添加</el-button>
				</el-form-item>
			</el-form>
			<!-- <el-form ref="form1" :model="form" label-width="110px"
				style="margin-top: 20px;width: 48%;background-color: aliceblue;padding:10px 1px 1px 10px;">
			</el-form> -->
		</div>
		<!-- </el-card> -->
</template>

<script>
	const token = window.sessionStorage.getItem('token')
	import {
		provinceAndCityData,
		regionData,
		provinceAndCityDataPlus,
		regionDataPlus,
		CodeToText,
		TextToCode
	} from 'element-china-area-data'
	export default {
		name: 'inputSample',
		data() {
			return {
				myHeaders: {
					Authorization: 'bearer' + token
				},
				fileList: [],
				form: {},
				cyd_data: {},//抽样单信息上部分
				unit_test: {},//受检单位信息
				unit_production: {},//生产单位信息
				unit_sample: {},//抽样单位信息
				units: [],
				production: [],
				// 样品下拉框
				sampleList: [],
				task_id: '',
				undertake_id: '',
				// 产地
				options: regionData,
				selectedOptions: []
			}
		},
		created() {
			this.task_id = this.$route.params.task_id
			this.undertake_id = this.$route.params.under_id
			console.log(this.task_id)
			console.log(this.undertake_id)
			this.getTestObj()
			console.log(this.regionData)
		},
		methods: {

			// 获取样本列表
			async getTestObj() {
				const result = await this.$http.post('getTestObj', {
					task_id: this.task_id,
					undertake_id: this.undertake_id
				})
				console.log(result.data.data.sample_info.name)
				if (result.data.code == 0) {
					this.sampleList = result.data.data.list
					this.unit_sample.unit_name = result.data.data.sample_info.name
				}
				console.log(this.unit_sample.unit_name)
			},
			// 生成样品编码
			async createCode() {
				var that = this
				const result = await this.$http.post('getSampleId', {
					task_id: this.task_id
				})
				console.log(result.data.data.id)
				if (result.data.code == 0) {
					that.cyd_data.sample_id = result.data.data.id
					this.getTestObj()
				}
			},
			// 获取选择产地
			handleChange() {
				var that = this
				var loc = "";
				// console.log(this.form.place)
				for (let i = 0; i < this.selectedOptions.length; i++) {
					loc += CodeToText[this.selectedOptions[i]];
				}
				// console.log(loc)
				that.cyd_data.place = loc
				console.log(that.cyd_data.place)
			},
			// 上传成功(要确认数据)
			uploadSuccess(res) {
				console.log(res.data)
				this.cyd_data.file_url = res.data.fileUrl
				this.cyd_data.file_name = res.data.fileName
			},
			async onSubmit() {
				console.log(this.cyd_data.sample_name)
				var that = this
				console.log(that.cyd_data)
				that.cyd_data.task_id = that.task_id
				that.cyd_data.undertake_id = that.undertake_id
				that.form.cyd_data = that.cyd_data
				that.form.unit_test = that.unit_test
				that.form.unit_production = that.unit_production
				that.form.unit_sample = that.unit_sample
				console.log(that.form)
				const result = await this.$http.post('saveSample', this.form)
				if (result.data.code == 0) {
					// that.form.sample_id = result.data.data.id
					this.$message({
						type: 'success',
						message: '提交抽样单成功!'
						
					});
				}
			},
			// 重置表单
			resetForm() {
				// console.log(this.form)
					this.cyd_data = '';
					this.unit_test = '';
					this.unit_production = '';
					this.unit_sample = '';
					this.selectedOptions = '';
			},
			// 返回
			back() {
				var that = this
				console.log(that.task_id)
				this.$router.push({
					name: 'setTaskcd',
					params: {
						id: that.task_id
					}
				})
			}
		},
	}
</script>

<style lang="less" scoped>
	.form {
		display: flex;
		flex-direction: row;
		justify-content: center;
		align-content: flex-start;
	}

	.el-descriptions {
		width: 80%;
		margin: auto;
	}

	/deep/ .el-descriptions__title {
		margin: auto;
		font-size: 1.25rem;
		// font-weight: 31.25rem !important;
	}
	.btn{
		margin-top:20px ;
		// float: right;
		text-align: center;
	}
</style>
