<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-select
                v-model="form.wlevel"
                placeholder="请选择停泊点策略"
                @change="wlevel => plain.wlevel = wlevel">
                <el-option
                  v-for="item in points"
                  :key="item.planid"
                  :label="`停泊点策略${item.planid}(${item.wlevel})`"
                  :value="item.wlevel">
                </el-option>
              </el-select>
            </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-title" style="margin-top: 30px">
      <span>时间点</span>
      <el-button type="primary" @click="addTime">新增</el-button>
    </div>
    <div class="hum-page-section">
      <el-table v-loading="loading" :data="times" border>
        <el-table-column label="序号" type="index" />
        <el-table-column label="时间点">
          <template slot-scope="scope">
            <el-time-picker
              v-model="scope.row.time"
              value-format="HH:mm"
              :picker-options="{ format: 'HH:mm' }"
              placeholder="请选择时间点">
            </el-time-picker>
          </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="deleteTime(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 { savePlainTime, getSiteSection, getPlainTime, listPlainPoint } from '@/api/site/berthing'
import SectionChart from './chart';

export default {
  components: {
    SectionChart,
  },
  data() {
    return {
      timeId: 0,
      siteId: 0,
      site: {},
      loading: false,
      times: [],
      points: [],
      plain: {
        sections: [],
        wlevelmax: '',
        wlevelmin: '',
        wlevel: '',
      },
      form: {
        timeId: '',
        id: '',
        berthingId: '',
        siteId: '',
        siteName: '',
        type: '',
        wlevel: '',
      },
      rules: {
        wlevel: [
          {required: true, message: "起测水位不能为空", trigger: "blur"}
        ],
      },
    }
  },
  computed: {
    title() {
      return this.timeId ? '编辑时间点策略' : '新增时间点策略';
    }
  },
  methods: {
    loadPoints() {
      listPlainPoint({ siteId: this.siteId, page: 1, size: 10000 }).then(response => {
          this.points = response.data.records;
        }
      );
    },
    loadPlainTime() {
      getPlainTime(this.timeId).then((res) => {
        const { timeId, siteId, wlevel, times } = res.data || {};

        this.plain.wlevel = wlevel;

        this.form.timeId = timeId;

        this.times = (JSON.parse(times) || []).map((time) => ({ time }));

        this.siteId = siteId;
        this.loadSite();
        this.loadPoints();
        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;
      })
    },
    addTime() {
      this.times = [...this.times, { time: '' }];
    },
    deleteTime(index) {
      this.times.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,
          times: this.times.map(({ time }) => time).sort(),
        }
        savePlainTime(data).then(response => {
          this.$modal.msgSuccess(this.timeId ? "编辑成功" : "新增成功");
          this.$router.back();
        }).finally(() => {
          this.loading = false;
        });
      });
    },
    download() {
      window.open(`${process.env.VUE_APP_BASE_API}/berthing/downFile/${this.id}`, '_blank')
    },
  },
  mounted() {
    this.timeId = this.$route.params.id;
    this.siteId = this.$route.query.siteId;
    if (this.siteId) {
      this.loadSite();
      this.loadPoints();
      this.loadSection();
      this.loadConfig();
    }
    if (this.timeId) {
      this.loadPlainTime()
    }
  },
}
</script>

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