<!--
 * @Author: zhp
 * @Date: 2023-11-08 15:30:27
 * @LastEditTime: 2024-03-25 14:14:04
 * @LastEditors: zhp
 * @Description:
-->
<template>
  <el-dialog :visible.sync="visible" :width="'35%'" :append-to-body="true" :close-on-click-modal="false" class="dialog">
    <template #title>
      <slot name="title">
        <div class="titleStyle">
          {{ !dataForm.id ? '新增' : '编辑' }}
        </div>
      </slot>
    </template>

    <el-form ref="dataForm" :model="dataForm" :rules="dataRule" label-width="100px" label-position="top"
      @keyup.enter.native="dataFormSubmit()">
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item label="参数名称" prop="paramId">
            <el-select v-model="dataForm.paramId" filterable placeholder="参数名称" @change="getData" multiple>
              <el-option v-for="dict in materialList" :key=" dict.id" :label="dict.name" :value="dict.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="工艺最小值" prop="minValue">
            <el-input v-model="dataForm.minValue" placeholder="请输入工艺最小值" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="工艺最大值" prop="maxValue">
            <el-input v-model="dataForm.maxValue" placeholder="请输入工艺最大值" clearable />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="8">
          <el-form-item label="工艺标准值" prop="defaultValue">
            <el-input v-model="dataForm.defaultValue" placeholder="请输入工艺标准值" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="dataForm.remark" placeholder="请输入备注" clearable />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row style="text-align: right">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import {
  getProcessEquValueBomDet,
  createProcessEquValueBomDetList,
  updateProcessEquValueBomDet,
  getValueList
} from '@/api/extend/processEquValueBom';

export default {
	props: {
		productId: {
			type: String,
			default: '',
		},
	},
	data() {
		return {
      visible: false,
      materialList:[],
			dataForm: {
				id: undefined,
        bomId: undefined,
        paramId:[],
        minValue:undefined,
        maxValue: undefined,
        defaultValue: undefined,
        standardMinValue: undefined,
        standardMaxValue: undefined,
        remark:null
      },
      equipmentId:null,
			dataRule: {
        paramId: [{ required: true, message: '参数名称不能为空', trigger: 'change' }],
			},
		};
  },
  mounted () {
  },
  methods: {
    getData(val) {
      this.materialList.forEach((ele) => {
        if (val === ele.id) {
          console.log(ele)
          this.dataForm.code = ele.code
          this.dataForm.standardMinValue = ele.minValue
          this.dataForm.standardMaxValue = ele.maxValue
          // this.dataForm.code = ele.id
        }
      })
    },
    init(id, bomId, equipmentId) {
      console.log(equipmentId);
      this.dataForm.id = id || '';
      this.dataForm.bomId = bomId || '';
      this.equipmentId = equipmentId
      this.getDict()
      console.log(bomId);
			this.visible = true;
			this.$nextTick(() => {
				this.$refs['dataForm'].resetFields();
				if (this.dataForm.id) {
          getProcessEquValueBomDet({
            id: this.dataForm.id,
					}).then((res) => {
            this.dataForm = res.data;
            const arr = []
            arr.push(res.data.paramId)
            this.dataForm.paramId = arr
            console.log(this.dataForm.paramId);
            // this.dataForm.materialId = materialId;
						// this.dataForm.value = value;
					});
				}
			});
    },
    getDict() {
      console.log(this.equipmentId);
      getValueList({
        id: this.equipmentId
      }).then((res) => [
        this.materialList = res.data,
        console.log(res)
      ])
    },
		// 表单提交
		dataFormSubmit() {
			this.$refs['dataForm'].validate((valid) => {
				if (valid) {
					// 修改的提交
          if (this.dataForm.id) {
            const dataObj = {
              paramId: this.dataForm.paramId.toString(),
              bomId: this.dataForm.bomId,
              minValue: this.dataForm.minValue,
              maxValue: this.dataForm.maxValue,
              defaultValue: this.dataForm.defaultValue,
              standardMinValue: this.dataForm.standardMinValue,
              standardMaxValue: this.dataForm.standardMaxValue,
              remark: this.dataForm.remark,
              id: this.dataForm.id
            }
						updateProcessEquValueBomDet({
              ...dataObj,
						}).then((response) => {
							this.$modal.msgSuccess('修改成功');
							this.visible = false;
							this.$emit('refreshDataList');
						});
						return;
          }
          const dataArr = this.dataForm.paramId.map(ele => {
            return {
              paramId: ele,
              bomId: this.dataForm.bomId,
              minValue: this.dataForm.minValue,
              maxValue: this.dataForm.maxValue,
              defaultValue: this.dataForm.defaultValue,
              standardMinValue: this.dataForm.standardMinValue,
              standardMaxValue: this.dataForm.standardMaxValue,
              remark: this.dataForm.remark
            }
          });
					// 添加的提交
          createProcessEquValueBomDetList(dataArr).then((response) => {
						this.$modal.msgSuccess('新增成功');
						this.visible = false;
						this.$emit('refreshDataList');
					});
				}
			});
		},
	},
};
</script>

<style scoped>
.dialog >>> .el-dialog__body {
	padding: 30px 24px;
}
.dialog >>> .el-dialog__header {
	font-size: 16px;
	color: rgba(0, 0, 0, 0.85);
	font-weight: 500;
	padding: 13px 24px;
	border-bottom: 1px solid #e9e9e9;
}
.dialog >>> .el-dialog__header .titleStyle::before {
	content: '';
	display: inline-block;
	width: 4px;
	height: 16px;
	background-color: #0b58ff;
	border-radius: 1px;
	margin-right: 8px;
	position: relative;
	top: 2px;
}
</style>
