<template>
	<div class="content-main">
		<el-form inline class="search-content" label-width="72px" :model="searchForm">
			<el-row>
				<el-form-item label="单位名称:">
					<el-input clearable placeholder="请输入" v-model="searchForm.qymc"></el-input>
				</el-form-item>
				<el-form-item label="企业经营类型:" label-width="90ox">
					<el-select clearable placeholder="请选择" v-model="searchForm.jyfw">
						<el-option v-for="(item, i) in jyfwOption" :key="i" :value="item.value" :label="item.label"></el-option>
					</el-select>
				</el-form-item>
				<el-form-item label="隶属于:">
					<el-cascader
						clearable
						class="inputCompatible"
						:options="tree"
						:props="defaultParams"
						:change-on-select="true"
						:show-all-levels="false"
						@change="handleTreeChange"
						v-model="searchForm.sslsxzglbm"
					></el-cascader>
				</el-form-item>
				<div class="search-button">
					<span class="search-line"></span>
					<div class="search-select" @click="searchShowMoreBool = !searchShowMoreBool">
						更多条件
						<i class="el-icon-arrow-up" v-show="searchShowMoreBool"></i>
						<i class="el-icon-arrow-down" v-show="!searchShowMoreBool"></i>
					</div>
					<el-button type="primary" @click="onSearch" size="small" class="iconfont iconchaxun">查询</el-button>
					<el-button
						type="primary"
						size="small"
						class="iconfont iconzhongzhi"
						@click="searchForm = {}, onSearch()"
					>重置</el-button>
				</div>
			</el-row>
			<el-row v-show="searchShowMoreBool">
				<el-form-item label="注册地:">
					<el-cascader
						change-on-select
						@expand-change="dqchange"
						:props="optionsprops"
						:options="diquoptions"
						:show-all-levels="false"
						v-model="searchForm.qyszqy"
					></el-cascader>
				</el-form-item>
			</el-row>
			<el-row class="solid"></el-row>
		</el-form>
		<el-table border :data="tableData" stripe v-loading="loading">
			<el-table-column type="index" label="序号" align="center" width="60"></el-table-column>
			<el-table-column prop="qymc" label="单位名称" align="center" show-overflow-tooltip></el-table-column>
			<el-table-column prop="jyfw" label="企业经营类型" align="center" show-overflow-tooltip></el-table-column>
			<el-table-column prop="qyxz" label="单位性质" align="center" show-overflow-tooltip></el-table-column>
			<el-table-column prop="sslsxzglbmmc" label="隶属于" align="center" show-overflow-tooltip></el-table-column>
			<el-table-column prop="qyszqy" label="注册地" align="center" show-overflow-tooltip></el-table-column>
			<el-table-column prop="fddbr" label="法定代表人" align="center" show-overflow-tooltip></el-table-column>
			<el-table-column prop="zcrq" label="创建时间" align="center" show-overflow-tooltip>
				<template slot-scope="scope">
					<span>{{ scope.row.createDate && scope.row.createDate.slice(0, 10) }}</span>
				</template>
			</el-table-column>
			<el-table-column prop="index" label="操作" align="center" width="150">
				<template slot-scope="scope">
					<span>
						<el-tooltip class="item" effect="light" content="查看" placement="top">
							<el-button
								type="primary"
								size="small"
								class="iconfont iconchakan2 operate_show_btn"
								@click="onDetail(scope.row, 2)"
							></el-button>
						</el-tooltip>
					</span>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination
			class="pagination-wrap"
			@size-change="handleSizeChange"
			@current-change="handleCurPageChange"
			:current-page="pageForm.pageNum"
			:page-sizes="[10, 20, 30, 40]"
			:page-size="100"
			layout="total, sizes, prev, pager, next, jumper"
			:total="pageForm.total"
		></el-pagination>
	</div>
</template>

