<template>
  <div class="device">
    <template v-if="!addOrUpdateVisible && !detailVisible && !historyVisible && !damageVisible && !checkVisible && !repairVisible">
      <!-- 查询、新增操作 -->
      <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="设备状态">
          <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-button v-if="isAuth('prod:equipment:save')" type="primary" @click="addOrUpdateHandle(0)">新增</el-button>
        </el-form-item>
        <el-form-item>
          <el-checkbox v-model="dataForm.onlineState" :true-label="'1'" :false-label="'0'" @change="search()">只看在线</el-checkbox>
        </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="code"
          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="online"
          header-align="center"
          align="center"
          min-width="80"
          :show-overflow-tooltip="true"
          label="是否在线">
          <template slot-scope="scope">
            <el-tag v-if="Number(scope.row.online) === 0" type="danger">离线</el-tag>
            <el-tag v-else type="success">在线</el-tag>
          </template>
        </el-table-column>
        <el-table-column
          prop="spindleActualSpeed"
          header-align="center"
          align="center"
          min-width="120"
          :show-overflow-tooltip="true"
          label="设备主轴转速">
        </el-table-column>
        <el-table-column
          prop="name"
          header-align="center"
          align="center"
          min-width="140"
          :show-overflow-tooltip="true"
          label="设备名称">
        </el-table-column>
        <el-table-column
          prop="model"
          header-align="center"
          align="center"
          min-width="120"
          :show-overflow-tooltip="true"
          label="设备型号">
        </el-table-column>
        <el-table-column
          prop="specifications"
          header-align="center"
          align="center"
          min-width="120"
          :show-overflow-tooltip="true"
          label="设备规格">
        </el-table-column>
        <el-table-column
          prop="useFor"
          header-align="center"
          align="center"
          min-width="100"
          :show-overflow-tooltip="true"
          label="设备用途">
        </el-table-column>
        <el-table-column
          prop="accuracy"
          header-align="center"
          align="center"
          min-width="100"
          :show-overflow-tooltip="true"
          label="设备精度">
        </el-table-column>
        <el-table-column
          prop="manufacturers"
          header-align="center"
          align="center"
          min-width="120"
          :show-overflow-tooltip="true"
          label="制造厂家">
        </el-table-column>
        <el-table-column
          prop="manufacturingNumber"
          header-align="center"
          align="center"
          min-width="120"
          :show-overflow-tooltip="true"
          label="设备出厂编号">
        </el-table-column>
        <el-table-column
          prop="leaveFactoryDate"
          header-align="center"
          align="center"
          min-width="160"
          :show-overflow-tooltip="true"
          label="设备出厂日期">
        </el-table-column>
        <el-table-column
          prop="verificationDate"
          header-align="center"
          align="center"
          min-width="160"
          :show-overflow-tooltip="true"
          label="检定日期">
        </el-table-column>
        <el-table-column
          prop="validityDate"
          header-align="center"
          align="center"
          min-width="160"
          :show-overflow-tooltip="true"
          label="检定有效期（止）">
        </el-table-column>
        <el-table-column
          prop="attachList"
          header-align="center"
          align="center"
          min-width="120"
          :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="notes"
          header-align="center"
          align="center"
          min-width="180"
          :show-overflow-tooltip="true"
          label="备注">
        </el-table-column>
        <el-table-column
          prop="responsibilityUserName"
          header-align="center"
          align="center"
          min-width="80"
          :show-overflow-tooltip="true"
          label="责任人">
        </el-table-column>
        <el-table-column
          prop="userOfName"
          header-align="center"
          align="center"
          min-width="100"
          :show-overflow-tooltip="true"
          label="当前使用人">
        </el-table-column>
        <el-table-column
          prop="breakageCause"
          header-align="center"
          align="center"
          min-width="180"
          :show-overflow-tooltip="true"
          label="损坏原因">
        </el-table-column>
        <el-table-column
          prop="attachListBreakage"
          header-align="center"
          align="center"
          min-width="120"
          :show-overflow-tooltip="true"
          label="报损附件">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.attachListBreakage" 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="attachListVerification"
          header-align="center"
          align="center"
          min-width="120"
          :show-overflow-tooltip="true"
          label="检定报告附件">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.attachListVerification" 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="state"
          fixed="right"
          header-align="center"
          align="center"
          min-width="100"
          :show-overflow-tooltip="true"
          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="230"
          label="操作">
          <template slot-scope="scope">
