<template>
  <div class="app-container" id="">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="关键字" prop="keyWord">
        <el-input
          v-model="queryParams.keyWord"
          placeholder="请输入关键字"
          clearable
          :maxlength="20"
          style="width: 240px"
        />
      </el-form-item>
      <!-- 日期范围 -->
      <el-form-item label="领料日期" prop="timeRange">
        <el-date-picker
          v-model="queryParams.timeRange"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          clearable
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
        />
      </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">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="tableList" :span-method="objectSpanMethod">
      <el-table-column label="序号" align="center" width="80" type="index">
        <template scope="scope">
          <span v-if="scope.$index == 0">合计：</span>
          <span v-else>{{ (queryParams.pageNum - 1) * 10 + scope.$index }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工器具名称" align="center" prop="maTypeName" :show-overflow-tooltip="true" />
      <el-table-column label="规格型号" align="center" prop="typeName" :show-overflow-tooltip="true" />
      <!-- <el-table-column label="预领数量" align="center" prop="preNum" :show-overflow-tooltip="true" /> -->
      <el-table-column label="出库数量" align="center" prop="outNum" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <!-- 当 manageType 为 '编码' 时，使用可点击的 span -->
          <span class="clickText" v-if="scope.row.manageType == '0'" @click="openRecords(scope.row)">
            {{ scope.row.outNum }}
          </span>
          <!-- 否则，直接显示数字 -->
          <span v-else>
            {{ scope.row.outNum }}
          </span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="待出库数量" align="center" prop="pendingNum" :show-overflow-tooltip="true" /> -->
      <el-table-column label="领料日期" align="center" prop="leaseDate" :show-overflow-tooltip="true" />
      <el-table-column label="领料班组" align="center" prop="teamName" :show-overflow-tooltip="true" />
      <el-table-column label="领料工程" align="center" prop="projectName" :show-overflow-tooltip="true" />
      <el-table-column label="班组长" align="center" prop="leasePerson" :show-overflow-tooltip="true" />
      <el-table-column label="出库人" align="center" prop="outPerson" :show-overflow-tooltip="true" />
      <el-table-column label="领料单号" align="center" prop="code" :show-overflow-tooltip="true" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :page-sizes="[5, 10, 15, 20, 30]"
      @pagination="getList"
    />

    <!-- 在库编码设备弹窗-->
    <el-dialog :title="title" :visible.sync="openRecord" width="1200px" append-to-body>
      <el-form :model="dialogQuery" ref="dialogQuery" size="small" :inline="true" label-width="80px">
        <el-form-item label="关键字" prop="keyWord">
          <el-input
            v-model="dialogQuery.keyWord"
            placeholder="请输入关键字"
            clearable
            :maxlength="30"
            style="width: 240px"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleDialogQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-refresh" size="mini" @click="handleDialogReset">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-download" size="mini" @click="handleDialogStoreExport">导出</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="dialogList">
        <el-table-column
          label="序号"
          align="center"
          width="80"
          type="index"
          :index="indexContinuation(dialogQuery.pageNum, dialogQuery.pageSize)"
        ></el-table-column>
        <el-table-column label="机具名称" align="center" prop="materialName" :show-overflow-tooltip="true" />
        <el-table-column label="规格型号" align="center" prop="typeName" :show-overflow-tooltip="true" />
        <el-table-column label="设备编码" align="center" prop="maCode" :show-overflow-tooltip="true" />
        <el-table-column label="出库人员" align="center" prop="outPerson" :show-overflow-tooltip="true" />
        <el-table-column label="出库时间" align="center" prop="outTime" :show-overflow-tooltip="true" />
      </el-table>
      <pagination
        v-show="dialogTotal > 0"
        :total="dialogTotal"
        :page.sync="dialogQuery.pageNum"
        :limit.sync="dialogQuery.pageSize"
        @pagination="getStoreCodeList"
      />
    </el-dialog>
  </div>
</template>

<script>
import { getLeaseInfoDetailsApi, getLeaseInfoDetailsNoPageListApi, getSecondLeaseInfoApi } from '@/api/materialsStation'

export default {
  name: '',
  dicts: [],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 弹出层标题
      title: '查看',
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 主表格数据
      tableList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyWord: null,
        timeRange: null
      },

      //在库弹窗
      openRecord: false,
      dialogList: [], //在库编码设备列表
      dialogQuery: {
        pageNum: 1,
        pageSize: 10,
        keyWord: undefined
      },
      dialogTotal: 0
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取列表
    async getList() {
      this.loading = true
      const params = {
        keyWord: this.queryParams.keyWord,
        pageSize: this.queryParams.pageSize,
        pageNum: this.queryParams.pageNum,
        startTime: this.queryParams.timeRange && this.queryParams.timeRange[0],
        endTime: this.queryParams.timeRange && this.queryParams.timeRange[1]
      }
      const res = await getLeaseInfoDetailsApi(params)
      if (res.data.rows.length > 0) {
        this.tableList = res.data.rows
      } else {
        this.tableList = []
      }
      this.total = res.data.total
      console.log(this.tableList)
      let param = {
        keyWord: this.queryParams.keyWord,
        startTime: this.queryParams.timeRange && this.queryParams.timeRange[0],
        endTime: this.queryParams.timeRange && this.queryParams.timeRange[1]
      }
      const response = await getLeaseInfoDetailsNoPageListApi(param)
      let obj = {
        outNum: response.data.outNum || 0,
        preNum: response.data.preNum || 0,
        pendingNum: response.data.pendingNum || 0
      }
      this.tableList.unshift(obj)
      console.log(this.tableList)
      this.loading = false
    },
    indexContinuationForTable(num, size) {
      let number = (num - 1) * size
      console.log(number)
      if (number == 0) {
        return '合计'
      } else {
        return number
      }
    },
    // 合并单元格 rowIndex=行数  columnIndex=列数
    // 这里是合并table的除表头外的第一行的第二列 + 除表头外的第二行的第二列
    // 注意列数和行数从 0 开始
    objectSpanMethod({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0 && columnIndex == 0) {
        let rowspan = 1
        let colspan = 3
        return { rowspan, colspan }
      }
      // 解决偏移的重要代码
      // 重点在 else if 判断必须加因为合并单元格数据会出现偏移、
      // 否则如果是 第二行 的 第二列 那么隐藏原有第二行 的 第二列的数据使他隐藏
      else if (rowIndex === 0 && columnIndex < 3) {
        return {
          rowspan: 0,
          colspan: 0
        }
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'material/material_lease_apply_info/exportLeaseInfoDetails',
        {
          ...this.queryParams
        },
        `工器具领料记录_${new Date().getTime()}.xlsx`
      )
    },
    //查看在库编码数据
    openRecords(row) {
      this.openRecord = true
      this.dialogQuery.keyWord = ''
      this.dialogQuery.typeId = row.typeId
      this.dialogQuery.parentId = row.parentId
      this.getStoreCodeList()
    },
    /** 查询在库编码列表 */
    getStoreCodeList() {
      getSecondLeaseInfoApi(this.dialogQuery).then(response => {
        this.dialogList = response.data.rows
        this.dialogTotal = response.data.total
      })
    },
    /** 搜索在库按钮操作 */
    handleDialogQuery() {
      this.dialogQuery.pageNum = 1
      this.getStoreCodeList()
    },
    handleDialogReset() {
      this.dialogQuery.pageNum = 1
      this.dialogQuery.keyWord = ''
      this.getStoreCodeList()
    },
    handleDialogStoreExport() {
      this.download(
        'material/material_lease_apply_info/exportSecondLeaseInfo',
        { ...this.dialogQuery },
        `出库数量_${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep.el-table .fixed-width .el-button--mini {
  width: 60px !important;
  margin-bottom: 10px;
}
.clickText {
  color: #02a7f0;
  cursor: pointer;
}
</style>
