<template>
    <div class="app-container">
        <el-form :model="form" :rules="rules" ref="form" label-width="100px" :inline="false" size="normal">
            <el-form-item label="选择货品:" prop="hp">
              <el-select v-model="form.hp" placeholder="请选择货品">
                <el-option
                    v-for="item in form.goodsList"
                    :key="item.value"
                    :label="item.goodsname"
                    :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="货品重量:" prop="zl">
                <el-input v-model.number="form.zl" @input="handleInput"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" size="default" @click="submitHandler"
                  >修改</el-button
                >
            </el-form-item>
        </el-form>

    </div>
</template>

<script>
export default {
    name: 'SC01',
    props: ["formData", "row"],
    data() {
        return {
            form: {
                hp: '',
                zl: ''
            },
            rules: {
                hp: [
                    { required: true, message: '请选择货品', trigger: 'change' },
                ],
                zl: [
                    { required: true, message: '请输入货品重量', trigger: 'blur' },
                    { type: 'number', message: '重量必须为数字值',trigger: 'blur' }
                ]
                
            }
        };
    },
    computed: {
        myFormData() {
        return this.formData;
        },
    },
    watch: {
        myFormData: {
            handler(nval) {
                this.form = nval.resultMap;
                this.form.hp = this.form.planInfo[0]?.goodId
                console.log(this.form);
                this.isEdite = this.form.id ? true : false;
            },
            deep: true,
            immediate: true,
        },
    },

    mounted() {
        
    },

    methods: {
        handleInput() {
        if (!/^[1-9]\d*$/.test(this.form.zl)) {
            this.$message.error('请输入正整数！');
        }
      },
        async submitHandler() {
            let res = await this.$refs.form?.validate();
            if (res) {
                let payLoad = {};
                let { hp, zl } = this.form;
                if (this.isEdit) {
                let { taskKey, taskNodeKey } = this.myFormData.resultMap;
                payLoad.updateCommonEntityList = [
                    {
                    basicMap: {
                        tableName: "kucun",
                    },
                    conditionMap: {
                        task_key: taskKey,
                        task_node_key: taskNodeKey,
                    },
                    commMap: {
                        hp,
                        zl,
                    },
                    },
                ];
                } else {
                payLoad.insertCommonEntityList = [
                    {
                    basicMap: {
                        tableName: "kucun",
                    },
                    addListMap: [
                        {
                        hp,
                        zl,
                        taskKey: this.row.benTaskProcessKey,
                        taskNodeKey: this.row.benTaskNodeKey,
                        },
                    ],
                },
            ];
        }
        submitNodeForm(payLoad).then((response) => {
          if (response.code == 200) {
            this.$modal.msgSuccess("保存成功");
          } else {
            this.$modal.error("保存失败,请稍后再试");
          }
        });
      }
    },
        async getFormData() {
      let formData = {
        flag: false,
        msg: "",
      };
      try {
        let valid = await this.$refs.form.validate();
        if (valid) {
          formData.flag = true;
          formData.data = this.form;
          return formData;
        } else {
          formData.msg = "表单校验异常,请规范填写表单数据";
          return formData;
        }
      } catch (error) {
        // console.log(error);
        formData.msg = "表单校验异常,请规范填写表单数据";
        return formData;
      }
    },
    },
};
</script>

<style lang="scss" scoped>

</style>