<template>
  <div class="device">
    <template v-if="!detailVisible && !finishVisible">
      <!-- 查询、新增操作 -->
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()">
        <el-form-item label="名称/编号">
          <el-input v-model="dataForm.name" placeholder="名称/编号" clearable/>
        </el-form-item>
        <el-form-item label="报修人">
          <user-component v-model="dataForm.creatorId"/>
        </el-form-item>
        <el-form-item label="报修日期">
          <el-date-picker
            v-model="dataForm.date"
            value-format="yyyy-MM-dd HH:mm:ss"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设备状态">
          <el-select
            v-model="dataForm.state"
            remote
            placeholder="请选择">
            <el-option
              v-for="item in optionsState"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="search()">查询</el-button>
        </el-form-item>
      </el-form>
      <!-- 数据表格 -->
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%;">
        <el-table-column
          label="序号"
          type="index"
          width="50"
          align="center">
        </el-table-column>
        <el-table-column
          prop="equipmentName"
          header-align="center"
          align="center"
          min-width="140"
          :show-overflow-tooltip="true"
          label="设备名称">
        </el-table-column>
        <el-table-column
          prop="codeNumber"
          header-align="center"
          align="center"
          min-width="100"
          :show-overflow-tooltip="true"
          label="设备编号">
        </el-table-column>
        <el-table-column
          prop="content"
          header-align="center"
          align="center"
          min-width="140"
          :show-overflow-tooltip="true"
          label="报修问题说明">
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          min-width="140"
          :show-overflow-tooltip="true"
          label="报修附件">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.attachList" style="display: inline">
              <span v-if="index > 0">,</span>
              <a :key="item.fileName + index" type="primary" href="#" @click="previewFile(item.fileName, item.url)">{{ item.fileName }}</a>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="createTime"
          header-align="center"
          align="center"
          min-width="160"
          :show-overflow-tooltip="true"
          label="报修时间">
        </el-table-column>
        <el-table-column
          prop="repairName"
          header-align="center"
          align="center"
          label="报修人">
        </el-table-column>
        <el-table-column
          prop="maintenanceRecord"
          header-align="center"
          align="center"
          min-width="140"
          :show-overflow-tooltip="true"
          label="维修说明记录">
        </el-table-column>
        <el-table-column
          header-align="center"
          align="center"
          min-width="140"
          :show-overflow-tooltip="true"
          label="维修完成记录附件">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.attachListRepair" style="display: inline">
              <span v-if="index > 0">,</span>
              <a :key="item.fileName + index" type="primary" href="#" @click="previewFile(item.fileName, item.url)">{{ item.fileName }}</a>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="updateTime"
          header-align="center"
          align="center"
          min-width="160"
          :show-overflow-tooltip="true"
          label="维修完成时间">
        </el-table-column>
        <el-table-column
          prop="state"
          fixed="right"
          header-align="center"
          align="center"
          label="状态">
          <template slot-scope="scope">
            {{optionsState.find(t => t.value === scope.row.state).label}}
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="100"
          label="操作">
          <template slot-scope="scope">
            <el-button v-if="isAuth('prod:equipmentRepair:info')" type="text" size="small" @click="detailHandle(scope.row.repairId)">查看</el-button>
            <el-button v-if="isAuth('prod:equipmentRepair:finish') && Number(scope.row.state) === 1 && scope.row.creatorId === userId" type="text" size="small" @click="finishHandle(scope.row.repairId)">完成</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </template>
    <detail v-if="detailVisible" ref="detail" @onChose="onChose"></detail>
    <preview-component v-if="previewVisible" ref="preview" @onChose="onChose"></preview-component>
    <repair-finish v-if="finishVisible" ref="finish" @onChose="onChose" @refreshDataList="getDataList"></repair-finish>
  </div>
</template>

<script>
import Detail from './repair-detail'
import UserComponent from '../common/user-component'
import {optionsEquipmentRepairState} from '@/utils/enums'
import {isAuth} from '@/utils'
import DeviceInspection from '@/views/modules/device/device-inspection.vue'
import {getDate} from '@/utils/date-util'
import PreviewComponent from '@/views/modules/common/preview-component.vue'
import RepairFinish from '@/views/modules/device/repair-finish.vue'
export default {
  name: 'repair-list',
  components: {
    RepairFinish,
    PreviewComponent,
    DeviceInspection,
    Detail,
    History,
    UserComponent
  },
  data () {
    return {
      detailVisible: false,
      finishVisible: false,
      previewVisible: false,
      dataForm: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      optionsState: optionsEquipmentRepairState,
      userId: this.$store.state.user.id
    }
  },
  created () {
    this.getDataList()
  },
  methods: {
    getDate,
    isAuth,
    onChose () {
      this.detailVisible = false
      this.finishVisible = false
    },
    // 查询
    search () {
      this.pageIndex = 1
      this.getDataList()
    },
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.addOrUpdateVisible = false
      this.$http({
        url: this.$http.adornUrl('/biz-service/prodEquipmentRepair/list'),
        method: 'get',
        params: this.$http.adornParams({
          'current': this.pageIndex,
          'size': this.pageSize,
          'creatorId': this.dataForm.creatorId ? this.dataForm.creatorId : null,
          'name': this.dataForm.name ? this.dataForm.name : null,
          'state': this.dataForm.state ? this.dataForm.state : null,
          'dateStart': this.dataForm.date ? this.dataForm.date[0] : null,
          'dateEnd': this.dataForm.date ? this.dataForm.date[1] : null
        })
      }).then(({data}) => {
        if (data && data.code === '200') {
          this.dataList = data.data.records
          this.totalPage = Number(data.data.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()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (id, disable) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id, disable)
      })
    },
    detailHandle (id) {
      this.detailVisible = true
      this.$nextTick(() => {
        this.$refs.detail.init(id)
      })
    },
    // 预览
    previewFile (fileName, url) {
      this.previewVisible = true
      this.$nextTick(() => {
        this.$refs.preview.init(fileName, url)
      })
    },
    // 完成
    finishHandle (id) {
      this.finishVisible = true
      this.$nextTick(() => {
        this.$refs.finish.init(id)
      })
    }
  }
}
</script>

<style scoped>

</style>
