<!--
 * @Author: SunTao 328867980@qq.com
 * @Date: 2025-02-12 16:44:44
 * @LastEditors: SunTao 328867980@qq.com
 * @LastEditTime: 2025-02-21 10:00:26
 * @FilePath: \znxjxt-ui\src\views\xj\hisense\inter-change\components\add-inter.vue
 * @Description: 海信-收费站-新增/编辑
-->
<template>
  <div class="dialog-content">
    <el-form class="addForm" ref="addInterForm" :model="addInterForm" :rules="rules" label-width="8rem">
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="路段名称：" prop="name">
            <el-input v-model="addInterForm.name" placeholder="请填写名称" style="width: 100%" clearable>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="路段Id：" prop="sectionId">
            <el-input v-model="addInterForm.sectionId" placeholder="请填写id" style="width: 100%" clearable>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="开始桩号：" prop="startStake">
            <el-input v-model="addInterForm.startStake" placeholder="请填写开始桩号" style="width: 100%" clearable>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form-item label="结束桩号：" prop="endStake">
            <el-input v-model="addInterForm.endStake" placeholder="请填写结束桩号" style="width: 100%" clearable>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </div>
</template>

<script>
import { add, updata } from '@/api/xj/hisense/route-section'

export default {
  name: "AddSection",
  props: {
    // 接收父组件传过来的数据
    dialogItem: {
      type: Object,
      default: () => { },
    },
  },
  data() {
    return {
      // 新增表单
      addInterForm: {
        name: "",
        sectionId: "",
        startStake: "",
        endStake: "",
      },
      // 表单校验规则
      rules: {
        name: [{ required: true, message: "请填写名称", trigger: "blur" }],
        sectionId: [{ required: true, message: "请填写id", trigger: "blur" }, {
          pattern: /^[1-9]\d*$/,
          message: "请输入正整数",
        }],
        startStake: [{ required: true, message: "请填写开始桩号", trigger: "blur" }, {
          pattern: /^(0|[1-9]\d*)(\.\d{1,3})?$/,
          message: "请输入非负数",
        }],
        endStake: [{ required: true, message: "请填写桩号", trigger: "blur" }, {
          pattern: /^(0|[1-9]\d*)(\.\d{1,3})?$/,
          message: "请输入非负数",
        },],
      }
    }
  },
  watch: {
    dialogItem: {
      handler(val) {
        if (val) {
          this.addInterForm = JSON.parse(JSON.stringify(val))
        }
      }, immediate: true, deep: true
    }
  },
  mounted() {

  },
  methods: {
    /**
     * @description: 点击确定事件
     * @return {*}
     */
    submitForm() {
      this.$refs.addInterForm.validate((valid) => {
        if (valid) {
          const data = {
            ...this.addInterForm,
          };
          if (this.addInterForm.id) {
            updata(data).then(({ code }) => {
              if (code === 200) {
                this.$modal.msgSuccess("更新成功");
                this.$emit("cancel");
              }
            });
          } else {
            add(data).then(({ code }) => {
              if (code === 200) {
                this.$modal.msgSuccess("新增成功");
                this.$emit("cancel");
              }
            });
          }
        }
      });
    },

    /**
     * @description: 点击取消事件
     * @return {*}
     */
    cancel() {
      this.$emit("cancel");
    },
  },
}
</script>

<style lang="scss" scoped>
.dialog-content {
  width: 100%;
  // height: 20rem;
}

/* 页脚 */
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}
</style>
