<template>
  <div>
    <el-form
      ref="form"
      :model="formData"
      :rules="rules"
      label-width="200px"
      class="form"
    >
      <el-form-item label="铁路名：" prop="name">
        <el-input v-model="formData.name" placeholder="请输入铁路名" />
      </el-form-item>
      <el-form-item label="铁路线起点站名：" prop="startPointName">
        <el-input
          v-model="formData.startPointName"
          placeholder="请输入铁路线起点站名"
        />
      </el-form-item>
      <el-form-item label="铁路线终点站名：" prop="endPointName">
        <el-input
          v-model="formData.endPointName"
          placeholder="请输入铁路线终点站名"
        />
      </el-form-item>
      <el-form-item label="铁路全长（公里）：" prop="totalLong">
        <el-input
          v-model="formData.totalLong"
          placeholder="请输入铁路全长公里数"
        />
      </el-form-item>
    </el-form>
    <div class="btn">
      <el-button type="primary" @click="reset">重置</el-button>
      <el-button type="primary" @click="submit">确认提交</el-button>
    </div>
  </div>
</template>
<script>
import { railWaysave } from "../../api";
import { successAlert, warningAlert } from "@/utils/alert";
import { mapGetters, mapActions } from "vuex";
export default {
  props: [],
  components: {},
  data() {
    return {
      formData: {
        endPointName: "",
        name: "",
        startPointName: "",
        totalLong: "",
      },

      rules: {
        name: [
          { required: true, message: "请输入铁路名", trigger: "blur" },
          // { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        startPointName: [
          { required: true, message: "请输入铁路线起点站名", trigger: "blur" },
        ],
        endPointName: [
          { required: true, message: "请输入铁路线终点站名", trigger: "blur" },
        ],
        totalLong: [
          { required: true, message: "请输入铁路全长公里数", trigger: "blur" },
        ],
        siteLongitude: [
          { required: true, message: "请输入铁路全长公里数", trigger: "blur" },
        ],
      },
    };
  },
  computed: {},
  methods: {
    ...mapActions({
      asyncrailWayList: "railWay/asyncList",
    }),
    reset() {
      this.$refs.form.resetFields();
    },
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          railWaysave(this.formData).then((res) => {
            if (res.code == 200) {
              successAlert("添加成功");
              this.asyncrailWayList();
            } else {
              warning("添加失败");
            }
          });
        }
        this.formData = {
          endPointName: "",
          name: "",
          startPointName: "",
          totalLong: "",
        };
      });
    },
  },
  mounted() {},
};
</script>
<style lang="scss" scoped>
.form {
  padding: 20px 0;
  width: 600px;
  margin: 0 auto;
}
.btn {
  padding-top: 50px;
  text-align: center;
  button {
    width: 120px;
  }
}
</style>