<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="siteIdHandler" />
              </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>
      <el-table v-loading="loading" :data="list" border max-height="300">
        <el-table-column label="序号" type="index" />
        <el-table-column label="站码" prop="id" />
        <el-table-column label="断面名称" prop="berthingName" />
        <el-table-column label="创建日期">
          <template slot-scope="scope">
            <span>{{ formatDateTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="断面图" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-s-operation" @click="showSectionChart(scope.row)">查看</el-button>
          </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-download" @click="download(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" />
    </div>

    <el-dialog title="断面图" :visible.sync="sectionChartVisible" width="60%" destroy-on-close>
      <SectionChart :sections="sections" />
    </el-dialog>

  </div>
</template>

<script>
import { listSection, deleteSection } from "@/api/site/berthing";
import SiteSelector from '@/components/SiteSelector';
import ChartList from './chart-list';
import SectionChart from './chart';

export default {
  components: {
    SiteSelector,
    ChartList,
    SectionChart,
  },
  data() {
    return {
      siteId: '',
      queryParams: {
        page: 1,
        size: 1000,
      },
      loading: true,
      total: 0,
      list: [],
      sections: [],
      sectionChartVisible: false,
    }
  },
  methods: {
    siteIdHandler(v) {
      this.siteId = v;
      this.handleQuery();
    },
    handleQuery() {
      this.getList()
    },
    resetQuery() {},
    getList() {
      this.loading = true;
      listSection({ ...this.queryParams, siteId: this.siteId }).then(response => {
          const history = (response.data.records || []).filter(({ status }) => status === 0);
          this.$refs.chart.setOptions(history);
          this.list = history
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    showSectionChart(section) {
      this.sectionChartVisible = true;
      this.sections = JSON.parse(section.positions);
    },
    download(section) {
      window.open(`${process.env.VUE_APP_BASE_API}/berthing/downFile/${section.berthingId}`, '_blank')
    },
    handleDelete(section) {
      this.$modal.confirm('是否确认删除断面名称为"' + section.berthingName + '"的数据项？').then(function() {
        return deleteSection(section.berthingId);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
  }
}
</script>

<style scoped>

</style>
