<template>
  <div class="table-page">
    <div class="table-page-content">
      <div class="right-table">
        <div class="full-page bg">
          <div class="search-box">
            <div class="btn-box" ref="btnPanel">
              <el-button
                icon="el-icon-receiving"
                type="primary"
                class="btn btn-plus"
                size="small"
                :loading="syncLoading"
                @click="handleRefreshData"
              >
                同步
              </el-button>
            </div>
            <SearchCommon @search="handleSearch" :optionList="optionList" />
          </div>
          <div class="main-table-container">
            <div class="table-container" style="padding: 20px">
              <div class="table-content" ref="tableRef">
                <el-table
                  stripe
                  v-loading="tableLoading"
                  element-loading-spinner="el-icon-loading"
                  element-loading-background="rgba(0, 0, 0, 0.6)"
                  ref="multipleSelection"
                  :data="tableData"
                  row-key="id"
                  :max-height="tableMaxHeight"
                  class="table-list"
                  border
                  @select="handleSelection"
                  @selection-change="handleSelectionChange"
                >
                  <el-table-column
                    type="index"
                    width="100"
                    label="序号"
                    align="center"
                    :index="
                      index =>
                        index +
                        1 +
                        (params.pageBean.page - 1) * params.pageBean.pageSize
                    "
                  />
                  <el-table-column
                    prop="code"
                    min-width="100"
                    show-overflow-tooltip
                    align="center"
                    label="路线编号"
                  />
                  <el-table-column
                    prop="name"
                    min-width="100"
                    show-overflow-tooltip
                    align="center"
                    label="路线名称"
                  />
                  <el-table-column
                    prop="companyName"
                    min-width="140"
                    show-overflow-tooltip
                    align="center"
                    label="所属公司"
                  />
                  <el-table-column
                    prop="start"
                    min-width="140"
                    show-overflow-tooltip
                    align="center"
                    label="起点地址"
                  />
                  <el-table-column
                    prop="end"
                    min-width="140"
                    show-overflow-tooltip
                    align="center"
                    label="终点地址"
                  />
                  <el-table-column
                    prop="startPeg"
                    min-width="100"
                    show-overflow-tooltip
                    align="center"
                    label="起点桩号"
                  />
                  <el-table-column
                    prop="endPeg"
                    min-width="100"
                    show-overflow-tooltip
                    align="center"
                    label="终点桩号"
                  />
                  <el-table-column
                    prop="mileage"
                    min-width="100"
                    show-overflow-tooltip
                    align="center"
                    label="里程（千米）"
                  />
                  <el-table-column
                    show-overflow-tooltip
                    min-width="80"
                    align="center"
                    label="操作"
                  >
                    <template v-slot="{row}">
                      <el-link
                        type="primary"
                        :underline="false"
                        @click="handleDetailPage(row)"
                      >
                        详情
                      </el-link>
                    </template>
                  </el-table-column>
                </el-table>
                <pagination
                  ref="paginationPanel"
                  style="margin-top: 10px"
                  :pageResult="params.pageBean"
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                />
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import {getSorterDirection, refineRequestBody} from '@/utils/requestWrappers'
import pagination from '@/components/SuperPagination/index.vue'
import {TableMixin} from '@/mixins/tableMixin'
import SearchCommon from '@/components/SearchCommon/index.vue'
import {tableData, synchroData} from '@/api/publicBasic/routeInfo'
import {dictionaryEscape} from '@/utils/dictionary'
import PubSub from 'pubsub-js'

export default {
  name: 'TablePage',
  components: {
    pagination,
    SearchCommon
  },
  mixins: [TableMixin],
  data() {
    return {
      pubSub: null,
      tableMaxHeight: '600px',
      tableLoading: true,
      tableData: [],
      optionList: [
        {
          title: '路线名称',
          prop: 'name',
          type: 'input',
          default: true
        },
        {
          title: '路线编码',
          prop: 'code',
          type: 'input',
          default: true
        }
      ],
      params: {
        pageBean: {pageSize: 20, page: 1, total: 0},
        querys: [
          {operation: 'LIKE', property: 'name'},
          {operation: 'LIKE', property: 'code'}
        ],
        sorter: []
      },
      syncLoading: false // 同步按钮Loading
    }
  },
  created() {
    this.loadData()
  },
  mounted() {
    this.pubSub = PubSub.subscribe('TablePage', (_, data) => {
      switch (data.type) {
        case 'add':
          this.params.pageBean.page = 1
          this.loadData()
          break
        case 'edit':
          this.loadData()
          break
      }
    })
  },
  methods: {
    dictionaryEscape,
    getSorterDirection,

    //获取数据
    async loadData() {
      try {
        this.tableLoading = true
        const {total = 0, rows = []} = await tableData(
          refineRequestBody(this.params)
        )
        this.params.pageBean.total = total
        this.tableData = rows
      } finally {
        this.tableLoading = false
      }
    },

    // 同步数据
    handleRefreshData() {
      this.$confirm('是否路段数据？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            this.syncLoading = true
            const {state} = await synchroData()
            if (state) {
              this.$message.success(`数据同步成功`)
              this.params.pageBean.page = 1
              await this.loadData()
            }
          } finally {
            this.syncLoading = false
          }
        })
        .catch(_ => _)
    },

    // 改变页面
    changePage(pageName = '', row = {}) {
      if (!pageName) return

      this.$emit(
        'changePage',
        pageName,
        _ => {
          PubSub.publish(pageName, row)
        },
        row.id ? '编辑' : '添加'
      )
    }
  },
  beforeDestroy() {
    // 在组件销毁之前 清除订阅消息
    PubSub.unsubscribe(this.pubSub)
  }
}
</script>

<style lang="scss" scoped></style>
