<template>
	<div>
		<base-layout ref="layout" @pageChange="pageChange" :buttonList="buttonList" :showTitle="false"
			@onFuncBtn="getFuncBtn" :isPage="true">
			<div slot="main">
				<div class="mainTable">
					<base-table ref="customtable" :border="true" :showIndex="false" :tabLoading.sync="tabLoading"
						:tableHeight="tableHeight" :tableData="tableData" :tableColumn="tableColumn" :slotrow="true"
						@radioChange="radioChange">
						<!-- 时间 -->
						<template v-slot:bm_CreateTime="{row}">
							<p>{{row.row.bm_CreateTime | timeSplit}}</p>
						</template>
					</base-table>
				</div>
			</div>
		</base-layout>
		<!-- 新增/编辑弹窗 -->
		<!-- <base-dialog :closeEscape="true" :showClose="true" :closeModal="false" :dialogVisible.sync="informationDialog"
			:footerShow="true" @handleConfirmClick="informationConfirmClick" class="userDialog" :title="selectStatus" width="95vw"
			top="1vh">
			<document-information ref="informationFrom"></document-information>
		</base-dialog> -->
		<form-construction :dialogVisible="informationDialog" @close="informationDialog = false"></form-construction>
		<!-- 代码生成弹窗 -->
		<base-dialog :closeEscape="true" :showClose="true" :closeModal="false" :dialogVisible.sync="gnerationDialog"
			:footerShow="true" class="userDialog" title="代码生成" @update:dialogVisible="codeGeneration" @handleConfirmClick="codeSaving" width="95vw" top="1vh">
			<code-gneration ref="gnerationFrom"></code-gneration>
		</base-dialog>
		<!-- 编码设置窗 -->
		<base-dialog :closeEscape="true" :showClose="true" :closeModal="false" :dialogVisible.sync="orderDialog"
			:footerShow="true" @handleConfirmClick="orderConfirmClick" class="userDialog" title="编码设置" width="600px"
			top="10vh">
			<order-Setting ref="orderSettingFrom" :radioId="radioID"></order-Setting>
		</base-dialog>
		<!-- 单号设置弹窗 -->
		<base-dialog :closeEscape="true" :showClose="true" :closeModal="false" :dialogVisible.sync="permissionDialog"
			:footerShow="true" @handleConfirmClick="permissionConfirmClick" class="userDialog" title="数据权限设置"
			width="700px" top="10vh">
			<data-permission ref="permissionFrom"></data-permission>
		</base-dialog>
	</div>
</template>

