<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="设备编号" prop="deviceCode">
        <el-input
          v-model="queryParams.deviceCode"
          placeholder="请输入设备编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="登记有效日期">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

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

    <el-table v-loading="loading" :data="specialDeviceRecordList">
      <el-table-column label="记录编号" align="center" prop="operateCode" />
      <el-table-column label="记录类型" align="center" prop="operateType" >
        <template slot-scope="scope">
          <span v-if="scope.row.operateType == '1'">设备保养计划</span>
          <span v-if="scope.row.operateType == '2'">设备维修工单</span>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="deviceName" />
      <el-table-column label="设备编号" align="center" prop="deviceCode" />
      <el-table-column label="登记有效日期" align="center" prop="effectiveDate" width="180">
        <template slot-scope="scope">
              <span v-if="scope.row.effectiveDate != null && scope.row.effectiveDate != '' && getDays(scope.row.effectiveDate, new Date())<30" style="color: red;">
                {{ scope.row.effectiveDate }}(即将到期)
              </span>
          <span v-if="scope.row.effectiveDate != null && scope.row.effectiveDate != '' && getDays(scope.row.effectiveDate, new Date())>30">
                {{ scope.row.effectiveDate }}
              </span>
          <span v-if="scope.row.effectiveDate == null || scope.row.effectiveDate == ''">-</span>
        </template>
      </el-table-column>
      <el-table-column label="记录状态" align="center" prop="recordStatus" >
        <template slot-scope="scope">
          <span v-if="scope.row.operateType == '1'">
            <span v-if="scope.row.recordStatus == '0'">待确认</span>
            <span v-if="scope.row.recordStatus == '1'">保养中</span>
            <span v-if="scope.row.recordStatus == '2'">待验收</span>
            <span v-if="scope.row.recordStatus == '3'">验收完成</span>
          </span>
          <span v-if="scope.row.operateType == '2'">
            <span v-if="scope.row.recordStatus == '0'">待确认</span>
            <span v-if="scope.row.recordStatus == '1'">维修中</span>
            <span v-if="scope.row.recordStatus == '2'">待验收</span>
            <span v-if="scope.row.recordStatus == '3'">验收完成</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
    </el-table>

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

  </div>
</template>

<script>
import { listSpecialDeviceRecord, getSpecialDeviceRecord, delSpecialDeviceRecord, addSpecialDeviceRecord, updateSpecialDeviceRecord, exportSpecialDeviceRecord } from "@/api/deviceManagement/specialDeviceRecord";

export default {
  name: "SpecialDeviceRecord",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 特种设备台账表格数据
      specialDeviceRecordList: [],
      // 日期范围
      dateRange: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        operateId: null,
        deviceCode: null,
        effectiveDate: null,
        maintenanceRecord: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询特种设备台账列表 */
    getList() {
      this.loading = true;
      listSpecialDeviceRecord(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.specialDeviceRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        operateId: null,
        deviceCode: null,
        effectiveDate: null,
        maintenanceRecord: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    getDays(date1 , date2){
      var time = new Date(date1).getTime() - date2.getTime();
      var days = parseInt(time/1000/60/60/24);

      return days;
    }
  }
};
</script>
