<template lang='pug'>
    el-container(style="padding:0px;height:77.6vh;")
        el-header(style='height:42px;line-height:42px;border: 1px solid rgba(195, 195, 195, 1);background: #f4f4f4;')
            span.title-bold.title-left-color()  站点接管管理
            el-popover( placement="bottom" width="150")
                    el-button( slot="reference" size='mini' type="primary" icon="el-icon-s-tools" style='float:right;transform:translateY(6px)')  筛选
                    el-checkbox-group( v-model="checkList")
                        el-col( :span="24")
                            el-checkbox( v-for="(item,index) in tableHeader" :label="item" :key="index" )
                              span {{item.label}}
        el-main.box_main
            el-card()
                div.tableCard()
                    el-form(:inline="true" :model="dataForm" size="mini" )
                        el-form-item( label="申请被接管时间:")
                            el-date-picker(type="datetime" value-format="yyyy-MM-dd HH:mm:ss" style="width:175px;" v-model="dataForm.startTime" placeholder="选择开始时间"  clearable)
                        el-form-item( label="至")
                            el-date-picker( type="datetime" value-format="yyyy-MM-dd HH:mm:ss" style="width:175px;" v-model="dataForm.endTime" placeholder="选择结束时间" clearable)
                        el-form-item( label="审批状态:")
                            el-select.input_width(v-model="dataForm.status" placeholder="请选择"  clearable)
                                el-option( label="待审批" :value="1")
                                el-option( label="审批通过" :value="2")
                                el-option( label="审批不通过" :value="3")
                                el-option( label="已中断" :value="4")
                                el-option( label="放弃接管" :value="5")
                        el-button(@click="reloadDataList" size="mini" icon="el-icon-search" type="primary") 查询
                        el-button(@click="reSet()" size="mini" type="primary" icon="el-icon-refresh-right") 重置
                //- el-table( :data="dataList" @sort-change='sortChange' style="width:100%;" v-loading="dataListLoading" :stripe="true" :header-cell-style="{background:'#EEF8FF',color:'#333333'}")
                //-     el-table-column( type='index', header-align='center', align='center',width=100 label='序号')
                //-       template(scope="scope")
                //-         span {{scope.$index+(pageIndex - 1) * pageSize + 1}}
                //-     el-table-column( prop="stationName" columnKey="station_name" header-align="center" align="center" label="站点名称" sortable='custom')
                //-     el-table-column( prop="applyType" columnKey="apply_type" header-align="center" align="center" label="申请接管类型" sortable='custom')
                //-         template(slot-scope='scope')
                //-             font(v-if='scope.row.applyType===1') 线路中心
                //-             font(v-if='scope.row.applyType===2') 路网中心
                //-     el-table-column( prop="applyUserName" columnKey="apply_user_name" header-align="center" align="center" label="申请人" sortable='custom')
                //-     el-table-column( prop="applyTime" columnKey="apply_time" header-align="center" align="center" label="申请时间" sortable='custom')
                //-     el-table-column( prop="takeoverType" columnKey="takeover_type" header-align="center" align="center" label="接管类型" sortable='custom')
                //-         template(slot-scope='scope')
                //-             font(v-if='scope.row.takeoverType===1') 主动接管
                //-             font(v-if='scope.row.takeoverType===2') 被动接管
                //-     el-table-column( prop="status" columnKey="status" header-align="center" align="center" label="审批状态" sortable='custom')
                //-         template(slot-scope='scope')
                //-             font(v-if='scope.row.status===1') 待审批
                //-             font(v-if='scope.row.status===2') 审批通过
                //-             font(v-if='scope.row.status===3') 审批不通过
                //-             font(v-if='scope.row.status===4') 已中断
                //-             font(v-if='scope.row.status===5') 放弃接管
                //-     el-table-column( prop="approveTime" columnKey="approve_time" header-align="center" align="center" label="接管开始时间" sortable='custom')
                //-         template(slot-scope='scope')
                //-             font(v-if='scope.row.approveTime') {{scope.row.approveTime}}
                //-             font(v-else) --
                //-     el-table-column( prop="interruptTime" columnKey="interrupt_time" header-align="center" align="center" label="接管结束时间" sortable='custom')
                //-         template(slot-scope='scope')
                //-             font(v-if='scope.row.interruptTime') {{scope.row.interruptTime}}
                //-             font(v-else) --
                //-     el-table-column( prop header-align="center" align="center" label="操作" width='180')
                //-         template( slot-scope="scope")
                //-             el-button(type="text" size="mini" v-if="isAuth('sys:takeover:info')" @click="queryHandle(scope.row)") 查看详情
                //-             el-button(type="text" size="mini" v-if="isAuth('sys:takeover:approve') && scope.row.status===1" @click="approveHandle(scope.row.id)") 审批申请
                //-             el-button(type="text" size="mini" v-if="scope.row.status===2" @click="interruptHandle(scope.row)") 中断被接管
                drag-table( :data="dataList" :header="checkList" :isToLine='true' :option="tableOption"  @getDataList="getDataList" v-loading="dataListLoading"  :operationNum="4"  :isShowOperate="true")
                  //- -- 插槽
                  el-table-column(slot="fixed"  fixed  prop=""  label="序号" width="80"  header-align="center" align="center")
                      template( slot-scope="scope")
                        span   {{scope.$index+(pageIndex - 1) * pageSize + 1}}

                  template( slot='applyUserName' slot-scope="scope")
                    font(v-if='scope.row.applyType===1') 线路管理员
                    font(v-if='scope.row.applyType===2') 路网管理员

                  template( slot='applyType' slot-scope="scope")
                    font(v-if='scope.row.applyType===1') 线路中心
                    font(v-if='scope.row.applyType===2') 路网中心

                  template(slot='takeoverType' slot-scope='scope')
                    font(v-if='scope.row.takeoverType===1') 主动接管
                    font(v-if='scope.row.takeoverType===2') 被动接管
                  template(slot='status' slot-scope='scope')
                    font(v-if='scope.row.status===1') 待审批
                    font(v-if='scope.row.status===2') 审批通过
                    font(v-if='scope.row.status===3') 审批不通过
                    font(v-if='scope.row.status===4') 已中断
                    font(v-if='scope.row.status===5') 放弃接管

                  template(slot='approveTime' slot-scope='scope')
                            font(v-if='scope.row.approveTime') {{scope.row.approveTime}}
                            font(v-else) --

                  template(slot='interruptTime' slot-scope='scope')
                            font(v-if='scope.row.interruptTime') {{scope.row.interruptTime}}
                            font(v-else) --

                  //- 操作项
                  template( slot-scope="scope"  )
                    el-button(type="text" size="mini" @click="queryHandle(scope.row)") 查看详情
                    el-button(type="text" size="mini" v-if="isAuth('sys:takeover:approve') && scope.row.status===1" @click="approveHandle(scope.row.id)") 审批申请
                    el-button(type="text" size="mini" v-if="scope.row.status===2" @click="interruptHandle(scope.row)") 中断被接管

        el-footer.box_footer
            el-pagination(@size-change="sizeChangeHandle" background  @current-change="currentChangeHandle" :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize"  :total="totalPage" layout="total, sizes, prev, pager, next, jumper")
            //- 弹窗, 新增 / 修改
            approve-apply(v-if="approveApplyVisible" ref="approveApply" @refreshdatalist="getDataList")
            query-detail(v-if="queryDetailVisible" ref="queryView" @refreshdatalist="getDataList")
</template>

<script>
import approveApply from './approve-apply'
import queryDetail from './query-detail'
import dragTable from '../../components/tab'
export default {
  name: 'sys-station-takeover',
  data () {
    return {
      checkList: [], //筛选数据
      tableHeader: [
        { label: '站点名称', prop: 'stationName' },
        { label: '申请接管类型', prop: 'applyType', slot: true },
        { label: '申请人', prop: 'applyUserName', slot: true },
        { label: '申请时间', prop: 'applyTime' },
        { label: '接管类型', prop: 'takeoverType', slot: true },
        { label: '审批状态', prop: 'status', slot: true },
        { label: '接管开始时间', prop: 'approveTime', slot: true },
        { label: '接管结束时间', prop: 'interruptTime', slot: true }
      ],
      tableOption: {
        border: false, //是否边框
        maxHeight: 500 //高度
      },
      data: [],
      dataForm: { startTime: '', endTime: '', status: '' },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      approveApplyVisible: false,
      queryDetailVisible: false
    }
  },
  components: {
    approveApply,
    queryDetail,
    dragTable
  },
  watch: {
    totalPage() { //注意这个函数的名字必须和你监听data中的属性的名字一样，这样才能当你data中的属性发生变化时，触发这个函数
      let pages = Math.ceil(this.totalPage / this.pageSize)//新数据总页数
      //总页数小于当前页数则重新加载列表数据
      if (pages < this.pageIndex) {
          this.pageIndex = pages || 1
          this.getDataList()//获取表格数据的方法
      }
    }
  },
  created () {
    this.reloadDataList()
    this.checkList = [...this.tableHeader]
  },
    watch: {
    message(message) {
      if (message) {
        // console.log('message', message)
        this.getDataList()
        this.$store.dispatch('d2admin/takeover/set', false)
      }
    }
  },
  computed: {
    message() {
      return this.$store.state.d2admin.takeover.message
    }
  },
  methods: {
    //重置
    reSet() {
      this.pageIndex = 1
      this.dataForm = { startTime: '', endTime: '', status: '' }
      this.reloadDataList()
    },
    //排序
    sortChange (column) {
      console.log(column)
      if (column.order === 'descending') {
        this.order = 'desc'
      } else {
        this.order = 'asc'
      }
      if (column.column.columnKey) {
        this.sort = column.column.columnKey
      } else {
        this.sort = column.prop
      }
      this.getDataList()
    },
    reloadDataList() {
      this.pageIndex = 1
      this.getDataList()
    },
    // 获取数据列表
    getDataList (sort, order) {
      // console.log(this.sort, this.order)

      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/sysStationTakeover/list'),
        method: 'post',
        data: {
          startTime: this.dataForm.startTime,
          endTime: this.dataForm.endTime,
          status: this.dataForm.status,
          page: this.pageIndex,
          rows: this.pageSize,
          sort: sort,
          order: order,
          stationId: localStorage.getItem('stationId')
        }
      }).then(data => {
        // console.log('data', data)
        if (data && data.code === 0) {
          this.dataList = data.page.rows
          this.totalPage = data.page.total
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    //查看详情
    queryHandle (row) {
      this.queryDetailVisible = true
      this.$nextTick(() => {
        this.$refs.queryView.init(row)
      })
    },
    // 审批申请
    approveHandle (id) {
      this.approveApplyVisible = true
      this.$nextTick(() => {
        this.$refs.approveApply.init(id)
      })
    },
    // 中断被接管
    interruptHandle (item) {
      this.$confirm(`确认中断被接管?`, '中断被接管', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        closeOnClickModal: false
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/sysStationTakeover/interrupt'),
          method: 'post',
          data: {
            id: item.id,
            stationId: item.stationId,
            status: 4,
            stationName: item.stationName,
            byx3: item.byx3,
            type: JSON.parse(localStorage.getItem('sysSystem')).type || 1
          }
        }).then(data => {
          if (data && data.code === 0) {
            sessionStorage.removeItem('takeover')
            this.getDataList()
          }
        })
      }).catch(() => {
      })
    }
  }
}
</script>
<style lang='scss' scoped>
.box_main /deep/.el-table .el-table--fit .el-table--striped .el-table--enable-row-hover .el-table--enable-row-transition{
  border: 1px solid  !important;

}
</style>
