<!-- 设备管理 -->
<template>
  <div class="production">
    <template v-if="!addOrUpdateVisible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="queryPage()">
        <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.code"
              :label="item.value"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="queryPage()">查询</el-button>
          <el-button v-if="isAuth('prod:equipment:save')" type="primary" @click="addOrUpdateHandle(0, false)">新增设备</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="code"
          header-align="center"
          align="center"
          width="160"
          :show-overflow-tooltip="true"
          label="编码">
        </el-table-column>
        <el-table-column
          prop="name"
          header-align="center"
          align="center"
          width="160"
          :show-overflow-tooltip="true"
          label="名称">
        </el-table-column>
        <el-table-column
          prop="state"
          header-align="center"
          align="center"
          :formatter="formatState"
          label="状态">
        </el-table-column>
        <el-table-column
          prop="specifications"
          header-align="center"
          align="center"
          width="160"
          :show-overflow-tooltip="true"
          label="型号规格">
        </el-table-column>
        <el-table-column
          prop="manufacturers"
          header-align="center"
          align="center"
          width="160"
          :show-overflow-tooltip="true"
          label="制造商">
        </el-table-column>
        <el-table-column
          prop="verificationDate"
          header-align="center"
          align="center"
          min-width="160"
          label="检定日期">
        </el-table-column>
        <el-table-column
          prop="validityDate"
          header-align="center"
          align="center"
          min-width="160"
          label="检定有效期（止）">
        </el-table-column>
        <el-table-column
          prop="-"
          header-align="center"
          align="center"
          min-width="200"
          label="使用说明书">
          <template slot-scope="scope">
            <div v-for="(item, index) in scope.row.attachList" style="display: inline">
            <a :key="item.fileName + index" type="primary" :href="downloadUrl + item.url" target="_blank">{{ item.fileName }}</a>
              <span v-if="index > 0">,</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          prop="notes"
          header-align="center"
          align="center"
          width="180"
          :show-overflow-tooltip="true"
          label="备注">
        </el-table-column>
        <el-table-column
          prop="responsibilityUserName"
          header-align="center"
          align="center"
          label="责任人">
        </el-table-column>
        <el-table-column
          prop="userOfName"
          header-align="center"
          align="center"
          label="操作人">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="120"
          label="操作">
          <template slot-scope="scope">
            <el-button v-if="isAuth('prod:equipment:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id, false)">编辑</el-button>
            <el-button v-if="isAuth('prod:equipment:updateState') && Number(scope.row.state) < 3" type="text" size="small" @click="damaged(scope.row.id)">报损</el-button>
  <!--          <el-button v-if="isAuth('prod:equipment:record')" type="text" size="small" @click="addOrUpdateHandle(scope.row.id, true)">点检记录</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>
  </div>
</template>

<script>
  import AddOrUpdate from './equipment-add-or-update'
  import { getEquipmentList } from '@/api/production'
  import { downloadUrl } from '@/api/file'
  export default {
    name: 'equipment',
    components: {
      AddOrUpdate
    },
    data () {
      return {
        addOrUpdateVisible: false,
        dataForm: {},
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        // 设备状态（1:正常，2:未检定，3:损坏，4:维修中）
        optionsState: [
          {
            code: '1',
            value: '正常'
          },
          {
            code: '2',
            value: '未检定'
          },
          {
            code: '3',
            value: '损坏'
          },
          {
            code: '4',
            value: '维修中'
          }
        ],
        downloadUrl: downloadUrl
      }
    },
    created () {
      this.getDataList()
    },
    methods: {
      onChose () {
        this.addOrUpdateVisible = false
      },
      // 查询
      queryPage () {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.addOrUpdateVisible = false
        let params = {
          'current': this.pageIndex,
          'size': this.pageSize,
          'name': this.dataForm.name ? this.dataForm.name : null,
          'state': this.dataForm.state ? this.dataForm.state : null
        }
        getEquipmentList(params).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)
        })
      },
      // 报损
      damaged (id) {
        if (!id) return
        this.$confirm(`确定报损?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl(`/biz-service/equipment/updateState/${id}`),
            method: 'POST'
          }).then(({data}) => {
            if (data && data.code === '200') {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }).catch(() => {})
      },
      // 转换属性“状态”
      formatState (row) {
        if (!row.state) return ''
        const item1 = this.optionsState.find((item) => item.code === row.state.toString())
        return item1 ? item1.value : ''
      }
    }
  }
</script>

<style scoped>

</style>
