<template>
	<template-page ref="pageRef" :get-list="getList" :table-attributes="tableAttributes" :table-events="tableEvents"
		:operationColumnWidth="80" :options-evens-group="optionsEvensGroup" :moreParameters="moreParameters"
		:column-parsing="columnParsing" :operation="operation()" :exportList="exportList" :replaceOrNotMap="true"
		expCode="exp_qt">
	</template-page>
</template>

<script>
import TemplatePage from '@/components/template/template-page-1.vue'
import import_mixin from '@/components/template/import_mixin.js'
import ImageUpload from '@/components/file-upload'
import { required, mobileRequired, mobile, httpUrl, email } from '@/components/template/rules_verify.js'
import { del } from "@/api/workOrder/settlementStandardInstall";
import { listPageV2, pageExport, add, edit } from "@/api/workOrder/settlementStandardOther";
import { getSourceList } from "@/api/workOrder/settlementStandardInstall";
import { thousands } from '@/utils/common'
import operation_mixin from '@/components/template/operation_mixin.js'
export default {
	components: { TemplatePage, ImageUpload },
	mixins: [import_mixin, operation_mixin],
	data() {
		return {
			// 表格属性
			tableAttributes: {
				// 启用勾选列
				selectColumn: false
			},
			// 表格事件
			tableEvents: {
				'selection-change': this.selectionChange
			},
			// 勾选选中行
			recordSelected: [],
			/** 表单变量 */
			formDialogType: 0,
			formDialogTitles: ["新增", "编辑", "详情"],
			formDialog: false,
			formData: {
				companyName: JSON.parse(localStorage.getItem('greemall_user')).companyName,
			},
			editIndex: null,
			mainList: [],
			smallList: [],
			typeList: [],
			sourceList: []
		}
	},
	computed: {
		// 事件组合
		optionsEvensGroup() {
			return [
				[
					[
						this.optionsEvensAuth("add_qt", {
							click: this.addData
						})
					]
				]
			]
		},
		// 更多参数
		moreParameters() {
			return []
		},
		formItems() {
			return []
		}
	},
	created() {
		this.getSourceList()
	},
	methods: {
		// 列表请求函数
		getList: listPageV2,
		getList(p) {
			try {
				var pam = JSON.parse(JSON.stringify(p))
				this.editIndex = null
				return listPageV2(pam)
			} catch (error) {
				console.log(error)
			}
		},
		// 列表导出函数
		exportList: pageExport,
		// 表格列解析渲染数据更改
		columnParsing(item, defaultData) {
			if (item.jname === 'orderChannelText') {
				defaultData.render = (h, { row, index, column }) => {
					return this.editIndex == index ? (
						<div class="redbordererr">
							<el-form-item prop={`tableData.${index}.orderChannelId`} rules={[{ required: true, message: '工单来源不能为空', trigger: 'blur' }]}>
								<el-select value={{ dictCode: row.orderChannelId, dictValue: row.orderChannelText }} value-key="dictCode" placeholder="请选择"
									onInput={(val) => {
										row.orderChannelText = val.dictValue
										row.orderChannelId = val.dictCode
									}}>
									{
										this.sourceList.map((item, index) => {
											return <el-option key={item.dictCode} label={item.dictValue} value={item}></el-option>
										})
									}
								</el-select>
							</el-form-item>
						</div>
					) : (<div style="padding: 6px;">{row.orderChannelText}</div>)
				}
			}
			if (item.jname === 'typeName') {
				defaultData.render = (h, { row, index, column }) => {
					return this.editIndex == index ? (
						<div class="redbordererr">
							<el-form-item prop={`tableData.${index}.typeName`} rules={[{ required: true, message: '其他费用类型不能为空', trigger: 'blur' }]}>
								<el-input type="text" value={row.typeName} placeholder="请输入"
									onInput={(val) => {
										row.typeName = val
										row.type = val
									}}>
								</el-input>
							</el-form-item>
						</div>
					) : (<div style="padding: 6px;">{row.typeName}</div>)
				}
			}
			if (item.jname === 'label') {
				defaultData.render = (h, { row, index, column }) => {
					return this.editIndex == index ? (
						<div class="redbordererr">
							<el-form-item prop={`tableData.${index}.label`} rules={[{ required: true, message: '功率不能为空', trigger: 'blur' }]}>
								<el-input type="text" value={row.label} placeholder="请输入"
									onInput={(val) => {
										row.label = val
									}}>
								</el-input>
							</el-form-item>
						</div>
					) : (<div style="padding: 6px;">{row.label}</div>)
				}
			}
			if (item.jname === 'normAmount') {
				defaultData.render = (h, { row, index, column }) => {
					return this.editIndex == index ? (
						<div class="redbordererr">
							<el-form-item prop={`tableData.${index}.normAmount`} rules={[{ required: true, message: '单价不能为空', trigger: 'blur' }]}>
								<el-input type="text" value={row.normAmount} placeholder="请输入"
									onInput={(val) => {
										row.normAmount = val
									}}>
								</el-input>
							</el-form-item>
						</div>
					) : (<div style="padding: 6px;">{thousands(row.normAmount)}</div>)
				}
			}
			if (item.jname === 'remark') {
				defaultData.render = (h, { row, index, column }) => {
					return this.editIndex == index ? (
						<div class="redbordererr">
							<el-form-item prop={`tableData.${index}.remark`}>
								<el-input type="text" value={row.remark} placeholder="请输入"
									onInput={(val) => {
										row.remark = val
									}}>
								</el-input>
							</el-form-item>
						</div>
					) : (<div style="padding: 6px;">{row.remark}</div>)
				}
				defaultData.columnAttributes.width = 200
			}
			return defaultData
		},
		// 监听勾选变化
		selectionChange(data) {
			this.recordSelected = data
		},
		getSourceList(){
			getSourceList({ pageNum: 1, pageSize: -1, params: [{param: "a.dict_type", compare: "=", value: "ORDER_CHANNEL"}] }).then(res => {
				this.sourceList = [...[{ dictValue: '通用', dictCode: '0' }], ...res.data.records]
			})
		},

		operation() {
			return this.operationBtn({
				edit_qt: {
					conditions: ({ row, index, column }) => {
						return row.id
					},
					name: ({ row, index, column }) => {
						return this.editIndex == index ? '确定' : '编辑'
					},
					click: ({ row, index, column }) => {
						this.$refs.pageRef.tableForm().validate((valid) => {
							if (valid) {
								if (this.editIndex != index) {
									this.editIndex = index
								} else {
									this.edit(row)
								}
							} else {
								console.log('error submit!!');
								return false;
							}
						});
					}
				},
				save_qt: {
					conditions: ({ row, index, column }) => {
						return !row.id
					},
					click: ({ row, index, column }) => {
						this.$refs.pageRef.tableForm().validate((valid) => {
							if (valid) {
								this.add(row)
							} else {
								console.log('error submit!!');
								return false;
							}
						});
					}
				},
				del_qt: {
					prompt: "请确认是否删除该数据, 是否继续?",
					click: ({ row, index, column }) => {
						if (row.id) {
							del({
								id: row.id
							}).then(res => {
								if (res.code == 200) {
									this.$message({ type: 'success', message: '删除成功!' })
									this.$refs.pageRef.refreshList()
								} else {
									this.$message.error(res.msg);
								}
							})
						} else {
							this.$refs.pageRef.deleteRowData(0);
							this.editIndex = null
						}
					}
				}
			})
		},

		addData() {
			this.$refs.pageRef.tableForm().validate((valid) => {
				if (valid) {
					this.editIndex = 0
					this.$refs.pageRef.insertionData(0, {
						companyWechatName: JSON.parse(localStorage.getItem('greemall_user')).companyName,
						settleNormType: 'OTHER',
						orderChannelText: '',
						orderChannelId: '',
						typeName: '',
						type: '',
						label: '',
						normAmount: '',
						remark: ''
					})
				} else {
					console.log('error submit!!');
					return false;
				}
			});
		},
		openForm() {
			this.formDialog = true;
		},
		add(row) {
			add(row).then(res => {
				if (res.code == 200) {
					this.editIndex = null
					this.$message({ type: 'success', message: `保存成功!` })
					this.$refs.pageRef.refreshList()
				}
			})
		},
		edit(row) {
			edit({
				id: row.id,
				settleNormType: 'OTHER',
				orderChannelText: row.orderChannelText,
				orderChannelId: row.orderChannelId,
				typeName: row.typeName,
				type: row.type,
				label: row.label,
				normAmount: row.normAmount,
				remark: row.remark
			}).then(res => {
				if (res.code == 200) {
					this.editIndex = null
					this.$message({ type: 'success', message: `编辑成功!` })
					this.$refs.pageRef.refreshList()
				}
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.tab {
	padding: 20px 20px 0 20px;
}

.redbordererr {
	::v-deep .el-form-item {
		margin: 0 !important;
		overflow: hidden;
	}
}
</style>
