<template>
  <div class="hum-page-container">
    <div class="hum-page-title">测流设置</div>
    <el-form :model="form" :rules="rules" size="large" ref="form" label-position="top">
      <div class="hum-page-form">
        <el-row :gutter="30">
          <el-col :span="6">
            <el-form-item label="当前站点" prop="siteId">
              <el-input
                v-model="form.siteName"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="起测水位" prop="wlevel">
              <el-input-number
                placeholder="请输入起测水位"
                v-model="form.wlevel"
                @change="wlevel => plain.wlevel = wlevel"
                :precision="2"
                :step="0.1"
                :min="plain.wlevelmin"
                :max="plain.wlevelmax" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="水位变幅" prop="wlevelchange">
              <el-input-number
                placeholder="请输入水位变幅"
                v-model="form.wlevelchange"
                @change="wlevelchange => plain.wlevelchange = wlevelchange"
                :precision="2"
                :step="0.1"
                :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="时间间隔（分钟）" prop="timespan">
              <el-input-number
                placeholder="请输入时间间隔"
                v-model="form.timespan"
                :precision="2"
                :step="0.1"
                :min="0" />
            </el-form-item>
          </el-col>
        </el-row>
      </div>
    </el-form>
    <div class="hum-page-title" style="margin-top: 30px">断面图</div>
    <div class="hum-page-section">
      <SectionChart :plain="plain" />
    </div>
    <div class="hum-page-form-action">
      <el-button @click="$router.back()">取消</el-button>
      <el-button type="primary" @click="submitForm" :loading="loading">保存</el-button>
    </div>
  </div>
</template>

<script>
import { getSite, getConfig } from '@/api/site/site'
import { savePlainWater, getSiteSection, getPlainWater } from '@/api/site/berthing'
import SectionChart from './chart';

export default {
  components: {
    SectionChart,
  },
  data() {
    return {
      floatId: 0,
      siteId: 0,
      site: {},
      loading: false,
      plain: {
        sections: [],
        wlevelmax: 0,
        wlevelmin: 0,
        wlevel: 0,
        wlevelchange: 0,
      },
      form: {
        floatId: '',
        id: '',
        berthingId: '',
        siteId: '',
        siteName: '',
        type: '',
        wlevel: '',
        timespan: '',
        wlevelchange: '',
      },
      rules: {
        wlevel: [
          {required: true, message: "起测水位不能为空", trigger: "blur"}
        ],
        timespan: [
          {required: true, message: "时间间隔不能为空", trigger: "blur"}
        ],
        wlevelchange: [
          {required: true, message: "水位变幅不能为空", trigger: "blur"}
        ],
      },
    }
  },
  computed: {
    title() {
      return this.floatId ? '编辑水位变幅策略' : '新增水位变幅策略';
    }
  },
  methods: {
    loadPlainWater() {
      getPlainWater(this.floatId).then((res) => {
        const { floatId, siteId, wlevel, wlevelchange, timespan } = res.data || {};

        this.plain.wlevel = wlevel;
        this.plain.wlevelchange = wlevelchange;

        this.form.floatId = floatId;
        this.form.wlevelchange = wlevelchange;
        this.form.timespan = timespan;

        this.siteId = siteId;
        this.loadSite();
        this.loadSection();
        this.loadConfig();
      })
    },
    loadSection() {
      getSiteSection(this.siteId).then((res) => {
        const { positions, berthingId } = res.data || {};
        this.form.berthingId = berthingId;
        this.plain.sections = JSON.parse(positions) || [];
      })
    },
    loadConfig() {
      getConfig(this.siteId).then((res) => {
        const { wlevelmax, wlevelmin, type } = res.data || {};
        this.plain.wlevelmax = wlevelmax;
        this.plain.wlevelmin = wlevelmin;
        this.form.wlevel = this.plain.wlevel;
        this.form.type = type;
      })
    },
    loadSite() {
      getSite(this.siteId).then((res) => {
        const site = res.data || {};
        this.form.id = this.id;
        this.form.siteId = this.siteId;
        this.form.siteName = site.siteName;
        this.site = site;
      })
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        this.loading = true;
        const data = {
          ...this.form,
          wlevelmax: this.plain.wlevelmax,
          wlevelmin: this.plain.wlevelmin,
        }
        savePlainWater(data).then(response => {
          this.$modal.msgSuccess(this.floatId ? "编辑成功" : "新增成功");
          this.$router.back();
        }).finally(() => {
          this.loading = false;
        });
      });
    },
  },
  mounted() {
    this.floatId = this.$route.params.id;
    this.siteId = this.$route.query.siteId;
    if (this.siteId) {
      this.loadSite();
      this.loadSection();
      this.loadConfig();
    }
    if (this.floatId) {
      this.loadPlainWater()
    }
  },
}
</script>

<style lang="scss" scoped>
.el-input-number {
  width: 100%;
}
</style>
