<template>
  <el-card class="content-z">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-form-item>
          <a-input
            v-model="queryParam.stationName"
            style="width: 150px"
            placeholder="输入区间站场名称"
          />
        </a-form-item>
        <a-form-item>
          <j-list-select-tag
            v-model="queryParam.lineId"
            style="width: 150px"
            placeholder="选择线路"
            dict-code="line"
          />
        </a-form-item>
        <a-form-item>
          <a-tree-select
            v-model="deptId"
            style="width: 150px"
            :show-search="true"
            allow-clear
            placeholder="选择车间"
            :tree-data="treeData"
            tree-node-filter-prop="label"
            :replace-fields="{children:'children', title:'deptName', key:'deptId', value: 'deptId' }"
          />
        </a-form-item>
        <a-form-item>
          <j-dict-select-tag
            v-model="queryParam.xingbie"
            style="width: 150px"
            placeholder="选择行别"
            dict-code="xingbie"
          />
        </a-form-item>
        <a-form-item>
          <a-input
            v-model="queryParam.startMarker"
            style="width: 150px"
            placeholder="输入开始公里标"
          />
        </a-form-item>
        <a-form-item>
          <a-input
            v-model="queryParam.endMarker"
            style="width: 150px"
            placeholder="输入结束公里标"
          />
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="searchQuery">查询</a-button>
          <a-button style="margin-left: 8px" @click="searchReset">重置</a-button>
        </a-form-item>
      </a-form>
    </div>
    <!-- 操作按钮区域 -->
    <div class="table-operator" style="margin: 15px 0">
      <a-button type="primary" icon="plus" @click="handleAdd">新增</a-button>
      <!--      <a-button ghost type="danger" icon="download" @click="handleExportXls('角色信息')">导出</a-button>-->
      <!--      <a-upload name="file" :show-upload-list="false" :multiple="false" :headers="tokenHeader" :action="url.importExcelUrl" @change="handleImportExcel">-->
      <!--        <a-button ghost type="danger" icon="import">导入</a-button>-->
      <!--      </a-upload>-->

      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete" />
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon" /> 已选择&nbsp;<a style="font-weight: 600">{{
          selectedRowKeys.length }}</a>项&nbsp;&nbsp;
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        row-key="stationId"
        :columns="columns"
        :data-source="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :row-selection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange"
      >
        <template slot="deptName" slot-scope="text, record">
          <span v-for="(item, index) in record['deptStations']">
            <span v-if="record['deptStations'].length -1 != index"> {{ item.deptName || item.deptId }} ，</span>
            <span v-else>{{ item.deptName || item.deptId }} </span>
          </span>
        </template>
        <template slot="lineName" slot-scope="text, record">
          {{ record.lineName || record.lineId }}
        </template>
        <span slot="action" slot-scope="text, record">
          <a-button size="small" type="primary" @click="handleEdit(record)">
            编辑
          </a-button>
          <a-divider type="vertical" />
          <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.stationId)">
            <a-button size="small" type="danger">
              删除
            </a-button>
          </a-popconfirm>
        </span>
      </a-table>
      <check-and-edit-model ref="modalForm" @ok="modalFormOk" />
    </div>
    <!-- table区域-end -->
    <!-- 表单区域 -->
  </el-card>
</template>
<script>
import { listMixin } from '@/mixin/listMixin'
import columns from './indexColumns'
import CheckAndEditModel from './CheckAndEditModel'
import { getAction } from '@/api/request'

export default {
  components: {
    CheckAndEditModel
  },
  mixins: [listMixin],
  data() {
    return {
      // 查询条件
      deptId: undefined,
      queryParam: {},
      // 表头
      columns: columns(this),
      treeData: [],
      url: {
        list: '/business/catenary/bus/station/list',
        delete: '/business/catenary/bus/station/',
        exportXlsUrl: '/exportXlsUrl',
        tree: '/business/catenary/bus/line/getDeptByLineId/',
        importExcelUrl: `${process.env.VUE_APP_BASE_API}/importExcelUrl`
      }
    }
  },
  watch: {
    deptId(val) {
      this.queryParam.deptId = val
    },
    'queryParam.lineId': {
      handler: function(newV, oldV) {
        if (newV) {
          this.loadTree(newV)
        }
        this.$nextTick(() => {
          this.queryParam.deptId = undefined
          this.deptId = undefined
        })
      }
    }
  },
  created() {
  },
  methods: {
    loadTree(lineId) {
      this.treeData = []
      getAction(this.url.tree + lineId).then((res) => {
        if (res.code === 200) {
          this.treeData = res.rows
        }
      })
    }
  }
}
</script>
<style scoped>
  @import '~@/assets/less/common.less'
</style>
