<template>
	<div class="content-main grainStoragePage">
		<el-tabs type="card" v-model="activeName" @tab-click="activeClick">
			<el-form
				ref="curform"
				:model="curform"
				label-width="80px"
				class="search-bm-boder"
				:inline="true"
			>
				<el-row>
					<el-form-item label="申请类别:" prop="zglxDm">
						<el-select
							v-model="curform.zglxDm"
							style="width:170px"
							clearable
							placeholder="申请类别"
						>
							<el-option
								v-for="item in options"
								:key="item.dictCode"
								:label="item.dictName"
								:value="item.dictCode"
							></el-option>
						</el-select>
					</el-form-item>
					<el-form-item label="申请类型:" prop="zgsqlx">
						<el-select
							v-model="curform.zgsqlx"
							style="width:170px"
							clearable
							placeholder="申请类型"
						>
							<el-option
								v-for="item in optionss"
								:key="item.dictCode"
								:label="item.dictName"
								:value="item.dictCode"
							></el-option>
						</el-select>
					</el-form-item>

					<el-form-item label="状态:" prop="sqzt">
						<el-select
							v-model="curform.sqzt"
							style="width:170px"
							clearable
							placeholder="状态"
						>
							<el-option
								v-for="item in optionp"
								:key="item.dictCode"
								:label="item.dictName"
								:value="item.dictCode"
							></el-option>
						</el-select>
					</el-form-item>
					<el-form-item>
						<div class="search-button">
							<span class="search-line"></span>

							<el-button
								type="primary"
								size="small"
								class="iconfont iconchaxun"
								@click="queryFun"
								>查询</el-button
							>
							<el-button
								type="primary"
								class="iconfont iconzhongzhi"
								@click="resetFun"
								size="small"
								>重置</el-button
							>
							<!-- <el-button
								type="primary"
								@click="onSearch"
								size="small"
								class="iconfont iconchaxun"
								>查询</el-button
							>
							<el-button
								type="primary"
								@click="
									formInline = {}
									onSearch()
								"
								size="small"
								class="iconfont iconzhongzhi"
								>重置</el-button
								>-->
						</div>
					</el-form-item>
				</el-row>
			</el-form>

			<el-row>
				<el-col :span="24">
					<el-table
						:data="tableList"
						@row-click="tableRowClick"
						v-loading="loadingTable"
						border
						stripe
						highlight-current-row
						style="margin-top: 10px;"
					>
						<el-table-column align="center" label="序号" width="60">
							<template slot-scope="scope">
								<span>{{ scope.$index + 1 }}</span>
							</template>
						</el-table-column>
						<el-table-column
							prop="qymc"
							label="企业名称"
							align="center"
							:show-overflow-tooltip="true"
						>
							<template slot-scope="scope">
								<span>{{
									scope.row.zglxDm == 1
										? '粮食类'
										: '食用植物油类'
								}}</span>
							</template>
						</el-table-column>
						<el-table-column
							prop="qyfr"
							label="企业法人"
							align="center"
							:show-overflow-tooltip="true"
						>
							<template slot-scope="scope">
								<span v-if="scope.row.zgsqlx == 1"
									>资格认证</span
								>
								<span v-if="scope.row.zgsqlx == 2"
									>资格变更</span
								>
								<span v-if="scope.row.zgsqlx == 3"
									>资格延续</span
								>
							</template>
						</el-table-column>
						<el-table-column
							prop="zglxDm"
							label="申请类别"
							align="center"
							:show-overflow-tooltip="true"
							width="300"
						>
							<template slot-scope="scope">
								<span>{{
									scope.row.zglxDm == 1
										? '新申请'
										: '补充申请'
								}}</span>
							</template>
						</el-table-column>
						<el-table-column
							prop="sqlx"
							label="申请类型"
							align="center"
							:show-overflow-tooltip="true"
						>
							<template slot-scope="scope">
								<span>{{
									scope.row.kzzd4 ? scope.row.kzzd4 : '--'
								}}</span>
							</template>
						</el-table-column>
						<el-table-column
							label="承储资格有效期"
							align="center"
							:show-overflow-tooltip="true"
						>
							<template slot-scope="scope">
								<span>{{ dateFormat(scope.row.tjspsj) }}</span>
							</template>
						</el-table-column>
						<el-table-column
							prop="zt"
							label="状态"
							align="center"
							:show-overflow-tooltip="true"
						>
							<template slot-scope="scope">
								<span v-if="scope.row.sqzt == 0">草稿</span>
								<span
									v-if="
										scope.row.sqzt == 1 && !scope.row.flag
									"
									>审批中</span
								>
								<span
									v-if="scope.row.sqzt == 1 && scope.row.flag"
									>待审核</span
								>
								<span v-if="scope.row.sqzt == 2">审批通过</span>
								<span v-if="scope.row.sqzt == 3"
									>审批未通过</span
								>
								<span v-if="scope.row.sqzt == 4">撤销资格</span>
								<span v-if="scope.row.sqzt == 5">打回修改</span>
							</template>
						</el-table-column>
						<el-table-column
							label="操作"
							align="center"
							width="180"
						>
							<template slot-scope="scope">
								<el-tooltip
									class="item"
									effect="light"
									content="查看"
									placement="top"
								>
									<el-button
										type="text"
										size="small"
										class="operate_show_btn iconfont iconchakan2"
										@click="seeZg(scope.row)"
									></el-button>
								</el-tooltip>
								<el-tooltip
									class="item"
									effect="light"
									content="审核"
									placement="top"
									v-if="scope.row.flag == true"
								>
									<el-button
										type="text"
										size="small"
										class="operate_show_btn iconfont iconshenhe"
										@click.stop="modifyZg(scope.row)"
									></el-button>
								</el-tooltip>
							</template>
						</el-table-column>
					</el-table>
					<div class="pag-right clearFix">
						<el-pagination
							class="right"
							@size-change="handleSizeChange"
							@current-change="handleCurrentChange"
							:current-page.sync="page.page"
							:page-sizes="[10, 20, 30, 40]"
							:page-size="page.size"
							layout="total, sizes, prev, pager, next, jumper"
							:total="page.total"
							v-if="paginationShow"
						></el-pagination>
					</div>
				</el-col>
			</el-row>
			<el-dialog
				title="资格认证申请"
				:visible.sync="dialogTyle"
				width="50%"
				center
				:close-on-click-modal="false"
			>
				<el-row>
					<el-form label-width="110px">
						<el-row>
							<el-col :span="20" :offset="2">
								<el-form-item
									label="请选择申请类型"
									class="botItemStyle"
								>
									<el-radio v-model="zglxDm" label="1"
										>粮食类承储资格</el-radio
									>
								</el-form-item>
							</el-col>
						</el-row>
						<el-row>
							<el-col :span="20" :offset="2">
								<el-form-item label class="botItemStyle">
									<el-radio
										v-model="zglxDm"
										label="2"
										disabled
										>食用植物油承储资格（暂未开放申请）</el-radio
									>
								</el-form-item>
							</el-col>
						</el-row>
					</el-form>
				</el-row>
				<span slot="footer" class="dialog-footer">
					<el-button
						type="primary"
						@click="saveType(1)"
						class="outCancelButton"
						:loading="loadingType"
						>保 存</el-button
					>
					<el-button
						type="primary"
						@click="dialogTyle = false"
						class="outCancelButton"
						>取 消</el-button
					>
				</span>
			</el-dialog>
		</el-tabs>
	</div>
