<template>
  <div class="app-container">
    <!-- 表单 -->
    <el-form :inline="true" :model="queryForm" ref="queryForm" label-width="68px" size="small" v-show="showSearch">
      <el-form-item label="关键字" prop="keyWord">
        <el-input v-model="queryForm.keyWord" clearable placeholder="请输入关键字" />
      </el-form-item>
      <!-- 物品种类下拉 -->
      <el-form-item label="物品种类" prop="goodsType">
        <el-select v-model="queryForm.goodsType" placeholder="请选择物品种类" filterable clearable>
          <el-option label="物品种类1" value="1"></el-option>
          <el-option label="物品种类2" value="2"></el-option>
        </el-select>
      </el-form-item>
      <!-- 设备类型下拉 -->
      <el-form-item label="设备类型" prop="deviceType">
        <el-select v-model="queryForm.deviceType" placeholder="请选择设备类型" filterable clearable>
          <el-option label="设备类型1" value="1"></el-option>
          <el-option label="设备类型2" value="2"></el-option>
        </el-select>
      </el-form-item>
      <!-- 规格型号下拉 -->
      <el-form-item label="规格型号" prop="specificationModel">
        <el-select v-model="queryForm.specificationModel" placeholder="请选择规格型号" filterable clearable>
          <el-option label="规格型号1" value="1"></el-option>
          <el-option label="规格型号2" value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" plain @click="resetQueryForm">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <!-- 列表 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="50"
        :index="indexContinuation(queryParams.pageNum, queryParams.pageSize)"
      />
      <el-table-column prop="machineryNum" label="机具编号" align="center" show-overflow-tooltip />
      <el-table-column prop="machineryName" label="机具名称" align="center" show-overflow-tooltip />
      <el-table-column prop="machineryType" label="机具类型" align="center" show-overflow-tooltip />
      <el-table-column prop="specificationModel" label="规格型号" align="center" show-overflow-tooltip />
      <el-table-column prop="rentalDate" label="租赁日期" align="center" show-overflow-tooltip>
        <template slot-scope="{ row }">
          <span :class="{ isWarning: row.activeLease }">{{ row.rentalDate }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="inspectionDate" label="检验日期" align="center" show-overflow-tooltip />
      <el-table-column prop="nextInspectionDate" label="下次检验日期" align="center" show-overflow-tooltip>
        <template slot-scope="{ row }">
          <span :class="{ isWarning: row.activeCheck }">{{ row.nextInspectionDate }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="source" label="来源" align="center" show-overflow-tooltip />
      <el-table-column prop="status" label="状态" align="center" show-overflow-tooltip />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
export default {
  name: 'RentalEquipmentManagement',
  data() {
    return {
      loading: false,
      showSearch: true,
      queryForm: {
        keyWord: '',
        goodsType: '',
        deviceType: '',
        specificationModel: '',
      },
      tableData: [
        {
          machineryNum: '001',
          machineryName: '机具1',
          machineryType: '机具类型1',
          specificationModel: '规格型号1',
          rentalDate: '2024-07-01',
          inspectionDate: '2024-08-01',
          nextInspectionDate: '2024-08-15',
          source: '来源1',
          status: '状态1',
        },
        {
          machineryNum: '002',
          machineryName: '机具2',
          machineryType: '机具类型2',
          specificationModel: '规格型号2',
          rentalDate: '2024-09-02',
          inspectionDate: '2024-08-02',
          nextInspectionDate: '2024-08-02',
          source: '来源2',
          status: '状态2',
        },
      ],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      total: 0,
    }
  },
  created() {
    this.getList()
    this.getGoodsTypeList()
    this.getDeviceTypeList()
    this.getSpecificationModelList()
  },
  methods: {
    // 搜索
    handleQuery() {
      console.log('🚀 ~ handleQuery ~ 搜索:',)
      this.getList()
    },
    // 重置
    resetQueryForm() {
      console.log('🚀 ~ resetQueryForm ~ 重置:')
      this.$refs.queryForm.resetFields()
      this.getList()
    },
    // 获取列表
    getList() {
      const params = {
        ...this.queryParams,
        ...this.queryForm,
      }
      console.log('🚀 ~ getList ~ 获取列表:', params)
      this.warning()
    },
    // 获取物品种类
    getGoodsTypeList() {
      console.log('🚀 ~ getGoodsTypeList ~ 获取物品种类下拉:')
    },
    // 获取设备类型
    getDeviceTypeList() {
      console.log('🚀 ~ getDeviceTypeList ~ 获取设备类型下拉:')
    },
    // 获取规格型号
    getSpecificationModelList() {
      console.log('🚀 ~ getSpecificationModelList ~ 获取规格型号下拉:')
    },
    // 预警 当前日期到租赁到期日期小于等于10天开始预警提醒，字体变为红色 当前日期到下次检验日期小于等于30天开始预警,字体变为红色
    warning() {
      // 获取当前日期
      const nowDate = new Date()
      // 循环列表
      this.tableData.forEach((item) => {
        // 获取租赁日期
        const rentalDate = new Date(item.rentalDate)
        // 获取下次检验日期
        const nextInspectionDate = new Date(item.nextInspectionDate)
        // 计算租赁日期到期时间差 单位：天: 租赁日期 - 当前日期 <= 10 天  开始预警
        const leaseTime = (rentalDate - nowDate) / (1000 * 60 * 60 * 24)
        if (leaseTime <= 10) {
          item.activeLease = true
        }
        // 计算下次检验日期时间差 单位：天: 下次检验日期 - 当前日期 <= 30 天  开始预警
        const checkTime = (nextInspectionDate - nowDate) / (1000 * 60 * 60 * 24)
        if (checkTime <= 30) {
          item.activeCheck = true
        }
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.isWarning {
  color: red;
}
</style>
