<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">
	</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 { thousands } from '@/utils/common'
export default {
  components: { TemplatePage, ImageUpload },
  mixins: [import_mixin],
  data() {
    return {
      // 事件组合
      optionsEvensGroup: [
	  	[
	  		[
	  			{
	  				name: '新建',
	  				click: this.addData
	  			}
	  		],
	  	],
	  ],
      // 表格属性
      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: []
    }
  },
  computed: {
    // 更多参数
    moreParameters() {
      return []
    },
    formItems() {
		return []
    }
  },
  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 === '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
    },
    // 表格操作列
    operation(h, { row, index, column }) {
      return (
        <div class='operation-btns'>
			{row.id?<el-button type="text" onClick={() => {
				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;
					}
				});
			}}>{this.editIndex == index?'确定':'编辑'}</el-button>:null}
			{!row.id?<el-button type="text" onClick={() => {
				this.$refs.pageRef.tableForm().validate((valid) => {
					if (valid) {
						this.add(row)
					} else {
						console.log('error submit!!');
						return false;
					}
				});
			}}>保存</el-button>:null}
			<el-button type="text" style="color: #FF0000" onClick={() => {
				row.id?this.$confirm('请确认是否删除该数据, 是否继续?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
					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);
						}
					})
				}): this.$refs.pageRef.deleteRowData(0);this.editIndex = null
			}}>删除</el-button>
        </div>
      )
    },
    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',
					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',
			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>
