<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="选择站点">
                <el-select v-model="siteId" placeholder="请选择站点" @change="handleSiteChange">
                  <el-option
                    v-for="item in siteList"
                    :key="item.siteId"
                    :label="item.siteName"
                    :value="item.siteId">
                  </el-option>
                </el-select>
              </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, listSite } from "@/api/site/site";
import { deletePlainPoint, getSiteSection, listPlainPoint } from "@/api/site/berthing";
import ChartList from './chart-list';

export default {
  components: {
    ChartList
  },
  data() {
    return {
      siteId: '',
      siteList: [],
      plain: {
        wlevelmax: 0,
        wlevelmin: 0,
        list: [],
        sections: [],
      },
      queryParams: {
        page: 1,
        size: 1000,
      },
      loading: true,
      total: 0,
      list: []
    }
  },
  created() {
    this.init();
  },
  methods: {
    handleQuery() {
      this.getList()
    },
    resetQuery() {},
    init() {
      this.loading = true;
      listSite({ page: 1, size: 1000 }).then(response => {
          this.siteList = response.data.records || [];
          if (this.siteList.length > 0) {
            this.siteId = this.siteList[0].siteId;
            this.getList();
            this.loadConfig();
            this.loadSection();
          }
        }
      ).catch(() => {
        this.loading = false;
      });
    },
    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>
