<!--
 * @Author: ysh
 * @Date: 2024-03-27 11:10:26
 * @LastEditors: Please set LastEditors
 * @LastEditTime: 2024-06-11 10:54:16
-->
<template>
  <div>
    <el-form :inline="true">
      <el-form-item label="设备状态：">
        <el-select v-model="queryParams.status" filterable clearable @change="getList">
          <el-option :value="'0'" label="被领用"></el-option>
          <el-option :value="'1'" label="可领用"></el-option>
          <el-option :value="'2'" label="维修中"></el-option>
          <el-option :value="'3'" label="已停用"></el-option>
          <el-option :value="'4'" label="已报废"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="设备名称：">
        <el-select v-model="queryParams.name" filterable @change="getList" clearable>
          <el-option v-for="item in nameList" :key="item.name" :label="item.name" :value="item.name">
          </el-option>
        </el-select>
        <!-- <el-input v-model="queryParams.name" clearable></el-input> -->
      </el-form-item>
      <el-form-item label="品牌：">
        <el-input v-model="queryParams.brand" clearable @keyup.enter.native="getList"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="getList" type="primary">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table ref="chooseCar" :data="list" @selection-change="handleSelectionChange" :row-key="getRowKeys">
      <el-table-column type="selection" width="50" align="center" :reserve-selection="true" />
      <el-table-column label="设备状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag :type="statusType(scope.row.status)">
            {{ statusTypeText(scope.row.status) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="出厂编号" align="center" prop="code" />
      <el-table-column label="设备品牌" align="center" prop="brand" />
      <el-table-column label="设备名称" align="center" prop="name" />
      <el-table-column label="规格型号" align="center" prop="series" />
      <el-table-column label="存放地址" align="center" prop="place" />
      <el-table-column label="单日成本" align="center" prop="dayCost" />
    </el-table>
    <div style="text-align: right;">
      <el-pagination @current-change="getList" :current-page.sync="queryParams.pageNum"
        :page-size="queryParams.pageSize" layout="total, prev, pager, next" :total="total">
      </el-pagination>
    </div>
    <div>
      已选设备：
      <el-tag v-for="item in chooseList" style="margin: 5px;" :key="item.deviceIdId">
        {{ item.name + '-' + item.series + '-' + item.brand }}
      </el-tag>
      <div style="text-align: center;margin-top: 20px;">
        <el-button type="primary" @click="confirmChoose">确认选择</el-button>
        <el-button @click="clearChoose">清空选择</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { listDevice,listDeviceName } from "@/api/oa/device/device";
export default {
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: '仪器设备'
      },
      list: [],
      form: {},
      total: 0,
      loading: false,
      chooseList: [],
      nameList: [],
    }
  },
  created() {
    this.getList();
    this.getNameList();
  },
  methods: {
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getNameList(){
      listDeviceName().then(res=>{
        this.nameList = res.data
      })
    },
    handleSelectionChange(val) {
      this.chooseList = val
    },
    getRowKeys(row) {
      return row.deviceId;
    },
    confirmChoose() {
      this.$emit('chooseList', this.chooseList)
    },
    clearChoose() {
      this.$refs.chooseCar.clearSelection();
    },
    statusTypeText(row) {
      if (row == '0') {
        return '被领用'
      }
      if (row == '1') {
        return '可领用'
      }
      if (row == '2') {
        return '维修中'
      }
      if (row == '3') {
        return '已停用'
      }
      if (row == '4') {
        return '已报废'
      }
    },
    statusType(row){
      if (row == '0') {
        return 'warning'
      }
      if (row == '1') {
        return 'success'
      }
      if (row == '2') {
        return 'info'
      }
      if (row == '3') {
        return 'danger'
      }
      if (row == '4') {
        return 'info'
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>