<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-row>
      </div>
    </el-form>
    <div class="hum-page-title" style="margin-top: 30px">断面图</div>
    <div class="hum-page-section">
      <SectionChart :plain="plain" @addPoint="addPoint" />
    </div>
    <div class="hum-page-title" style="margin-top: 30px">停泊点</div>
    <div class="hum-page-section">
      <el-table v-loading="loading" :data="points" border>
        <el-table-column label="序号" type="index" />
        <el-table-column label="起点距">
          <template slot-scope="scope">
            <span>{{ scope.row.x }}</span>
          </template>
        </el-table-column>
        <el-table-column label="水面修正系数">
          <template slot-scope="scope">
            <el-input-number
              placeholder="请输入水面修正系数"
              v-model="scope.row.factor"
              :precision="2"
              :step="0.1"
              :min="0.0" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-delete" @click="deletePoint(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </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 { savePlainPoint, getSiteSection, getPlainPoint } from '@/api/site/berthing'
import SectionChart from './chart';

export default {
  components: {
    SectionChart,
  },
  data() {
    return {
      stopId: 0,
      siteId: 0,
      site: {},
      loading: false,
      points: [],
      plain: {
        sections: [],
        wlevelmax: 0,
        wlevelmin: 0,
        wlevel: 0,
        positions: [],
      },
      form: {
        stopId: '',
        id: '',
        berthingId: '',
        siteId: '',
        siteName: '',
        type: '',
        wlevel: 0,
      },
      rules: {
        wlevel: [
          {required: true, message: "起测水位不能为空", trigger: "blur"}
        ],
      },
    }
  },
  computed: {
    title() {
      return this.stopId ? '编辑停泊点策略' : '新增停泊点策略';
    }
  },
  methods: {
    loadPlainPoint() {
      getPlainPoint(this.stopId).then((res) => {
        const { stopId, siteId, wlevel, positions, factors } = res.data || {};

        const positions1 = JSON.parse(positions);
        const factors1 = JSON.parse(factors);

        this.plain.wlevel = wlevel;
        this.plain.positions = positions1.map(({ x }) => x);

        this.points = positions1.map((p, index) => ({ ...p, factor: factors1[index] }))

        this.form.stopId = stopId;

        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;
      })
    },
    addPoint(point) {
      if (this.plain.positions.includes(point)) {
        return;
      }
      const p = this.plain.sections.find(({ x }) => x === point);
      const points = [...this.points, { ...p, factor: 1 }];
      points.sort((a, b) => a.x - b.x);
      this.points = points;
      const positions = [...this.plain.positions, point];
      positions.sort((a, b) => a - b);
      this.plain.positions = positions;
    },
    deletePoint(index) {
      this.points.splice(index, 1);
      this.plain.positions.splice(index, 1);
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        this.loading = true;
        const data = {
          ...this.form,
          wlevelmax: this.plain.wlevelmax,
          wlevelmin: this.plain.wlevelmin,
          positions: this.points.map(({ x, y }) => ({ x, y })),
          factors: this.points.map(({ factor }) => factor),
        }
        savePlainPoint(data).then(response => {
          this.$modal.msgSuccess(this.stopId ? "编辑成功" : "新增成功");
          this.$router.back();
        }).finally(() => {
          this.loading = false;
        });
      });
    },
    download() {
      window.open(`${process.env.VUE_APP_BASE_API}/berthing/downFile/${this.id}`, '_blank')
    },
  },
  mounted() {
    this.stopId = this.$route.params.id;
    this.siteId = this.$route.query.siteId;
    if (this.siteId) {
      this.loadSite();
      this.loadSection();
      this.loadConfig();
    }
    if (this.stopId) {
      this.loadPlainPoint()
    }
  },
}
</script>

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