<script>
	import baseTable from "@/components/base/baseTable"
	import baseLayout from "@/components/base/baseLayout"
	import baseDialog from "@/components/base/BaseNewDialog/index.vue"
	import documentInformation from "./documentInformation"
	import dataPermission from "./dataPermission"
	import codeGneration from "./codeGneration"
	import orderSetting from "./orderSetting.vue"
	import configData from "./configData"
	import formConstruction from "./formConstruction"
	import {
		GetBillList,
		GetBillData,
		GetTablesCoulmn,
		SaveData,
		GetBillDataPepedomSet,
		SaveBillDataPepedomSet,
		GetBillCodeSet,
		SaveBillCodeSet,
		DelMain,
		BillMachineCreateCode,
	} from '@/api/apis/documentManagement'
	import {
		getInfo,
	} from '@/utils/auth'
	export default {
		name: "review",
		components: {
			baseLayout,
			baseTable,
			baseDialog,
			documentInformation,
			dataPermission,
			codeGneration,
			orderSetting,
			formConstruction
		},
		data() {
			return {
				innerVisible: false,
				tableHeight: 'calc(100vh - 189px)',
				informationDialog: false,
				gnerationDialog: false,
				orderDialog: false,
				permissionDialog: false,
				buttonList: [{
						'menuName': '新增',
						icon: 'el-icon-plus'
					},
					{
						'menuName': '编辑',
						icon: 'el-icon-edit'
					},
					{
						'menuName': '代码生成',
						icon: 'el-icon-setting'
					},
					{
						'menuName': '单号设置',
						icon: 'el-icon-plus'
					},
					{
						'menuName': '数据权限设置',
						icon: 'icon-shoucang'
					},
					{
						'menuName': '删除',
						icon: 'el-icon-delete',
						type: 'danger'
					}
				],
				tabLoading: false,
				tableData: [],
				settingCenter: "left",
				pageModel: {
					page: 1,
					limit: 10,
					Sequence: '',
					SequenceName: ''
				},
				radioIndex: false,
				radioID: '',
				bm_functionName: '',
				selectStatus:''
			}
		},
		computed: {
			// 表头配置
			tableColumn() {
				return configData.tableColumnJson
			},
		},
		watch: {
			informationDialog: {
				deep: true, //深度监听设置为 true
				handler: function(newV, oldV) {
					if (newV == false) {
						this.$nextTick(() => {
							this.$refs.informationFrom.$refs.customForm.choiceAssignment({})
							this.$refs.informationFrom.dataMain = {}
							this.$refs.informationFrom.tablesData = []
							this.$refs.informationFrom.tableCoulmnData = []
							this.$refs.informationFrom.temporaryTableData = []
							// this.$refs.orderSettingFrom.orderIndex = null
						})
					}
				}
			},
			orderDialog: {
				deep: true, //深度监听设置为 true
				handler: function(newV, oldV) {
					if (newV == false) {
						this.$nextTick(() => {
							this.$refs.orderSettingFrom.orderIndex = null
						})
					}
				}
			},

		},
		filters: {
			timeSplit(val) {
				if (!val) {
					return
				}
				let time = val.split("T")[0]
				return time
			},
		},
		mounted() {
			this.inPage()
		},
		methods: {
			inPage() {
				this.getTableData()
			},
			// 获得表格数据
			async getTableData() {
				this.tabLoading = true
				let params = {
					...this.pageModel
				}
				// this.$refs.customtable.clearPageIndex()
				setTimeout(() => {
					// 方法区
					this.tabLoading = false
				}, 2000);
				let res = await GetBillList(params)

				if (res.code == 1) {
					this.$nextTick(() => {
						this.$refs.layout.setPageTotal(res.data[0])
					})
					this.tableData = res.data[1]
				}

			},
			async informationGetBillData(id) {
				let params = {
					id
				}
				let res = await GetBillData(params)
				if (res.code == 1) {
					// this.informationDialog = true
					if (this.selectStatus == "编辑") {
						this.$nextTick(() => {
							this.$refs.informationFrom.$refs.customForm.ruleForm = Object.assign({}, res.data[0])
							this.$refs.informationFrom.dataMain.Bm_IsApprover = res.data[0].Bm_IsApprover == 0 ?
								false : true
							this.$refs.informationFrom.dataMain.Bm_IsAttachment = res.data[0].Bm_IsAttachment ==
								0 ? false : true
							this.$refs.informationFrom.dataMain.Bm_openType = res.data[0].Bm_openType?parseInt(res.data[0].Bm_openType):''
							this.$refs.informationFrom.tablesData = Object.assign([], res.data[1])
							// this.$refs.informationFrom.$refs.customForm.ruleForm  = Object.assign({}, res.data[0])
						})
					} else if (this.selectStatus == "代码生成") {
						let selectAll = ["Bm_IsMenu","Bm_IsButton","Bm_IsCreateTable","Bm_IsEntity","Bm_IsController","Bm_IsView","Bm_IsJS"]
						let buildSelected = []
 							for(let key in res.data[0]){
 								if(selectAll.includes(key)){
									if (res.data[0][key] == 1) {
										buildSelected.push(key)
									}
								}
 						}
						this.$nextTick(() => {
							this.$refs.gnerationFrom.$refs.customForm.ruleForm = Object.assign({}, res.data[0])
							this.$refs.gnerationFrom.dataMain.Bm_IsApprover = res.data[0].Bm_IsApprover == 0 ?
								false : true
							this.$refs.gnerationFrom.dataMain.Bm_IsAttachment = res.data[0].Bm_IsAttachment ==
								0 ? false : true
							this.$refs.gnerationFrom.dataMain.Bm_openType = res.data[0].Bm_openType?parseInt(res.data[0].Bm_openType):''
							this.$refs.gnerationFrom.databaseTableData = Object.assign([], res.data[1])
							this.$refs.gnerationFrom.buildSelected = buildSelected
						})
					}
				}
			},
			// 获得数据权限详情
			async GetPermissionDetail(id) {
				let params = {
					id
				}
				let res = await GetBillDataPepedomSet(params)
				if (res.code == 1) {
					this.permissionDialog = true
					this.$nextTick(() => {
						this.$refs.permissionFrom.$refs.customForm.incomingParameters(res.data[0])
						this.$refs.permissionFrom.$refs.customForm.getField("bm_functionName", this
							.bm_functionName)
						this.$refs.permissionFrom.$refs.customForm.getField("id", id)
					})
				}
			},
			// 获取编码详情
			async getOrderDetail(id) {
				let params = {
					id
				}
				let res = await GetBillCodeSet(params)
				if (res.code == 1) {
					this.orderDialog = true
					this.$nextTick(() => {
						this.$refs.orderSettingFrom.orderTableData = Object.assign([], res.data[0])
						this.$refs.orderSettingFrom.formData.name = this.bm_functionName
					})
				}
			},
			// 关闭代码生成弹窗重置数据
			codeGeneration(){
				this.$refs.gnerationFrom.fieldTableData = []
				this.$refs.gnerationFrom.buildSelected = []
			},
			// 单选
			radioChange(val) {
				this.radioIndex = val.index
				this.radioID = val.bm_id
				this.bm_functionName = val.bm_functionName
			},
			// 查询、筛选事件
			pageChange(model) {
				this.radioIndex = false
				this.radioID = ''
				this.pageModel.page = model.page
				this.pageModel.limit = model.limit
				this.$refs.customtable.clearRadioIndex()
				this.getTableData()
			},
			// 按钮点击事件
			getFuncBtn(btnEven) {
				if (btnEven.menuName == '新增') {
					this.informationDialog = true
					this.selectStatus = "新增"
					// this.$nextTick(() => {
					// 	let ruleForm = this.$refs.informationFrom.$refs.customForm.getField('Bm_id', 0)
					// })
				}
				if (btnEven.menuName == '编辑') {
					if (this.radioIndex === false) {
						this.$vmNews('请选择一条数据！')
						return
					}
					this.informationGetBillData(this.radioID)
					this.selectStatus = "编辑"
					this.informationDialog  = true

				}
				if (btnEven.menuName == '代码生成') {
					if (this.radioIndex === false) {
						this.$vmNews('请选择一条数据！')
						return
					}
					this.informationGetBillData(this.radioID)
					this.selectStatus = "代码生成"
					this.gnerationDialog = true
				}
				if (btnEven.menuName == '单号设置') {
					if (this.radioIndex === false) {
						this.$vmNews('请选择一条数据！')
						return
					}
					this.getOrderDetail(this.radioID)

				}
				if (btnEven.menuName == '数据权限设置') {
					if (this.radioIndex === false) {
						this.$vmNews('请选择一条数据！')
						return
					}
					this.GetPermissionDetail(this.radioID)
				}
				if (btnEven.menuName == '删除') {
					this.delData()
				}

			},
			delData(){
				if (this.radioIndex === false) {
					this.$vmNews('请选择一条数据！')
					return
				}
				this.$confirm('确定删除吗？', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					let params = {
						id:this.radioID,
					}
					DelMain(params).then(res=>{
						if(res.code == 1){
							this.$vmNews('删除成功！','success')
							this.tableReset()
						}
					})
					
				}).catch(() => {});
			},
			// 数据权限保存
			async permissionConfirmClick() {
				let ruleForm = this.$refs.permissionFrom.$refs.customForm.ruleForm
				let params = {
					id: this.radioID,
					dataMain: {
						...ruleForm
					}
				}
				let res = await SaveBillDataPepedomSet(params)
				if (res.code == 1) {
					this.$vmNews('数据权限保存成功', 'success')
					this.tableReset()
					this.permissionDialog = false
				}
			},
			// 编码设置保存
			async orderConfirmClick() {
				let RuleStr = this.$refs.orderSettingFrom.formData.RuleStr
				let ruleData = this.$refs.orderSettingFrom.orderTableData
				let params = {
					id: this.radioID,
					RuleStr: RuleStr,
					ruleData: ruleData
				}
				let res = await SaveBillCodeSet(params)
				if (res.code == 1) {
					this.$vmNews('编码设置保存成功', 'success')
					this.tableReset()

					this.orderDialog = false
				}
			},
			async informationConfirmClick() {
				let ruleForm = this.$refs.informationFrom.$refs.customForm.ruleForm
				let dataMain = this.$refs.informationFrom.dataMain
				let temporaryTableData = this.$refs.informationFrom.temporaryTableData
				let tableCoulmnData = []
				if (temporaryTableData.length > 0) {
					temporaryTableData.forEach(el => {
						el.tableData.forEach(item => {
							item.tableNameEN = el.name
							tableCoulmnData.push(item)
						})
					})
				}
				if (dataMain.Bm_IsApprover) {
					dataMain.Bm_IsApprover = 1
				} else {
					dataMain.Bm_IsApprover = 0
				}
				if (dataMain.Bm_IsAttachment) {
					dataMain.Bm_IsAttachment = 1
				} else {
					dataMain.Bm_IsAttachment = 0
				}
				dataMain.Bm_CreatePersonID = JSON.parse(getInfo()).personID
				let params = {
					dataMain: {
						...ruleForm,
						...dataMain
					},
					tablesData: this.$refs.informationFrom.tablesData,
					tableCoulmnData:tableCoulmnData
				}

				let res = await SaveData(params)
				this.$vmNews(res.message,res.success === "true"?"success":'error')
				if(res.success === "true"){
					this.informationDialog = false
					this.tableReset()
				}
			},
			// 
			async codeSaving() {
				let dataAll = {
					IsCreateMenuNew: 0,
					IsCreateButtonNew: 0,
					IsCreateTableNew: 0,
					IsEntityNew: 0,
					IsControllerNew: 0,
					IsJSNew: 0,
					IsViewNew: 0
				}
				let data = this.$refs.gnerationFrom.needToSelect
				for(let key in dataAll) {
					if(data.includes(key)){
						dataAll[key] = 1
					}
				}
				let res = await BillMachineCreateCode({
					Bm_id: this.radioID,
					...dataAll,
				})
				this.gnerationDialog = false
				this.$refs.gnerationFrom.needToSelect = []
				this.$refs.gnerationFrom.fieldTableData = []
				this.$refs.gnerationFrom.buildSelected = []
				if (res.code ==1) {
					this.$vmNews('代码生成成功', 'success')
				} else if (res.success =="false") {
					let title = res.message
					this.$message.error(res.message);
				}
			},
			tableReset() {
				this.radioIndex = false
				this.radioID = ''
				this.pageModel.page = 1
				this.getTableData()
				this.$nextTick(() => {
					this.$refs.layout.setPageNum(1)
					this.$refs.customtable.clearRadioIndex()
				})
			}
		},
	}
</script>

<style scoped lang="scss">
	>>>.el-dialog--center .el-dialog__body {
		padding-top: 0 !important;
	}
</style>
