<template>
	<div class="sheet-classify-dialog">
		<el-dialog
		:visible.sync="isOpenDialog"
		:close-on-click-modal="false"
		:modal-append-to-body='false'
    :title="title"
		@close="cancelHandle"
		custom-class="dialog"
		center
		width="560px"
		v-dialogDrag>
			<div class="dialog-main">
				<el-form
				ref="diaForm"
				label-position="left"
				hide-required-asterisk
				label-width="80px"
				:model="formData"
				:rules="formRules">
				
          <el-form-item label="分类名称" prop="classify_name">
            <el-input
            v-model="formData.classify_name"
            style="width: 80%"
            placeholder="必填项"></el-input>
          </el-form-item>
				</el-form>
			</div>
			<div class="dia-bot">
				<el-button type="primary" style="margin-right:20px" @click="saveHandle">保存</el-button>
				<el-button type="primary" plain @click="cancelHandle">取消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
import * as sheetInterface from '@/api/modules/sheetApi.js';
import futuresInterface from '@/api/modules/futuresBaseApi';
import chartRelevanceApi from '@/api/modules/chartRelevanceApi.js';
import { fittingEquationInterface,statisticFeatureInterface } from '@/api/modules/chartRelevanceApi';
export default {
	props: {
		isOpenDialog: {
			type: Boolean,
		},
		//标题
		title: {
			type: String,
			default: '添加'
		},
		form: {
			type: Object,
		}
	},
	watch: {
		'isOpenDialog': {
			handler(newval) {
        if(newval) {
          this.formData = {...this.form};
        }
			}
		}
	},
	data () {
		return {			
			formData: {},
      formRules: {
        classify_name:[
          { required: true, message: '分类名称不能为空', trigger: 'blur' },
        ]
      },

		};
	},
	methods: {
		async saveHandle() {
			await this.$refs.diaForm.validate()
      const { classify_name,classify_id }  = this.formData;
      
			const handleMap = {
				'/sheetList': this.sheetClassifyApi,
				'/commordityChartBase': this.commodityClassifyApi,
				'/chartrelevance':this.relevanceClassifyApi,
				'/fittingEquationList': this.fittingEquationClassifyApi,
				'/statisticFeatureList': this.statisticFeatureClassifyApi
			}
			handleMap[this.$route.path] && handleMap[this.$route.path](classify_name,classify_id)
		},

		/* 表格分类接口 */
		async sheetClassifyApi(classify_name,classify_id) {
			const { Ret,Msg } = this.title==='添加' 
        ? await sheetInterface.classifyAdd({ ExcelClassifyName:classify_name })
        : await sheetInterface.classifyEdit({ ExcelClassifyName:classify_name, ExcelClassifyId:classify_id  })
        
      if( Ret !== 200) return
      this.$message.success(Msg);
      this.cancelHandle();
      this.$emit('successCallback')
		},

		/* 商品曲线分类接口 */
		async commodityClassifyApi(classify_name,classify_id) {
			const { Ret,Msg } = this.title==='添加' 
        ? await futuresInterface.classifyAdd({ ChartClassifyName:classify_name })
        : await futuresInterface.classifyEdit({ ChartClassifyName:classify_name, ChartClassifyId:classify_id  })
        
      if( Ret !== 200) return
      this.$message.success(Msg);
      this.cancelHandle();
      this.$emit('successCallback')
		},

		/* 相关性图表 */
		async relevanceClassifyApi(classify_name,classify_id) {
			const { Ret,Msg } = this.title==='添加' 
				? await chartRelevanceApi.classifyAdd({ ChartClassifyName:classify_name })
				: await chartRelevanceApi.classifyEdit({ ChartClassifyName:classify_name, ChartClassifyId:classify_id  })
				
			if( Ret !== 200) return
			this.$message.success(Msg);
			this.cancelHandle();
			this.$emit('successCallback')
		},

		/* 拟合方程曲线 */
		async fittingEquationClassifyApi(classify_name,classify_id){
			const { Ret,Msg } = this.title==='添加' 
				? await fittingEquationInterface.classifyAdd({ ChartClassifyName:classify_name })
				: await fittingEquationInterface.classifyEdit({ ChartClassifyName:classify_name, ChartClassifyId:classify_id  })
				
			if( Ret !== 200) return
			this.$message.success(Msg);
			this.cancelHandle();
			this.$emit('successCallback')
		},

		/* 统计特征曲线 */
		async statisticFeatureClassifyApi(classify_name,classify_id){
			const { Ret,Msg } = this.title==='添加' 
				? await statisticFeatureInterface.classifyAdd({ ChartClassifyName:classify_name })
				: await statisticFeatureInterface.classifyEdit({ ChartClassifyName:classify_name, ChartClassifyId:classify_id  })
				
			if( Ret !== 200) return
			this.$message.success(Msg);
			this.cancelHandle();
			this.$emit('successCallback')
		},

		/* 取消 */
		cancelHandle() {
      this.formData = {}
			this.$refs.diaForm.resetFields();
			this.$emit('update:isOpenDialog',false)
		},

	},
}
</script>
<style lang='scss'>
.sheet-classify-dialog {
	.dialog-main {
		padding-left: 50px;
	}
	.el-cascader .el-input {
		width: 100%;
	}
	.dia-bot {
		margin: 52px 0 30px;
		display: flex;
		justify-content: center;

	}
}
</style>