<script>
import {list, tree ,getZcd} from "@/api/grainCirculationPrice/coInfo/index.js";
import {findByGroupId,getdqchilder,cityDetain} from "@/api/dict.js";
function formatData(list) {
	return list.map(item => {
		let newItem = {};
		newItem.label = item.orgName;
		newItem.value = item.orgName;
		newItem.code = item.orgCode;
		if (item.hasOwnProperty("children") && item.children.length > 0) {
			newItem.children = formatData(item.children);
		}
		return newItem;
	});
}
function getCascaderObj(val, opt) {
	return val.map(function(value, index, array) {
		for (var itm of opt) {
			if (itm.value == value) {
				opt = itm.children;
				return itm;
			}
		}
		return null;
	});
}
var states = {
	0: "未审核",
	1: "审核已通过",
	2: "驳回"
};
var dwlxObj = {
	1: "应急",
	2: "仓储",
	3: "加工",
	4: "物流",
	5: "配送"
};
export default {
	data() {
		return {
			jyfwOption: [{
				value: '粮油收储企业',
				label: '粮油收储企业'
			}, {
				value: '粮油批发市场',
				label: '粮油批发市场'
			}, {
				value: '超市',
				label: '超市'
			}, {
				value: '小麦粉加工企业',
				label: '小麦粉加工企业'
			}, {
				value: '大米加工企业',
				label: '大米加工企业'
			}, {
				value: '其他成品粮加工企业',
				label: '其他成品粮加工企业'
			}, {
				value: '食用植物油加工企业',
				label: '食用植物油加工企业'
			}, {
				value: '食品及副食酿造企业',
				label: '食品及副食酿造企业'
			}, {
				value: '制酒企业',
				label: '制酒企业'
			}, {
				value: '饲料企业',
				label: '饲料企业'
			}, {
				value: '养殖企业',
				label: '养殖企业'
			}, {
				value: '淀粉企业',
				label: '淀粉企业'
			}, {
				value: '酒精企业',
				label: '酒精企业'
			}, {
				value: '其他粮油深加工企业',
				label: '其他粮油深加工企业'
			}, {
				value: '粮油机械制造企业',
				label: '粮油机械制造企业'
			}, {
				value: '非填报指标',
				label: '非填报指标'
			}],
			defaultParams: {
				label: "label",
				value: "code",
				children: "children",
				name: "label",
				emitPath: false
			},
			searchShowMoreBool: false,
			loading: true,
			pageForm: {
				pageNum: 1,
				total: 0,
				pageSize: 10
			},
			statusOpt: [],
			status: [
				// { code: '91000013', name: '已通过' }, { code: '91000046', name: '自治区驳回' }, { code: '91000043', name: '盟市已通过' }, { code: '91000044', name: '盟市已驳回' }, { code: '91000041', name: '旗县已通过' }, { code: '91000042', name: '旗县已驳回' }, { code: '91000004', name: '待审核' // 已提交 }
			], // 状态
			searchForm: {
				qyszqy: null, // 状态
				sqrq: null, // 申请日期
				jyfw: ""
			},
			tableData: [],
			dwlx: [
				
			],
			tree: [],
			optionsprops: {
				label: "countyName",
				value: "countyCode",
				children: "children"
			},
			diquoptions: [
				{
					countyCode: "150000",
					countyName: "内蒙古",
					children: []
				}
			],
			zt: [
				{ name: "未审核", val: 0 },
				{ name: "审核已通过", val: 1 },
				{ name: "驳回", val: 2 }
			] // 状态
		};
	},
	created() {
		this.getList();
		this.initOpts();
		this.zcd();
		cityDetain().then(res => {
			if (res.infoCode == 200) {
				this.xjmc = res.data;
				res.data.forEach(item => {
					this.diquoptions[0].children.push({
						countyCode: item.countyCode,
						countyName: item.countyName,
						children: []
					});
				});
			}
		});
	},
	methods: {
		handleTreeChange(val) {
			var item = getCascaderObj(val, this.tree);
			var value = item[item.length - 1];
			this.formQuery.qylsqxname = value.value;
			this.formQuery.sslsxzglbm = value.code;
		},
		dqchange(val) {
			if (val[val.length - 1] != '150000') {
				let params = val[val.length - 1];
				getdqchilder(params).then(res => {
					if (res.infoCode == 200) {
						this.sjmc = res.data;
						res.data.forEach(msg => {
							delete msg.children;
						});
						// console.log();
						this.diquoptions[0].children.forEach(item => {
							if (item.countyCode == val[val.length - 1]) {
								item.children = res.data;
							}
						});
					}
				});
			}
			console.log(val);
		},
		handleTreeChange1(val) {
			var item = getCascaderObj(val, this.tree);
			var value = item[item.length - 1];
			this.formQuery.qylsqxname = value.value;
			this.formQuery.zt = value.code;
		},
		zcd(){
			getZcd({ code: '150000' }).then(res => {
				this.statusOpt = res.data
			});
		},
		initOpts() {
			// 企业类型
			findByGroupId('QYLX').then(res => {
				dwlxObj = {};
				this.dwlx = res.data.map(d => {
					dwlxObj[d.dictCode] = d.dictName;
					return { value: d.dictCode, label: d.dictName };
				});
			});
			tree({ orgCode: sessionStorage.orgCode }).then(res => {
				if (!res) {
					this.tree = [];
					return;
				}
				var arr = [];
				arr.push(res.data);
				this.tree = formatData(arr);
			});
		},
		
		onSearch() {
			this.pageForm.pageNum = 1;
			this.getList();
		},
		getList() {
			this.loading = true;
			let { pageNum, pageSize } = this.pageForm;
			let { zt } = this.searchForm;
			this.searchForm.symble = true;
			if (this.searchForm.qyszqy) {
				this.searchForm.qyszqybhs = [];
				this.searchForm.qyszqybhs.push(this.searchForm.qyszqy[this.searchForm.qyszqy.length - 1]);
			} else {
				// delete this.searchForm.qyszqy;
				delete this.searchForm.qyszqybhs;
			}
			// delete this.searchForm.qyszqy;
			list({
				...this.searchForm,
				pageNum,
				pageSize
				// zt: zt || null
			})
				.then(res => {
					this.loading = false;
					if (res.infoCode === 200) {
						this.pageForm.total = res.data.total;
						var list = res.data.list;
						list.forEach(item => {
							item.qyztmc = states[item.qyzt];
							
							if (item.tzQylxVOList) {
								var dwlxmc = [];
								for (var x in item.tzQylxVOList) {
									dwlxmc.push(item.tzQylxVOList[x].qylxmc);
								}
								item.dwlxmc = dwlxmc.join(',');
								
							}
						});
						this.tableData = list;
						return;
					} else {
						this.tableData = [];
						this.pageForm.total = 0;
					}
				})
				.catch(err => {
					this.loading = false;
				});
		},
		onDetail(row, state) {
			this.$router.push({
				path: '/circulate/coInfo/detail',
				query: {
					state: state,
					tyshxydm: row.tyshxydm,
					jhbm: row.jhbm,
					taskId: row.rwid,
					lcid: row.lcid,
					sszz: row.sszz
				}
			});
		},
		handleSizeChange(val) {
			this.pageForm.pageSize = val;
			this.getList();
		},
		handleCurPageChange(val) {
			this.pageForm.pageNum = val;
			this.getList();
		}
	}
};
</script>
<style lang="scss" scope>
.search-content {
	.search-button {
		margin-left: 10px !important;
		.search-line {
			float: left;
			height: 40px !important;
		}
		.search-select, .iconfont {
			float: left;
		}
		.search-select {
			margin-top: 2px;
		}
		.el-button {
			margin-top: 3px;
		}
	}
	.el-form-item {
		float: left;
	}
	.el-input, .el-select {
		width: 180px;
	}
	.el-date-editor {
		width: 250px;
	}
	/deep/ .el-input__inner {
		vertical-align: top;
	}
}
.pagination-wrap {
	margin-top: 10px !important;
	/deep/ .el-input {
		vertical-align: top;
	}
}
.solid {
	margin-bottom: 20px;
	border-top: 1px #75b5fa dashed;
}
</style>
