<template>
  <app-container>
    <search-area :need-clear="true" :need-search-more="false" type="seperate" search-more-type="default" @search="fetchData" @clear="clearInput">
      <!--一般查询条件-->
      <search-item>
        <el-input v-model.trim="listQuery.carCode" size="small" placeholder="车牌号" clearable/>
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.description" size="small" placeholder="车辆描述" clearable/>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.carType" size="small" placeholder="车辆类型">
          <el-option
            v-for="item in carTypeList"
            :key="item.value"
            :label="item.name"
            :value="item.value"/>
        </el-select>
      </search-item>
      <search-item>
        <dept-select v-model="listQuery.deptId" :dept-show="true" size="small" placeholder="请选择使用单位" />
      </search-item>
    </search-area>
    <normal-table :data="list" :head="tableOption.head" :query="listQuery" :total="total" :columns="columns" :list-loading="listLoading" :options="tableOption.options" :tools-option="tableOption.toolsOption" @change="changePage">
      <template slot="btns">
        <el-button v-if="hasPerm('/car/busCarInfo/add')" size="small" class="edit_btn" @click.stop="add">新增</el-button>
        <el-upload
          v-if="hasPerm('/car/busCarInfo/add')"
          :limit="1"
          :show-file-list="false"
          :http-request="uploadFile"
          :file-list="fileList"
          action="string"
          accept=".xls,.xlsx"
          class="edit_btn">
          <el-button slot="trigger" size="small">批量导入</el-button>
        </el-upload>
        <!--<el-button size="small" class="edit_btn">删除</el-button>-->
        <el-button v-if="hasPerm('/car/busCarInfo/export')" size="small" class="edit_btn" @click="batchExport">批量导出</el-button>
      </template>
      <template slot="columns">
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.stop="goDetail(scope.row)">详情</el-button>
            <el-button v-if="hasPerm('/car/busCarInfo/update')" type="text" size="small" @click.stop="edit(scope.row)">编辑</el-button>
            <el-button v-if="hasPerm('/car/busCarInfo/delete')" type="text" size="small" @click.stop="del(scope.row)">删除</el-button>
            <!--<el-button carType="text" size="small" @click.stop="registerIris(scope.row)">岗位调整</el-button>-->
          </template>
        </el-table-column>
      </template>
    </normal-table>
    <edit-car v-show="editShow" ref="editcar" @watchChild="fetchData"/>
  </app-container>
</template>

