<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="typeName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入工器具名称"
          clearable
          :maxlength="20"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="typeModelName">
        <el-input
          v-model="queryParams.typeModelName"
          placeholder="请输入规格型号"
          clearable
          :maxlength="20"
          style="width: 240px"
        />
      </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" :max-height="650">
      <!-- houseName type typeName typeModelName unitName storeNum manageType usNum  repairNum inputNum repairInputNum allNum-->
      <el-table-column label="序号" align="center" type="index">
        <template scope="scope">
          <span v-if="scope.$index == 0">合计：</span>
          <span v-else>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index }}</span>
        </template>
      </el-table-column>
      <el-table-column label="工程名称" align="center" prop="proName" width="200" />
      <el-table-column label="施工类型" align="center" prop="constructionType" :show-overflow-tooltip="true" />
      <el-table-column label="物资类型" align="center" prop="materialType" :show-overflow-tooltip="true" />
      <el-table-column label="物资名称" align="center" prop="typeName" :show-overflow-tooltip="true" />
      <el-table-column label="规格型号" align="center" prop="typeModelName" :show-overflow-tooltip="true" />
      <el-table-column label="计量单位" align="center" prop="unit" :show-overflow-tooltip="true" />
      <el-table-column label="在库数量" align="center" prop="storeNum" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <!-- 当 manageType 为 '编码' 时，使用可点击的 span -->
          <span
            class="clickText"
            v-if="scope.row.manageType == '编码' && scope.row.storeNum > 0 && scope.$index != 0"
            @click="openRecords(scope.row)"
          >
            {{ scope.row.storeNum }}
          </span>
          <!-- 否则，直接显示数字 -->
          <span v-else>
            {{ scope.row.storeNum }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="在用数量" align="center" prop="usNum" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span
            v-if="scope.row.usNum > 0 && scope.$index != 0"
            class="clickText"
            @click="openUserRecords(scope.row)"
          >
            {{ scope.row.usNum }}
          </span>
          <!-- 否则，直接显示数字 -->
          <span v-else>
            {{ scope.row.usNum }}
          </span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="总保有量" align="center" prop="allNum" :show-overflow-tooltip="true"></el-table-column> -->
      <!-- <el-table-column label="总保有量资产（万元）" align="center" prop="totalPrice" :show-overflow-tooltip="true" /> -->
      <el-table-column label="管理模式" align="center" prop="manageType" :show-overflow-tooltip="true" />
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      :page-sizes="[10, 20, 50, 100]"
      @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 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="typeName" :show-overflow-tooltip="true" />
        <el-table-column label="规格型号" align="center" prop="typeModelName" :show-overflow-tooltip="true" />
        <!-- <el-table-column label="入库数量" align="center" prop="storeNum" :show-overflow-tooltip="true" /> -->
        <el-table-column label="租赁价（元）" align="center" prop="buyPrice" :show-overflow-tooltip="true" />
        <el-table-column label="设备编码" align="center" prop="maCode" :show-overflow-tooltip="true" />
        <!-- <el-table-column label="库管员" align="center" prop="maKeeper" :show-overflow-tooltip="true" /> -->
        <el-table-column label="操作人" align="center" prop="inputUser" :show-overflow-tooltip="true" />
        <el-table-column label="入库时间" align="center" prop="inputTime" :show-overflow-tooltip="true" />
        <!-- <el-table-column label="入库方式" align="center" prop="inputType" :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>
    <!-- 在用编码设备弹窗-->
    <el-dialog :title="title" :visible.sync="openUseRecord" width="1200px" append-to-body>
      <el-form :model="dialogUseQuery" ref="dialogUseQuery" size="small" :inline="true" label-width="80px">
        <el-form-item label="关键字" prop="keyWord">
          <el-input
            v-model="dialogUseQuery.keyWord"
            placeholder="请输入关键字"
            clearable
            :maxlength="20"
            style="width: 240px"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleDialogUseQuery">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-download" size="mini" @click="handleDialogUserExport">导出</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="useRecordList">
        <el-table-column
          label="序号"
          align="center"
          width="80"
          type="index"
          :index="indexContinuation(dialogUseQuery.pageNum, dialogUseQuery.pageSize)"
        ></el-table-column>
        <el-table-column label="机具名称" align="center" prop="typeName" :show-overflow-tooltip="true" />
        <el-table-column label="规格型号" align="center" prop="typeModelName" :show-overflow-tooltip="true" />
        <el-table-column label="在用数量" align="center" prop="usNum" :show-overflow-tooltip="true" />
        <!-- <el-table-column label="租赁价（元）" align="center" prop="buyPrice" :show-overflow-tooltip="true" /> -->
        <el-table-column label="设备编码" align="center" prop="maCode" :show-overflow-tooltip="true" />
        <el-table-column label="操作人" align="center" prop="creator" :show-overflow-tooltip="true" />
        <el-table-column label="出库时间" align="center" prop="outTime" :show-overflow-tooltip="true" width="180" />
        <el-table-column label="班组" align="center" prop="teamName" :show-overflow-tooltip="true" />
        <el-table-column label="工程名称" align="center" prop="proName" :show-overflow-tooltip="true" width="180"/>
      </el-table>

      <pagination
        v-show="dialogUserTotal > 0"
        :total="dialogUserTotal"
        :page.sync="dialogUseQuery.pageNum"
        :limit.sync="dialogUseQuery.pageSize"
        @pagination="getUserRecords"
      />
    </el-dialog>
  </div>
</template>

<script>
import {
  getStorageInfoListApi,
  getUserRecordListApi,
  getStoreNumAndUseListApi,
  getRetainedEquipmentListNoPageApi
} from '@/api/materialsStation'
export default {
  name: 'ProDetails',
  dicts: [],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 弹出层标题
      title: '查看',
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 主表格数据
      tableList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyWord: null,
        typeName: null, //物资名称
        typeModelName: null //规格型号
      },

      //在库弹窗
      openRecord: false,
      dialogList: [], //在库编码设备列表
      dialogQuery: {
        pageNum: 1,
        pageSize: 10,
        keyWord: undefined
      },
      dialogTotal: 0,
      //在用弹窗
      openUseRecord: false,
      dialogUseQuery: {
        pageNum: 1,
        pageSize: 10,
        keyWord: undefined
      },
      useRecordList: [], //在用设备列表
      dialogUserTotal: 0
    }
  },
  created() {
    const query = this.$route.query || {}
    console.log('🚀 ~ created ~ query:', query)
    this.queryParams.proId = query.proId || ''
    this.queryParams.proCenter = query.proCenter || ''
    this.getList()
  },
  methods: {
    // 获取列表
    async getList() {
      this.loading = true
      const params = {
        proId: this.queryParams.proId || '',
        proCenter: this.queryParams.proCenter || '',
        keyWord: this.queryParams.keyWord,
        typeName: this.queryParams.typeName,
        typeModelName: this.queryParams.typeModelName,
        pageSize: this.queryParams.pageSize,
        pageNum: this.queryParams.pageNum
      }
      try {
        const res = await getStoreNumAndUseListApi(params)
        this.tableList = res.data.rows || []
        console.log('🚀 ~ getList ~ this.tableList:', this.tableList)
        this.total = res.data.total || 0
        let param = {
          proId: this.queryParams.proId || '',
          proCenter: this.queryParams.proCenter || '',
          keyWord: this.queryParams.keyWord,
          typeName: this.queryParams.typeName,
          typeModelName: this.queryParams.typeModelName
        }
        const response = await getRetainedEquipmentListNoPageApi(param)
        let obj = {
          // storeNum manageType usNum  repairNum inputNum repairInputNum allNum
          storeNum: response.data.storeNum || 0,
          usNum: response.data.usNum || 0,
          repairNum: response.data.repairNum || 0,
          inputNum: response.data.inputNum || 0,
          repairInputNum: response.data.repairInputNum || 0,
          pendingScrapNum: response.data.pendingScrapNum || 0,
          scrapNum: response.data.scrapNum || 0,
          allNum: response.data.allNum || 0,
          totalPrice: response.data.totalPrice || 0,
          fiveReplacementRate: response.data.fiveReplacementRate || 0,
          tenReplacementRate: response.data.tenReplacementRate || 0,
          tenPlusReplacementRate: response.data.tenPlusReplacementRate || 0,
          manageType: response.data.manageType || ''
        }
        this.tableList.unshift(obj)
        console.log(this.tableList)
        this.loading = false
      } catch (error) {
        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 = 6
        return { rowspan, colspan }
      }
      // 解决偏移的重要代码
      // 重点在 else if 判断必须加因为合并单元格数据会出现偏移、
      // 否则如果是 第二行 的 第二列 那么隐藏原有第二行 的 第二列的数据使他隐藏
      else if (rowIndex === 0 && columnIndex < 6) {
        return {
          rowspan: 0,
          colspan: 0
        }
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'material/material_maMachine/exportStoreNumAndUseList',
        {
          ...this.queryParams
        },
        `站内库存查询_${new Date().getTime()}.xlsx`
      )
    },
    //查看在库编码数据
    openRecords(row) {
      this.openRecord = true
      this.dialogQuery.keyWord = ''
      this.dialogQuery.typeId = row.typeId
      this.dialogQuery.proId = row.proId
      this.getStoreCodeList()
    },
    /** 查询在库编码列表 */
    getStoreCodeList() {
      getStorageInfoListApi(this.dialogQuery).then(response => {
        this.dialogList = response.data.rows
        this.dialogTotal = response.data.total
      })
    },
    /** 搜索在库按钮操作 */
    handleDialogQuery() {
      this.dialogQuery.pageNum = 1
      this.getStoreCodeList()
    },
    handleDialogStoreExport() {
      this.download(
        'material/material_maMachine/exportStorageInfo',
        { ...this.dialogQuery },
        `站内库存查询_在库设备_${new Date().getTime()}.xlsx`
      )
    },
    // ------------------
    //查看在用设备
    openUserRecords(row) {
      this.openUseRecord = true
      this.dialogUseQuery.typeId = row.typeId
      this.dialogUseQuery.keyWord = ''
      this.dialogUseQuery.proId = row.proId
      this.getUserRecords()
    },
    /** 查询在用设备列表 */
    getUserRecords() {
      getUserRecordListApi(this.dialogUseQuery).then(response => {
        this.useRecordList = response.data.rows
        this.dialogUserTotal = response.data.total
      })
    },
    /** 搜索在用按钮操作 */
    handleDialogUseQuery() {
      this.dialogUseQuery.pageNum = 1
      this.getUserRecords()
    },
    handleDialogUserExport() {
      this.download(
        'material/material_maMachine/exportUserRecordList',
        { ...this.dialogUseQuery },
        `站内库存查询_在用设备_${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>
