
<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog
    :visible.sync="showDialog"
    :title="title"
    :modal-append-to-body="false"
    style="text-align: left"
    width="40%"
    @close="closeDialog"
    :close-on-click-modal="false"
  >
    <div class="user-panel" v-loading="loading">
      <el-form
        ref="form"
        :model="formModel"
        :rules="ruleValidate"
        :label-width="'130px'"
      >
        <el-form-item label="上车站点" prop="startStationId">
          <el-select
            v-model="formModel.startStationId"
            filterable
            placeholder="请选择"
            style="width: 80%"
          >
            <el-option
              v-for="item in startStationData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="下车站点" prop="endStationId">
          <el-select
            v-model="formModel.endStationId"
            filterable
            placeholder="请选择"
            style="width: 80%"
          >
            <el-option
              v-for="item in endStationData"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="费用" prop="price">
          <el-input-number
            v-model="formModel.price"
            placeholder="请输入费用"
            style="width: 80%"
            :min="0"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="距离" prop="distance">
          <el-input-number
            v-model="formModel.distance"
            placeholder="请输入距离"
            style="width: 80%"
            :min="0"
          ></el-input-number>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="submitting"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import Constant from "@/constant";
import stationInfoApi from "@/api/bus/stationInfo";
import priceInfoApi from "@/api/bus/priceInfo";

import { getToken } from "@/utils/auth"; // get token from cookie

export default {
  props: ["routeId", "title"],
  data() {
    return {
      formModel: {
        enableChangeParent: false,
      },
      ruleValidate: {
        startStationId: [
          { required: true, message: "上车站点不能为空", trigger: "blur" },
        ],
        endStationId: [
          { required: true, message: "下车站点不能为空", trigger: "blur" },
        ],
        price: [{ required: true, message: "费用不能为空", trigger: "blur" }],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      typeData: [],
      treeData: [],
      props: {
        // 配置项（必选）
        value: "id",
        label: "name",
        children: "children",
      },
      startStationData: [],
      endStationData: [],
    };
  },
  watch: {
    "formModel.startStationId"(newVal, oldVal) {
      var formData = new FormData();
      formData.append("startStationId", newVal);
      formData.append("endStationId", this.formModel.endStationId);
      priceInfoApi.findPriceByStation(formData).then((response) => {
        var jsonData = response.data;
        this.formModel.price = jsonData.data.price;
        this.formModel.distance = jsonData.data.distance;
        this.formModel.id = jsonData.data.id;
      });
    },
    "formModel.endStationId"(newVal, oldVal) {
      var formData = new FormData();
      formData.append("startStationId", this.formModel.startStationId);
      formData.append("endStationId", newVal);
      priceInfoApi.findPriceByStation(formData).then((response) => {
        var jsonData = response.data;
        this.formModel.price = jsonData.data.price;
        this.formModel.distance = jsonData.data.distance;
        this.formModel.id = jsonData.data.id;
      });
    },
  },
  created() {
    var self = this;

    var formData = new FormData();
    formData.append("routeId", self.routeId);
    stationInfoApi.list(formData).then((response) => {
      var jsonData = response.data;
      this.startStationData = jsonData.data;
      this.endStationData = jsonData.data;
    });
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    handleSubmit() {
      var self = this;

      this.$refs["form"].validate((valid) => {
        if (valid) {
          (function () {
            var id = self.formModel.id;

            if (id == null || id.length == 0) {
              return priceInfoApi.add(self.formModel);
            } else {
              return priceInfoApi.update(self.formModel);
            }
          })().then(function (response) {
            var jsonData = response.data;

            if (jsonData.result) {
              self.$message({
                message: "保存成功!",
                type: "success",
              });

              self.$emit("close", true);
            } else {
              self.$message({
                message: jsonData.message + "",
                type: "warning",
              });

              self.$emit("close", false);
            }
          });
        }
      });
    },
  },
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>