<template lang='pug'>
    el-container(style="padding:0px;height:77.6vh;")
        el-header(style='height:42px;line-height:42px;border: 1px solid rgba(196, 196, 196, 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" @keyup.enter.native="getDataList()")
                        el-form-item(label="应急部门名称:")
                            el-input(  v-model="dataForm.name" placeholder="应急部门名称" clearable)
                        el-form-item( label="部门负责人:")
                            el-input(  v-model="dataForm.leader" placeholder="部门负责人" clearable)
                        el-button(@click="getDataList()" size='mini' icon="el-icon-search" type="primary") 查询
                        el-button(@click="reSet()" size='mini' type="primary" icon="el-icon-refresh-right") 重置
                        el-button( v-if="isAuth('em:org:save')" size='mini' icon="el-icon-plus" type="primary" style="float:right;" @click="addOrUpdateHandle()") 新增
                drag-table( :data="dataList" :header="checkList" :isToLine='true' :option="tableOption"  @getDataList="getDataList" v-loading="dataListLoading"  :operationNum="3"  :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='duty' slot-scope="scope")
                        font(:title="scope.row.duty") {{scope.row.duty}}
                  template( slot='remark' slot-scope="scope")
                        font(:title="scope.row.remark") {{scope.row.remark}}
                  template.operation( slot-scope="scope")
                    el-button( type="text" size="mini" @click="queryHandle(scope.row)") 详情
                    el-button( type="text" size="mini" v-if="isAuth('em:org:update') && scope.row.byx1 != '1'" @click="addOrUpdateHandle(scope.row.id)") 编辑
                    el-button( type="text" size="mini" v-if="isAuth('em:org:delete') && scope.row.byx1 != '1'" @click="deleteHandle(scope.row)") 删除
                //- 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', label='序号')
                      template(scope="scope")
                        span {{scope.$index+(pageIndex - 1) * pageSize + 1}}
                    el-table-column( prop="name" header-align="center" align="center" label="应急部门名称" sortable='custom')
                        //- div(slot-scope='scope')
                        //-   span() {{scope.row.name | ellipsis}}
                    el-table-column( prop="address" header-align="center" align="center" label="应急部门地址" sortable='custom')
                        div(slot-scope='scope')
                          span() {{scope.row.address | ellipsis}}
                    el-table-column( prop="linkphone" header-align="center" align="center" label="联系电话" sortable='custom')
                        //- div(slot-scope='scope')
                        //-   span() {{scope.row.linkphone | ellipsis}}
                    el-table-column( prop="leader" header-align="center" align="center" label="部门负责人" sortable='custom')
                        //- div(slot-scope='scope')
                        //-   span() {{scope.row.leader | ellipsis}}
                    el-table-column( prop="leaderPhone" column-key='leader_phone' header-align="center" align="center" label="部门负责人电话" sortable='custom' width=160)
                        //- div(slot-scope='scope')
                        //-   span() {{scope.row.leaderPhone | ellipsis}}
                    el-table-column( prop="duty" header-align="center" align="center" label="部门职责" sortable='custom')
                        div(slot-scope='scope')
                           span() {{scope.row.duty | ellipsis}}
                    el-table-column( prop="remark" header-align="center" align="center" label="备注" sortable='custom' )
                        div(slot-scope='scope')
                           span() {{scope.row.remark | ellipsis}}
                    el-table-column( prop header-align="center" align="center" label="操作" width='260px')
                        template.operation( slot-scope="scope")
                            el-button( type="text" size="mini" v-if="isAuth('em:org:info')" @click="queryHandle(scope.row)") 详情
                            el-button( type="text" size="mini" v-if="isAuth('em:org:update')" @click="addOrUpdateHandle(scope.row.id)") 编辑
                            el-button( type="text" size="mini" v-if="isAuth('em:org:delete')" @click="deleteHandle(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")
        //- 弹窗, 新增 / 修改
        add-or-update(v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshdatalist="getDataList")
        query-detail(v-if="queryDetailVisible" ref="queryView" @refreshdatalist="getDataList")
</template>

<script>
import AddOrUpdate from './user-add-or-update'
import queryDetail from './query-detail'
import dragTable from '../../components/tab'

export default {
  name: 'sys-user',
  filters: {
    ellipsis (value) {
      if (!value) return ''
      if (value.length > 12) {
        return value.slice(0, 12) + '...'
      }
      return value
    }
  },
  data () {
    return {
      data: [],
      isCollapse: true,
      dataForm: {},
      orgInfo: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      sort: 'create_time',
      order: 'desc',
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      queryDetailVisible: false,
      checkList: [], //筛选数据
      tableHeader: [
        { label: '应急部门名称', prop: 'name' },
        { label: '应急部门地址', prop: 'address' },
        { label: '联系电话', prop: 'linkphone' },
        { label: '部门负责人', prop: 'leader' },
        { label: '部门负责人电话', prop: 'leaderPhone' },
        { label: '部门职责', prop: 'duty', slot: true },
        { label: '备注', prop: 'remark', slot: true }
      ],
      tableOption: {
        border: false, //是否边框
        maxHeight: 500 //高度
      }
    }
  },
  components: {
    AddOrUpdate,
    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.checkList = [...this.tableHeader]
    this.getDataList()
  },
  methods: {
    //重置
    reSet () {
      this.pageIndex = 1
      this.dataForm = {}
      this.getDataList()
    },
    //查看
    queryHandle (id) {
      this.queryDetailVisible = true
      this.$nextTick(() => {
        this.$refs.queryView.init(id)
      })
    },
    sortChange (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()
    },
    // 获取数据列表
    getDataList (sort, order) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/emOrg/list'),
        method: 'post',
        params: this.$http.adornParams({
          name: this.dataForm.name,
          leader: this.dataForm.leader,
          page: this.pageIndex,
          limit: this.pageSize,
          sort: sort || 'create_time',
          order: order || 'desc',
          stationId: localStorage.getItem('stationId')
        })
      }).then(data => {
        if (data && data.code === 0) {
          const datas = data.page
          this.dataList = datas.records
          this.totalPage = datas.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()
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle (row) {
      this.orgInfo = row
      this.orgInfo.deleted = 1

      this.$confirm(`确认删除该部门?`, '删除部门', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        closeOnClickModal: false
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl('/emOrg/deleteById'),
            method: 'post',
            data: this.orgInfo
          }).then(data => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
        .catch(() => {
        })
    }
  }
}
</script>
<style lang='scss'>
</style>