<!--            <el-button v-if="isAuth('prod:equipment:info')" type="text" size="small" @click="detailHandle(scope.row.id)">查看</el-button>-->
            <el-button v-if="isAuth('prod:equipment:info')" type="text" size="small" @click="historyHandle(scope.row)">历史数据</el-button>
            <el-button v-if="isAuth('prod:equipment:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
            <el-button v-if="isAuth('prod:updateVerification:update')" type="text" size="small" @click="checkHandle(scope.row)">检定</el-button>
            <el-button v-if="isAuth('prod:updateBreakage:update')" type="text" size="small" @click="damageHandle(scope.row)">报损</el-button>
            <el-button v-if="isAuth('prod:equipmentRepair:insert')" type="text" size="small" @click="repairHandle(scope.row)">报修</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>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList" @onChose="onChose"></add-or-update>
    <detail v-if="detailVisible" ref="detail" @onChose="onChose"></detail>
    <device-history v-if="historyVisible" ref="history" @onChose="onChose"></device-history>
    <preview-component v-if="previewVisible" ref="preview" @onChose="onChose"></preview-component>
    <device-damage v-if="damageVisible" ref="damage" @onChose="onChose" @refreshDataList="getDataList"></device-damage>
    <device-check v-if="checkVisible" ref="check" @onChose="onChose" @refreshDataList="getDataList"></device-check>
    <device-repair v-if="repairVisible" ref="repair" @onChose="onChose" @refreshDataList="getDataList"></device-repair>
  </div>
</template>

<script>
import AddOrUpdate from './device-add-or-update'
import Detail from './temp-detail'
import History from './temp-history'
import OrgComponent from '../common/org-component'
import UserComponent from '../common/user-component'
import {optionsEquipmentState} from '@/utils/enums'
import {isAuth} from '@/utils'
import PreviewComponent from '@/views/modules/common/preview-component.vue'
import DeviceHistory from '@/views/modules/device/device-history.vue'
import DeviceDamage from '@/views/modules/device/device-damage.vue'
import DeviceCheck from '@/views/modules/device/device-check.vue'
import DeviceRepair from '@/views/modules/device/device-repair.vue'
export default {
  name: 'device-list',
  components: {
    DeviceRepair,
    DeviceCheck,
    DeviceDamage,
    DeviceHistory,
    PreviewComponent,
    Detail,
    History,
    AddOrUpdate,
    UserComponent,
    OrgComponent
  },
  data () {
    return {
      addOrUpdateVisible: false,
      detailVisible: false,
      historyVisible: false,
      previewVisible: false,
      damageVisible: false,
      checkVisible: false,
      repairVisible: false,
      dataForm: {},
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      optionsState: optionsEquipmentState
    }
  },
  created () {
    this.getDataList()
  },
  methods: {
    isAuth,
    onChose () {
      this.addOrUpdateVisible = false
      this.detailVisible = false
      this.historyVisible = false
      this.damageVisible = false
      this.checkVisible = false
      this.repairVisible = false
    },
    // 查询
    search () {
      this.pageIndex = 1
      this.getDataList()
    },
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.addOrUpdateVisible = false
      this.$http({
        url: this.$http.adornUrl('/biz-service/equipment/list'),
        method: 'get',
        params: this.$http.adornParams({
          'current': this.pageIndex,
          'size': this.pageSize,
          'name': this.dataForm.name ? this.dataForm.name : null,
          'state': this.dataForm.state ? this.dataForm.state : null,
          'onlineState': this.dataForm.onlineState !== undefined ? this.dataForm.onlineState : null
        })
      }).then(({data}) => {
        if (data && data.code === '200') {
          this.dataList = data.data.records
          // console.log('data.data.records', 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)
      })
    },
    // 历史记录
    historyHandle (row) {
      this.historyVisible = true
      this.$nextTick(() => {
        this.$refs.history.init(row.codeNumber)
      })
    },
    // 报损
    damageHandle (row) {
      this.damageVisible = true
      this.$nextTick(() => {
        this.$refs.damage.init(row)
      })
    },
    // 报修
    repairHandle (row) {
      this.repairVisible = true
      this.$nextTick(() => {
        this.$refs.repair.init(row)
      })
    },
    // 检定
    checkHandle (row) {
      this.checkVisible = true
      this.$nextTick(() => {
        this.$refs.check.init(row)
      })
    },
    // 删除
    deleteHandle (id) {
      if (!id) return
      let ids = []
      ids.push(id)
      this.$confirm(`确定删除?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl(`/iot-platform/temp/delete`),
          method: 'DELETE',
          data: ids
        }).then(({data}) => {
          if (data && data.code === '200') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    }
  }
}
</script>

<style scoped>

</style>