<script>
import { getCarListPage, delCar, batchImportCar, batchExportCar } from '@/api/sanitation/car'
import EditCar from './editCar'
import { getDictByType } from '@/api/common'
import DeptSelect from '@/components/DeptSelect/index'
export default {
  description: 'CarList',
  components: { DeptSelect, EditCar },
  data() {
    return {
      listQuery: {
        sys: '1', // 子系统
        carCode: '', // 车牌号
        description: '', // 车辆描述
        carType: '', // 车辆类型
        deptId: '', // 使用单位
        offset: 1,
        limit: 20,
        sort: '',
        order: 'desc'
      }, // 筛选条件
      columns: [
        {
          text: '车牌号',
          value: 'carCode',
          align: 'center'
        },
        {
          text: '车辆描述',
          value: 'description',
          align: 'center'
        },
        {
          text: '车辆类型',
          value: 'carTypeName',
          width: 165,
          align: 'center'
        },
        {
          text: '品牌型号',
          value: 'brandModel',
          align: 'center'
        },
        {
          text: '使用单位',
          value: 'deptName',
          align: 'center'
        },
        {
          text: '责任人',
          value: 'chargePerson',
          align: 'center'
        }
      ], // 显示列
      timeRange: [], // 时间范围
      list: [], // 列表数据
      total: 0, // 数据总数
      listLoading: true, // 列表加载动画
      carTypeList: [],
      bindList: [],
      fileList: [],
      tableOption: {
        head: {
          show: true, // 是否需要标题栏，
          text: '数据列表' // 标题名称
        },
        options: {
          needIndex: true // 是否需要序号列
        },
        toolsOption: {
          selectColumns: false, // 是否需要筛选列
          refresh: false // 是否需要刷新按钮
        }
      }, // 表格属性
      editShow: false // 编辑页面是否显示
    }
  },
  created() {
    this.fetchCarType()
    this.fetchBindType()
    this.fetchData()
  },
  methods: {
    fetchData() {
      this.listLoading = true
      const that = this
      getCarListPage(this.listQuery).then(response => {
        that.list = response.data.rows
        that.total = response.data.total
        that.listLoading = false
      })
    },
    // 点击编辑
    edit(row) {
      this.editShow = true
      this.$refs.editcar.initDialog('update', true, row)
    },
    // 点击详情
    goDetail(row) {
      this.editShow = true
      this.$refs.editcar.initDialog('detail', true, row)
    },
    // 点击新增
    add() {
      this.editShow = true
      this.$refs.editcar.initDialog('create', true)
    },
    // 删除单个车辆
    del(row) {
      const staffIds = [row.id]
      this.$confirm(
        '确定要删除该车辆吗？',
        '确认操作',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          carType: 'warning'
        }
      ).then(() => {
        delCar(staffIds).then(response => {
          if (response.code === 200) {
            this.$message.success('删除成功')
            this.fetchData()
          }
        })
      })
    },
    // 删除多个车辆
    batchDel() {
      if (this.checkSelection()) {
        const staffIds = []
        this.multipleSelection.forEach(function(value, index) {
          staffIds.push(value.id)
        })
        this.$confirm(
          '确定要删除所选车辆吗？',
          '确认操作',
          {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            carType: 'warning'
          }
        ).then(() => {
          delCar(staffIds).then(response => {
            if (response.carCode === 200) {
              this.$message.success('删除成功')
              this.fetchData()
            }
          })
        })
      } else {
        this.$message.error('至少选中一项')
      }
    },
    // 批量导入
    uploadFile(param) {
      // 判断文件大小是否符合要求
      const _file = param.file
      const isLt5M = _file.size / 1024 / 1024 < 5
      if (!isLt5M) {
        this.$message.error('请上传5M以下的excel文件')
        return false
      }
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '导入中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      // 发起导入请求
      batchImportCar(_file).then(res => {
        loading.close() // 关闭加载动画
        if (res.code === 200) {
          this.$message.success('导入成功')
          this.fetchData()
        } else {
          this.$message.error(res.message)
        }
      }).catch(() => {
        loading.close() // 关闭加载动画
      })
      this.fileList = []
    },
    // 批量导出
    batchExport() {
      // 全屏加载动画
      const loading = this.$loading({
        lock: true,
        text: '数据处理中,请稍后...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      batchExportCar(this.listQuery).then(res => {
        loading.close() // 关闭加载动画
        const blob = new Blob([res.data])
        const downloadElement = document.createElement('a')
        const href = window.URL.createObjectURL(blob) // 创建下载的链接
        downloadElement.href = href
        downloadElement.download = `车辆列表.xlsx` // 下载后文件名
        document.body.appendChild(downloadElement)
        downloadElement.click() // 点击下载
        document.body.removeChild(downloadElement) // 下载完成移除元素
        window.URL.revokeObjectURL(href) // 释放blob对象
      }).catch((res) => {
        loading.close()
      })
    },
    // 获取车辆类别
    fetchCarType() {
      getDictByType('carType').then(response => {
        this.carTypeList = response.data
      })
    },
    // 获取绑定类别
    fetchBindType() {
      getDictByType('bindType').then(response => {
        this.bindList = response.data
      })
    },
    // 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
    changePage(val) {
      if (val && val.size) {
        this.listQuery.limit = val.size
      }
      if (val && val.page) {
        this.listQuery.offset = val.page
      }
      this.fetchData()
    },
    // 重置后的操作， 若不需要显示重置按钮则不需要写
    clearInput() {
      this.listQuery = {
        sys: '1', // 子系统
        carCode: '', // 车牌号
        description: '', // 车辆描述
        carType: '', // 车辆类型
        deptId: '', // 使用单位
        offset: 1,
        limit: 20,
        sort: '',
        order: 'desc'
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .edit_btns{
    .edit_btn{
      float:right;
      margin-left:5px;
    }
  }
</style>
