<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.specifications" placeholder="规格型号" clearable)
                        el-form-item( label="品牌:")
                            el-input(  v-model="dataForm.brand" placeholder="品牌" clearable)
                        el-form-item( label="类型:")
                            el-select(v-model='dataForm.type' placeholder="请选择" clearable)
                                el-option(v-for="(item,index) in typeList" :key="index" :label="item.name" :value="item.id")
                        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:material:save')" size='mini' icon="el-icon-plus" type="primary" style="float:right;margin-bottom:10px;"  @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='type' slot-scope='scope')
                    span(v-for="(item,index) in typeList" :key="index" v-if='scope.row.type === item.id') {{item.name}}
                  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:material:update')" @click="addOrUpdateHandle(scope.row.id)") 编辑
                    el-button(type="text" size="mini"  v-if="isAuth('em:material:delete')" @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')
                    el-table-column( prop="specifications" header-align="center" align="center" label="规格型号" sortable='custom')
                    el-table-column( prop="typeName" header-align="center" align="center" label="类型" sortable='custom')
                    el-table-column( prop="brand" header-align="center" align="center" label="品牌" sortable='custom')
                    el-table-column( prop="unit" header-align="center" align="center" label="单位" sortable='custom')
                    el-table-column( prop="count" header-align="center" align="center" label="可使用数量" sortable='custom')
                    el-table-column( prop="sortValue" columnKey='sort_value' header-align="center" align="center" label="排序值" sortable='custom')
                    //- el-table-column( prop="status" header-align="center" align="center" label="是否使用" sortable='custom')
                      template(scope="scope")
                        span(v-if='scope.row.status == 1') 是
                        span(v-if='scope.row.status == 2') 否
                    el-table-column( prop="placeFor" columnKey='place_for' header-align="center" align="center" label="存放位置" sortable='custom')
                    el-table-column( prop header-align="center" align="center" label="操作" width=180)
                        template.operation( slot-scope="scope")
                            el-button(type="text" size="mini"   v-if="isAuth('em:material:info')" @click="queryHandle(scope.row)") 详情
                            el-button(type="text" size="mini"   v-if="isAuth('em:material:update')" @click="addOrUpdateHandle(scope.row.id)") 编辑
                            el-button(type="text" size="mini"  v-if="isAuth('em:material: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 > 20) {
        return value.slice(0, 20) + '...'
      }
      return value
    }
  },
  data () {
    return {
      data: [],
      isCollapse: true,
      dataForm: {},
      mInfo: {},
      dataList: [],
      typeList: [
        { id: '1', name: '通讯器材类' },
        { id: '2', name: '防疫类' },
        { id: '3', name: '消防器材' }
      ],
      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: 'specifications' },
        { label: '类型', prop: 'type', slot: true },
        { label: '品牌', prop: 'brand' },
        { label: '单位', prop: 'unit' },
        { label: '可使用数量', prop: 'count' },
        { label: '排序值', prop: 'sortValue' },
        { label: '存放位置', prop: 'placeFor' }
      ],
      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()
    // this.getType()
  },
  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()
    },
    //获取物资类型
    getType () {
      this.$http({
        url: this.$http.adornUrl('/sysDictionary/getListByParent'),
        method: 'get',
        params: { parentId: 'bf8efa4ef7fc4d4b94c7b7eaab4c9bed', stationId: localStorage.getItem('stationId') }
      }).then(data => {
        if (data && data.code === 0) {
          this.typeList = data.list
        }
      })
    },
    // 获取数据列表
    getDataList (sort, order) {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/emMaterial/list'),
        method: 'post',
        params: {
          name: this.dataForm.name,
          specifications: this.dataForm.specifications,
          brand: this.dataForm.brand,
          type: this.dataForm.type,
          typeName: this.dataForm.typeName,
          page: this.pageIndex,
          limit: this.pageSize,
          sort: sort || 'create_time',
          order: order || 'desc',
          stationId: localStorage.getItem('stationId')
        }
      }).then(data => {
        if (data && data.code === 0) {
          let 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.mInfo = row
      this.mInfo.deleted = 1

      this.$confirm(`确认删除该物资?`, '删除物资', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        closeOnClickModal: false
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl('/emMaterial/deleteById'),
            method: 'post',
            data: this.mInfo
          }).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>
