<template>
  <div class="hum-page-container">
    <div class="hum-page-title">停泊点策略管理</div>
    <div class="hum-page-section">
      <div class="hum-page-search">
        <el-form :model="queryParams" size="large" ref="queryForm" label-position="top">
          <el-row :gutter="30">
            <el-col :span="6">
              <el-form-item label="选择站点">
                <SiteSelector @change="handleSiteChange" />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item class="hum-page-search-action">
                <el-button type="primary" @click="handleQuery">查询</el-button>
                <el-button @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="hum-page-action">
        <el-button type="primary" @click="handleAdd">新增策略</el-button>
      </div>
      <el-table v-loading="loading" :data="list" border>
        <el-table-column label="站码" prop="id" />
        <el-table-column label="站点名称" prop="siteName" />
        <el-table-column label="策略编号" prop="planid" />
        <el-table-column label="起测水位" prop="wlevel" />
        <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-edit" @click="handleUpdate(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="hum-page-title" style="margin-top: 30px">断面图</div>
    <div class="hum-page-section">
      <ChartList ref="chart" :plain="plain" />
    </div>
  </div>
</template>

<script>
import { getConfig } from "@/api/site/site";
import { deletePlainPoint, getSiteSection, listPlainPoint } from "@/api/site/berthing";
import SiteSelector from '@/components/SiteSelector';
import ChartList from './chart-list';

export default {
  components: {
    SiteSelector,
    ChartList,
  },
  data() {
    return {
      siteId: '',
      plain: {
        wlevelmax: 0,
        wlevelmin: 0,
        list: [],
        sections: [],
      },
      queryParams: {
        page: 1,
        size: 1000,
      },
      loading: true,
      total: 0,
      list: []
    }
  },
  methods: {
    handleQuery() {
      this.getList()
    },
    resetQuery() {},
    getList() {
      this.loading = true;
      listPlainPoint({ ...this.queryParams, siteId: this.siteId }).then(response => {
          this.list = response.data.records;
          this.total = response.data.total;
          this.plain.list = response.data.records;
          this.loading = false;
        }
      );
    },
    loadConfig() {
      getConfig(this.siteId).then((res) => {
        const { wlevelmax, wlevelmin } = res.data || {};
        this.plain.wlevelmax = wlevelmax;
        this.plain.wlevelmin = wlevelmin;
      })
    },
    loadSection() {
      getSiteSection(this.siteId).then((res) => {
        const { positions } = res.data || {};
        this.plain.sections = JSON.parse(positions) || [];
      })
    },
    handleSiteChange(siteId) {
      this.siteId = siteId;
      this.$refs.chart.chart.clear();
      this.getList();
      this.loadConfig();
      this.loadSection();
    },
    handleAdd() {
      this.$router.push(`/site/plain/point/add?siteId=${this.siteId}`);
    },
    handleUpdate(plain) {
      this.$router.push(`/site/plain/point/edit/${plain.stopId}`);
    },
    handleDelete(plain) {
      this.$modal.confirm('是否确认删除策略编号为"' + plain.planid + '"的数据项？').then(function() {
        return deletePlainPoint(plain.stopId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
}
</script>

<style scoped>

</style>
