<!--
 * @Author: SunTao 328867980@qq.com
 * @Date: 2025-02-12 16:44:44
 * @LastEditors: SunTao 328867980@qq.com
 * @LastEditTime: 2025-02-17 16:19:52
 * @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="bridgeName">
            <el-input v-model="addInterForm.bridgeName" 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="bridgeId">
            <el-input v-model="addInterForm.bridgeId" placeholder="请填写收费站code" 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="桥梁code：" prop="bridgeCode">
            <el-input v-model="addInterForm.bridgeCode" placeholder="请填写收费站code" 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="bridgeStartStake">
            <el-input v-model="addInterForm.bridgeStartStake" 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="bridgeEndStake">
            <el-input v-model="addInterForm.bridgeEndStake" 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="bridgeStake">
            <el-input v-model="addInterForm.bridgeStake" 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/bridge'

export default {
  name: "AddBridge",
  props: {
    // 接收父组件传过来的数据
    dialogItem: {
      type: Object,
      default: () => { },
    },
  },
  data() {
    return {
      // 新增表单
      addInterForm: {
        bridgeName: "",
        bridgeId: "",
        bridgeCode: "",
        bridgeStartStake: "",
        bridgeEndStake: "",
        bridgeStake: "",
      },
      // 表单校验规则
      rules: {
        bridgeName: [{ required: true, message: "请填写名称", trigger: "blur" }],
        bridgeId: [{ required: true, message: "请填写桥梁id", trigger: "blur" }, {
          pattern: /^[1-9]\d*$/,
          message: "请输入正整数",
        }],
        bridgeCode: [{ required: true, message: "请填写code", trigger: "blur" }],
        bridgeStartStake: [{ required: false, message: "请填写桩号", trigger: "blur" }, {
          pattern: /^(0|[1-9]\d*)(\.\d{1,3})?$/,
          message: "请输入非负数",
        },],
        bridgeEndStake: [{ required: false, message: "请填写桩号", trigger: "blur" }, {
          pattern: /^(0|[1-9]\d*)(\.\d{1,3})?$/,
          message: "请输入非负数",
        },],
        bridgeStake: [{ required: false, 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>
