<template>
	<div>
		<base-page
			:searchParams="searchParams"
			:tableParams="tableParams"
			:tableUrl="tableUrl"
			:addUrl="addUrl"
			:editUrl="editUrl"
			:delUrl="delUrl"
			:editAuth="editAuth"
			:delAuth="delAuth"
			:infoUrl="infoUrl"
			:exportUrl="exportUrl"
			:importUrl="importUrl"
			:mubanUrl="mubanUrl"
			:editParams="editParams"
			:editElseRules="editElseRules"
			:editBtnName="(item) => (!item.latitude ? '待完善' : '修改')"
			idName="icDangerousChemicalsId"
		></base-page>
	</div>
</template>

<script>
import basePage from "@/views/modules/cpts/base/index";
import { getItemByIdInCascader, collapse } from "@/utils/cascader";

export default {
	props: {},

	data() {
		return {
			searchParams: [
				{
					field: "所属组织",
					keyName: "orgId",
					type: "cascader",
					value: () => [],
					supKeys: ["orgType"],
					supValues: [""],
					optionUrl: "/gov/org/customeragency/agencygridtreeV2",
					optionUrlParams: {
						customerId: this.$store.state.user.customerId,
					},
					optionList: [],
					optionProps: {
						multiple: false,
						value: "agencyId",
						label: "agencyName",
						children: "subAgencyList",
						checkStrictly: true,
						emitPath: false,
					},
					optionCook(obj) {
						return [obj];
					},
					handleChangeFn(val, item, that) {
						const { optionList } = item;
						const optionPlaneList = collapse(
							optionList,
							"subAgencyList"
						);
						if (val) {
							let selectedItem = getItemByIdInCascader(
								optionPlaneList,
								[val],
								"agencyId",
								"subAgencyList"
							)[0];
							item["supValues"][0] =
								selectedItem.level == "grid"
									? "grid"
									: "agency";
							console.log("handleChangeFn", selectedItem);
						} else {
							item["supValues"][0] = "";
						}
					},
				},
				{ field: "企业名称", keyName: "name", type: "input" },
				{
					field: "企业类别",
					keyName: "category",
					type: "select",
					optionUrl:
						"/gov/org/coverage/dict-select-list/dangerous_chemicals",
					optionUrlParams: {},
					optionList: [],
				},
				{ field: "企业地址", keyName: "address", type: "input" },
				{ field: "联系人", keyName: "principalName", type: "input" },
				{
					field: "安全负责人",
					keyName: "securityPrincipalName",
					type: "input",
				},
			],

			tableParams: [
				{ field: "序号", keyName: "", type: "no" },
				{ field: "所属组织", keyName: "orgName", type: "text" },
				{ field: "企业名称", keyName: "name", type: "text" },
				{ field: "企业类别", keyName: "categoryName", type: "text" },
				{ field: "企业地址", keyName: "address", type: "text" },
				{ field: "联系人", keyName: "principalName", type: "text" },
				{ field: "联系电话", keyName: "principalMobile", type: "text" },
				{
					field: "安全负责人",
					keyName: "securityPrincipalName",
					type: "text",
				},
				{
					field: "安全负责人电话",
					keyName: "securityPrincipalMobile",
					type: "text",
				},
			],
			tableUrl: "/gov/org/icDangerousChemicals/list",
			mubanUrl: "/gov/org/icDangerousChemicals/download",
			importUrl: "/gov/org/icDangerousChemicals/import",
			exportUrl: "/gov/org/icDangerousChemicals/export",

			addUrl: "/gov/org/icDangerousChemicals/add",
			editUrl: "/gov/org/icDangerousChemicals/edit",
			infoUrl: "/gov/org/icDangerousChemicals/detail",
			delUrl: "/gov/org/icDangerousChemicals/del",
			editAuth(item) {
				return item.agencyId == this.$store.state.user.agencyId;
			},
			delAuth(item) {
				return item.agencyId == this.$store.state.user.agencyId;
			},

			editParams: [
				{
					field: "所属组织",
					keyName: "orgId",
					type: "cascader",
					value: () => [],
					supKeys: ["orgType"],
					supValues: [""],
					optionUrl: "/gov/org/customeragency/agencygridtreeV2",
					optionUrlParams: {
						customerId: this.$store.state.user.customerId,
					},
					optionList: [],
					optionProps: {
						multiple: false,
						value: "agencyId",
						label: "agencyName",
						children: "subAgencyList",
						checkStrictly: true,
						emitPath: false,
					},
					optionCook(obj) {
						return [obj];
					},
					handleChangeFn(val, item, that) {
						const { optionList } = item;
						const optionPlaneList = collapse(
							optionList,
							"subAgencyList"
						);
						if (val) {
							let selectedItem = getItemByIdInCascader(
								optionPlaneList,
								[val],
								"agencyId",
								"subAgencyList"
							)[0];
							that.fmData[item["keyName"]] =
								selectedItem.agencyId;
							that.fmData[item["supKeys"][0]] =
								selectedItem.level == "grid"
									? "grid"
									: "agency";
							console.log("handleChangeFn", selectedItem);
						} else {
							that.fmData[item["keyName"]] = "";
							that.fmData[item["supKeys"][0]] = "";
						}
					},
					rules: [
						{
							required: true,
							message: "所属组织不能为空",
							trigger: "blur",
						},
					],
				},
				{
					field: "企业名称",
					keyName: "name",
					type: "input",
					maxlength: 50,
					editDisabled: false,
					rules: [
						{
							required: true,
							message: "企业名称不能为空",
							trigger: "blur",
						},
					],
				},
				{
					field: "企业类别",
					keyName: "category",
					type: "select",
					optionUrl:
						"/gov/org/coverage/dict-select-list/dangerous_chemicals",
					optionUrlParams: {
						// dictType: "ic_service_type",
					},
					optionList: [],
					editDisabled: false,
					rules: [
						{
							required: true,
							message: "企业类别不能为空",
							trigger: "blur",
						},
					],
				},

				{
					field: "是否重点安全企业",
					keyName: "securityFlag",
					type: "select",
					optionUrl: "",
					optionUrlParams: {},
					optionList: [
						{
							value: true,
							label: "是",
						},
						{
							value: false,
							label: "否",
						},
					],
					editDisabled: false,
					rules: [
						{
							required: true,
							message: "是否重点安全企业不能为空",
							trigger: "blur",
						},
					],
				},
				{
					field: "联系人",
					keyName: "principalName",
					type: "input",
					maxlength: 50,
				},
				{
					field: "联系电话",
					keyName: "principalMobile",
					type: "input",
					maxlength: 50,
				},
				{
					field: "安全负责人",
					keyName: "securityPrincipalName",
					type: "input",
					maxlength: 50,
				},
				{
					field: "安全负责人电话",
					keyName: "securityPrincipalMobile",
					type: "input",
					maxlength: 50,
				},
				{
					field: "包企领导",
					keyName: "bqLd",
					type: "input",
					maxlength: 50,
				},
				{
					field: "包企干部",
					keyName: "bqGb",
					type: "input",
					maxlength: 50,
				},
				{
					field: "企业地址",
					keyName: "address",
					type: "address",
					maxlength: 50,
					supKeys: ["longitude", "latitude"],
					supValues: ["", ""],
					rules: [
						{
							required: true,
							message: "经营地址坐标不能为空",
							trigger: "blur",
						},
					],
				},
				{
					field: "企业介绍",
					keyName: "companyProfile",
					type: "textarea",
				},
			],
			editElseRules: {
				longitude: {
					type: "number",
					required: true,
					message: "请拖动地图选择坐标点",
				},
			},
		};
	},
	components: { basePage },
	computed: {},
	watch: {},

	async mounted() {},

	methods: {},
};
</script>

<style lang="scss" scoped></style>
