<template>
  <div class="box-center">
    <!-- 头 -->
    <div class="app-top">
      <div class="app-titel" style="margin-top: 5px">
        <div class="div-lab">
          <el-input
            v-model="changeValue"
            clearable
            placeholder="请输入"
            maxlength="20"
            @change="selectChange"
            style="width: 363px"
          >
            <el-select
              v-model="selflag"
              slot="prepend"
              class="input-with-select"
              style="width: 115px"
              placeholder="请选择"
              @change="selChange"
            >
              <el-option label="设备编号" value="1"></el-option>
              <el-option label="领用人" value="2"></el-option>
            </el-select>
          </el-input>
        </div>

        <div class="div-lab">
          <div class="label">领用状态:</div>
          <el-select
            class="div-inp"
            clearable
            collapse-tags
            v-model="searchForm.receiveType"
            placeholder="请选择"
          >
            <el-option
              v-for="item in returnList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>
        <div style="margin-left: 20px">
          <el-button type="primary" @click="screen">筛选</el-button>
        </div>
        <div style="margin-left: 20px">
          <el-button @click="clascreen">清空筛选条件</el-button>
        </div>
        <div style="margin-right: 20px;margin-left:auto;">
          <el-button @click="exportFile">导出</el-button>
        </div>
      </div>
    </div>

    <!-- 表格 -->
    <div class="cen-tab">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="equipImei" label="设备编号" align="center">
        </el-table-column>
        <el-table-column prop="userName" label="领用人" align="center">
        </el-table-column>
        <el-table-column prop="phone" label="领用人手机" align="center">
        </el-table-column>

        <el-table-column
          prop="receiveBeginTime"
          label="领用时间"
          align="center"
        >
        </el-table-column>
        <el-table-column
          prop="receiveOperationName"
          label="领用操作人"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="receiveEndTime" label="归还时间" align="center">
        </el-table-column>
        <el-table-column
          prop="backOperationName"
          label="归还操作人"
          align="center"
        >
        </el-table-column>
        <el-table-column prop="receiveType" label="领用状态" align="center">
          <template slot-scope="{ row }">
            {{
              row.receiveType == 1
                ? "未领用"
                : row.receiveType == 2
                ? "使用中"
                : "已归还"
            }}
          </template>
        </el-table-column>
        <!-- <el-table-column prop="date" label="备注" align="center">
        </el-table-column> -->
        <!--  scope -->
      </el-table>
      <div style="display: flex; justify-content: flex-end; margin-top: 10px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 30, 50]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage: 4,
      value: "",
      input: "",
      tableData: [],
      multipleSelection: [],
      changeValue: "",
      dialogVisible: false,
      houseList: [],
      ruleForm: {
        correctWord: "",
        wrongWord: "",
        memo: "",
      },
      searchForm: {
        equipImei: "",
        userName: "",
        receiveType: "",
      },
      returnList: [
        {
          value: "1",
          label: "未领用",
        },
        {
          value: "2",
          label: "使用中",
        },
        {
          value: "3",
          label: "已归还",
        },
      ],
      currentPage: 1,
      selflag: "1",
      editFlag: false,
      size: 10,
      total: 10,
      rules: {
        correctWord: [
          { required: true, message: "请填写正确词", trigger: "change" },
        ],
        wrongWord: [
          { required: true, message: "请填写错误词", trigger: "change" },
        ],
      },
    };
  },
  mounted() {
    // 获取楼盘列表
    this.equipReceiveFindByPage();
  },
  methods: {
    selectChange(e) {
      this.searchForm.equipImei = "";
      this.searchForm.userName = "";
      if (!e) retun;
      if (this.selflag == 1) {
        // 设备编号
        this.searchForm.equipImei = e;
      }
      if (this.selflag == 2) {
        // 领用人
        this.searchForm.userName = e;
      }
    },
    selChange() {
      this.changeValue = "";
      this.searchForm.equipImei = "";
      this.searchForm.userName = "";
    },
    screen() {
      this.currentPage = 1;
      this.equipReceiveFindByPage();
    },
    exportFile() {
      this.exportMethodPost("autoSR/equipReceive/pageExport", "领用记录",this.searchForm);
    },
    // 导出.Excel公用方法
    exportMethodPost(url, name, data = {}) {
      axios({
        method: "get",
        url: url,
        params:data,
        responseType: "blob",
      })
        .then((res) => {
          let blob = new Blob([res], { type: "application/vnd.ms-excel" });
          let date = new Date();
          let time = date.toLocaleDateString();
          // console.log(time, "时间");
          if ("download" in document.createElement("a")) {
            const link = document.createElement("a");
            link.style.display = "none";
            link.href = URL.createObjectURL(blob);
            // link.download = res.headers['content-disposition'] //下载后文件名
            link.download = (name || "导出文件") + time + ".xlsx"; //下载的文件名
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
          } else {
            // console.log("--------------------jingla")
            let fileName = (name || "导出文件") + time + ".xlsx"; //下载的文件名
            navigator.msSaveBlob(blob, fileName);
          }
        })
        .catch((error) => {
          // Message.error({
          //     message: '网络连接错误'
          //  })
          console.log(error);
        });
    },
    clascreen() {
      this.currentPage = 1;
      this.searchForm.equipImei = "";
      this.searchForm.userName = "";
      this.searchForm.receiveType = "";
      this.equipReceiveFindByPage();
    },
    // 获取常错词列表
    equipReceiveFindByPage() {
      let obj = {
        current: this.currentPage,
        size: this.size,
        ...this.searchForm,
      };
      this.$api.api.equipReceiveFindByPage(obj).then((res) => {
        // console.log(res);
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val;
      this.equipReceiveFindByPage();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.equipReceiveFindByPage();
    },
  },
};
</script>

<style scoped="scoped" lang="scss" >
.box-center {
  width: 100%;
  padding: 15px;
  min-width: 1000px;
  padding-bottom: 100px;
}
.cen-tab {
  width: 100%;
  padding: 15px;
  background: #ffffff;
  margin-top: 15px;
}
.tophove {
  color: #ffffff;
  background: #2671e2;
}
.app-top {
  width: 100%;
  background: #ffffff;
  box-shadow: 0px 0px 12px 0px rgba(0, 0, 0, 0.04);
  border-radius: 4px;
  padding-top: 15px;
  padding-bottom: 15px;
  .app-titel {
    width: 100%;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    .label {
      font-size: 14px;
      font-weight: 400;
      color: #32363d;
      line-height: 32px;
      margin-left: 15px;
      min-width: 78px;
    }
    .toptimeqhuan {
      height: 30px;
      background: #ffffff;
      display: flex;
      align-items: center;
    }
    .toptimeqhuan div {
      padding-left: 20px;
      padding-right: 20px;
      text-align: center;
      line-height: 30px;
      font-size: 14px;
      margin-right: 15px;
      border-radius: 4px;
      border: 1px solid #e0e0e0;
    }
  }
}
.div-lab {
  display: flex;
  margin: 5px;
}
.div-inp {
  width: 250px;
}
</style>