</template>
<script>
import {
	getQualificationStatus,
	getCompanyInfo,
	saveQualification,
	getZgStatistics,
	dictionaryFun
} from '@/api/grainCoManage/warehousingLogistics/storageQualific'
import { optionss, options, optionp } from './ztoo3.js'
export default {
	data() {
		return {
			options: options,
			optionss: optionss,
			optionp: optionp,
			searchShowMoreBool: false,
			activeName: '承储资格申请',
			typeZgData: [],
			typeYwData: [], //申请业务类型集合
			applyStatusData: [],
			datePicker: [],
			paginationShow: true,
			tableList: [],
			loadingTable: false,

			page: {
				page: 1,
				size: 10,
				total: 0
			},
			curform: {
				kzzd3: '企业端', //判断草稿状态列表是否放行，企业端放行；管理端不放行（传值代表管理端）
				zglxDm: '', //承储资格类型
				zgsqlx: '', //申请业务类型
				sqzt: '', //审批状态
				kzzd1: '', //申请仓容
				startDate: '', //申请日期开始日期
				endDate: '' //申请日期结束日期
			},
			pageChange: false,
			initArr: [],
			areaCode: '',
			dialogTyle: false,
			loadingType: false,
			creditCode: '', //企业信用代码
			zglxDm: '1', //资格类型(粮食类或者食用植物油类):1是粮食，2是植物油
			zgsqlx: '', //资格申请类型：1-资格认证；2-资格变更；3-资格延续
			qyId: '',

			formData: {
				//几个tab的大对象
				companyName: '', //企业名称
				creditCode: '', //统一社会信用代码
				legalName: '', //法人代表
				officePhone: '', //联系电话
				phone: '', //手机号码
				email: '', //电子信箱
				fax: '', //传真号码
				postalCode: '', //邮政编码
				companyAddress: '', //通讯地址
				unitOfNatureName: '', //企业性质
				businessVarietyName: [], //经营范围
				unitPrimaryServiceName: [], //主营业务
				registerDate: '', //注册时间
				registerMoney: '', //注册资本（万元）
				totalNum: '', //企业总人数
				storeShouldForm: {
					storeType: [], //企业收储粮食主要品种
					tableGrain: [] //当前储粮品种表格
				},
				editableTabs: [
					{
						tabName: '主库区',
						formStoreHouse: {
							nameKq: '', //库区名称
							totalArea: '', //总仓容
							applyArea: '', //申请仓容
							storeArea: '', //库区占地面积
							isRoad: '0', //有无铁路专用线
							isWharf: '0', //有无水运码头
							roadClose: '', //紧邻公路类别
							roadDistance: '', //与紧邻公路距离（公里）
							isFlood: '0', //防洪标准是否不低于50年一遇
							isHarmful: '0', //周围是否无污染源及易燃易爆场所
							isWater: '0', //有无消防水源
							isFire: '0', //有无消防设施
							isScale: '0', //有无汽车衡
							isAssay: '0' //有无化验室
						}
					}
				], //库区基本情况
				tableHouse: [], //仓房基本情况
				grainTechnologyForm: {
					//主要储粮技术应用说明
					technologyExplain1: '',
					technologyExplain2: '',
					technologyExplain3: ''
				},
				checkForm: {
					//企业检化验条件表表格上边的表单
					isCheckRoom: '0', //有无专用检化验室
					isCheckPz: '0', //能否检测粮食质量等级和储存品质
					checkArea: '', //检化验室建筑面积
					isCheckSafe: '0', //能否筛查粮食主要食品安全指标
					tableDevice: [] //企业检化验条件表表格
				},
				tablePersonForm: {
					tableBgPerson: [] //主要专职保管人员
				},
				tablePeopleForm: {
					tableCheckPerson: [] //主要专职检化验人员
				},
				mainStoreForm: {
					tableStoreDevi: [] //主要仓储设备
				},
				storeManagerForm: {
					//企业仓储管理情况
					isManagerBL: '0', //“一符四无”粮仓比例是否达到100%
					isManagerPd: '', //评定单位
					isManagerSafe: '1', //两年内是否没有发生储粮安全事故
					isManagerSafeSg: '', //两年内是否没有发生储粮安全事故：事故情况
					isManagerSafeDeal: '', //两年内是否没有发生储粮安全事故：处理决定
					isManagerSc: '1', //两年内是否没有发生安全生产事故
					isManagerScSg: '', //两年内是否没有发生安全生产事故；事故情况
					isManagerScDeal: '', //两年内是否没有发生安全生产事故；处理决定
					isManagerFg: '1', //两年内是否没有发生违反国家政策法规记录
					isManagerFgQk: '', //违反政策法规情况
					isManagerFgDeal: '' //两年内是否没有发生违反国家政策法规记录：处理决定
				},
				financingForm: {
					//企业财务状况
					mainNote: '', //近两年是否有重大经营性亏损及其说明
					tableFinancingData: [{}, {}] //近两年财务报表
				}
			}
		}
	},
	created() {
		this.optionss.forEach((item, index) => {
			if (this.curform.sqlx == item.dictName) {
				this.curform.sqlxbm = item.dictCode
			}
		})
		this.options.forEach((item, index) => {
			if (this.curform.sqlb == item.dictName) {
				this.curform.sqlbbm = item.dictCode
			}
		})
		this.optionp.forEach((item, index) => {
			if (this.curform.zt == item.dictName) {
				this.curform.ztbm = item.dictCode
			}
		})
		this.areaCode = sessionStorage.getItem('areaCode')
		this.creditCode = sessionStorage.getItem('tyshxydm') //信用代码
		//tab位置
		const caseTabMark = sessionStorage.getItem('caseTabMark')
		if (caseTabMark != '承储资格申请' && caseTabMark != '查看资格') {
			this.activeName = '承储资格申请'
			this.getList() //检测标准列表
		} else {
			this.activeName = caseTabMark
			if (caseTabMark == '承储资格申请') {
				this.getList()
			} else if (caseTabMark == '查看资格') {
				this.getInitInfo()
			}
		}
		this.getCheckType(8041, 'typeZgData')
		this.getCheckType(8042, 'typeYwData')
		this.getCheckType(8043, 'applyStatusData')
	},
	methods: {
		activeClick(val) {
			sessionStorage.setItem('caseTabMark', val.name)
			if (val.name == '承储资格申请') {
				this.resetFun()
				this.queryFun()
			} else if (val.name == '查看资格') {
				this.getInitInfo()
			}
		},
		getList() {
			this.curform.startDate = this.datePicker ? this.datePicker[0] : ''
			this.curform.endDate = this.datePicker ? this.datePicker[1] : ''

			this.loadingTable = true
			getZgStatistics(this.curform, this.page).then((res) => {
				if (res.infoCode == 200) {
					console.log('sfddssd', res)
					this.tableList = res.data.resultList
					this.page.total = res.data.total
				} else {
					this.$message({
						type: 'error',
						message: res.msg,
						duration: 2000
					})
				}
				this.loadingTable = false
			})
		},
		queryFun() {
			//查询
			this.paginationShow = false
			this.handleCurrentChange(1)
			this.$nextTick(() => {
				this.paginationShow = true
			})
		},
		resetFun() {
			//重置
			;(this.curform = {
				kzzd3: '企业端', //判断草稿状态列表是否放行，企业端放行；管理端不放行（传值代表管理端）
				zglxDm: '', //承储资格类型
				zgsqlx: '', //申请业务类型
				sqzt: '', //审批状态
				kzzd1: '', //申请仓容
				startDate: '', //申请日期开始日期
				endDate: '' //申请日期结束日期
			}),
				(this.datePicker = [])
			this.queryFun()
		},
		tableRowClick(row, column, event) {
			if (event.label != '操作') {
				// this.seeZg(row)
			}
		},
		seeZg(row) {
			this.$router.push({
				path:
					'/grainCoManage/warehousingLogistics/storageQualific/details',
				query: {
					id: row.id,
					zglxDm: row.zglxDm, //申请类型
					zgzbh: row.zgzbh, //证书编号
					lcslId: row.lcslId, //流程id
					sqzt: row.sqzt, //申请状态
					yxqkssj: row.yxqkssj, //有效期开始时间
					yxqjzsj: row.yxqjzsj, //有效期结束时间
					kzzd1: row.kzzd1, //总仓容
					kzzd4: row.kzzd4, //仓房编号
					cxyy: row.cxyy, //撤销原因
					from: 'zgApply',
					state: 'sees'
				}
			})
		},
		modifyZg(row) {
			this.$router.push({
				path:
					'/grainCoManage/warehousingLogistics/storageQualific/details',
				query: {
					id: row.id,
					zglxDm: row.zglxDm, //申请类型
					zgzbh: row.zgzbh, //证书编号
					lcslId: row.lcslId, //流程id
					sqzt: row.sqzt, //申请状态
					taskId: row.taskId,
					taskName: row.taskName,
					yxqkssj: row.yxqkssj, //有效期开始时间
					yxqjzsj: row.yxqjzsj, //有效期结束时间
					kzzd1: row.kzzd1, //总仓容
					kzzd4: row.kzzd4, //仓房编号
					cxyy: row.cxyy, //撤销原因
					from: 'zgApplyshenhe',
					state: 'shenhe'
				}
			})
			// if (row.sqzt == 5) {
			// 	//5是打回修改
			// 	this.$router.push({
			// 		path: '/basq/toDoQualification',
			// 		query: {
			// 			id: row.id,
			// 			zglxDm: row.zglxDm, //申请类型
			// 			lcslId: row.lcslId, //流程id
			// 			sqLx: row.zgsqlx, //资格申请类型：1-资格认证等
			// 			sqzt: row.sqzt,
			// 			creatorCode: row.creatorCode, //发起人信用代码
			// 			from: 'zgApply'
			// 		}
			// 	})
			// } else {
			// 	//草稿状态时：0
			// 	this.$router.push({
			// 		path: 'qualificationApply',
			// 		query: {
			// 			state: 'upData',
			// 			id: row.id,
			// 			zgType: row.zglxDm, //资格类型(粮食类或者食用植物油类)
			// 			sqLx: row.zgsqlx //资格申请类型：1-资格认证等
			// 		}
			// 	})
			// }
		},
		handleSizeChange(val) {
			//条数改变
			this.page.size = val
			this.getList()
		},
		handleCurrentChange(val) {
			//页数改变
			this.page.page = val
			this.getList()
		},
		getCheckType(num, curArr) {
			//获取检验类别
			dictionaryFun(num).then((res) => {
				if (res.infoCode == 200) {
					this[curArr] = res.data
				} else {
					this.$message({
						message: res.msg,
						type: 'error',
						duration: 2000
					})
				}
			})
		},
		goZgApply() {
			this.loadingType = false
			this.dialogTyle = true
		},
		getInitInfo() {
			getQualificationStatus(this.areaCode).then((res) => {
				if (res.infoCode == 200) {
					// console.log('初始信息',res.data)
					this.initArr = res.data
					if (res.data.length == 0) {
						this.pageChange = true
					} else {
						this.pageChange = false
					}
				} else {
					this.$message({
						message: res.msg,
						type: 'error',
						duration: 2000
					})
				}
			})
		},
		getCompanyFun() {
			//获取企业基本信息
			getCompanyInfo(this.creditCode).then((res) => {
				if (res.infoCode == 200) {
					this.qyId = res.data.id //企业id
					this.formData.companyName = res.data.qymc //企业名称
					this.formData.creditCode = res.data.tyshxydm //统一社会信用代码
					this.formData.legalName = res.data.fddbr //法人代表
					this.formData.officePhone = res.data.bgdh //联系电话
					this.formData.phone = res.data.frsjh //手机号码
					this.formData.email = res.data.qydzyx //电子信箱
					this.formData.fax = res.data.qyczhm //传真号码
					this.formData.postalCode = res.data.qyyb //邮政编码
					this.formData.companyAddress = res.data.qytxdz //通讯地址
					this.formData.unitOfNatureName = res.data.qyxz //企业性质
					this.formData.businessVarietyName = res.data.jyfw //经营范围
					this.formData.unitPrimaryServiceName = res.data.qyzyyw //主营业务
					this.formData.registerDate = res.data.qyzcsj //注册时间
					this.formData.registerMoney = res.data.zczb //注册资本（万元）
					this.formData.totalNum = res.data.qycyrys //企业总人数
					this.saveCommonFun()
				} else {
					this.$message({
						message: res.msg,
						type: 'error',
						duration: 2000
					})
					this.loadingType = false
				}
			})
		},
		saveCommonFun() {
			//新增承储资格（保存和暂存调的方法）--此时是暂存
			const needArray = {
				sqxx: JSON.stringify(this.formData),
				status: 1,
				sqzt: 0, //申请状态(保存或者暂存传的字段)---0为暂存
				zglxDm: this.zglxDm, //资格类型(粮食类或者食用植物油类)
				zgsqlx: this.zgsqlx, //资格申请类型：1-资格认证；2-资格变更；3-资格延续
				qyId: this.qyId
			}
			saveQualification(needArray).then((res) => {
				if (res.infoCode == 200) {
					this.$message({
						message: res.msg,
						type: 'success',
						duration: 2000
					})
					console.log('sfdsdfsd', res.data)
					this.dialogTyle = false
					this.getList()
				} else {
					this.$message({
						message: res.msg,
						type: 'error',
						duration: 2000
					})
					this.loadingType = false
				}
			})
		},
		saveType(num) {
			this.zgsqlx = num //资格申请类型：1-资格认证
			this.loadingType = true
			this.getCompanyFun() //获取企业基本信息
		},
		goSee(item) {
			this.$router.push({
				path: 'cczgsq-detail',
				query: {
					id: item.id,
					pageTit:
						item.zglxDm == 1
							? '粮食类承储资格'
							: '食用植物油承储资格'
				}
			})
		},
		goModify(item) {
			this.$router.push({
				path: 'qualificationApply',
				query: {
					state: 'upData',
					id: item.id,
					zgType: item.zglxDm, //资格类型(粮食类或者食用植物油类)
					sqLx: item.zgsqlx //资格申请类型：1-资格认证等
				}
			})
		},
		goAdd() {
			this.$router.push({
				path: 'qualificationApply',
				query: { state: 'add' }
			})
		},
		goProcess(item) {
			//查看审批进度
			this.$router.push({
				path: 'seeSpProcess',
				query: {
					zgType: item.zglxDm,
					lcslId: item.lcslId
				}
			})
		},
		goLook(item) {
			//已获得资格及已经撤销资格的查看详情
			this.$router.push({
				path: 'cczgsq-detail',
				query: {
					id: item.id,
					pageTit:
						item.zglxDm == 1
							? '粮食类承储资格'
							: '食用植物油承储资格',
					zgzbh: item.zgzbh, //证书编号
					startDate: item.yxqkssj, //有效期开始时间
					endDate: item.yxqjzsj, //有效期结束时间
					kzzd1: item.kzzd1, //总仓容
					kzzd4: item.kzzd4, //仓房编号
					sqzt: item.sqzt, //审批状态
					cxyy: item.cxyy //撤销原因
				}
			})
		},
		goChange() {
			//申请变更
		},
		goLookReport() {
			//查看变更报告
		},
		// 时间格式化函数
		dateFormat(time) {
			if (time) {
				var day = time.split('T')
				//获取日期部分的年月日
				var days = day[0].split('-')
				return days[0] + '-' + days[1] + '-' + days[2]
			} else {
				return '--'
			}
		}
	}
}
</script>
<style lang="scss" scoped>
.iconfont {
	color: white;
}
.grainStoragePage {
	.noText {
		font-size: 18px;
		color: #888;
	}
	.botItemStyle.el-form-item {
		margin-bottom: 0px;
	}
	.el-dialog--center .el-dialog__body {
		padding-bottom: 20px;
	}
	.outButtonSty {
		padding: 9.7px 12px;
	}
	.imgStyle {
		vertical-align: top;
	}
	.imgStyle1 {
		position: relative;
		top: 1.5px;
	}
	.mainStyle {
		text-align: center;
		font-size: 16px;
	}
	.fontSty {
		font-size: 14px;
	}
	.el-select {
		width: 100%;
	}
	.itemStyle .el-form-item__label {
		line-height: 20px;
	}
	.el-date-editor .el-range-separator {
		width: 8%;
	}
}
</style>
