<template>
  <!-- 库存日志 -->
  <div class="app-container">
    <el-row>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
        <el-form-item label="模块名称" prop="modelTitle">
          <el-input
            v-model="queryParams.modelTitle"
            placeholder="请输入模块名称"
            clearable
            style="width: 200px"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="机具类型" prop="typeName">
          <el-cascader
            placeholder="请选择机具类型"
            :options="deviceTypeTree"
            :props="{ checkStrictly: true, value: 'label' }"
            @change="handleBaseValue"
            clearable
            filterable
            :show-all-levels="false"
          />
        </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>

    <el-table :data="tableList" border>
      <el-table-column align="center" label="序号" type="index" width="60" />
      <el-table-column align="center" prop="modelTitle" label="模块名称" show-overflow-tooltip />
<!--      <el-table-column show-overflow-tooltip align="center" prop="method" label="请求方式" />-->
<!--      <el-table-column show-overflow-tooltip align="center" prop="method" label="请求地址" />-->
<!--      <el-table-column show-overflow-tooltip align="center" prop="taskId" label="任务ID" />-->
      <el-table-column show-overflow-tooltip align="center" prop="typeParentName" label="机具类型" />
      <el-table-column show-overflow-tooltip align="center" prop="typeName" label="机具规格" />
      <el-table-column show-overflow-tooltip align="center" prop="preStoreNum" label="操作前库存" />
      <el-table-column show-overflow-tooltip align="center" prop="inNum" label="入库数量" />
      <el-table-column show-overflow-tooltip align="center" prop="outNum" label="出库数量" />
      <el-table-column show-overflow-tooltip align="center" prop="passNum" label="(退库)合格数量" />
      <el-table-column show-overflow-tooltip align="center" prop="maintenanceNum" label="(退库)维修数量" />
      <el-table-column show-overflow-tooltip align="center" prop="scrapNum" label="(退库)报废数量" />
<!--      <el-table-column show-overflow-tooltip align="center" prop="description" label="描述" />-->
      <el-table-column show-overflow-tooltip align="center" prop="postStoreNum" label="操作后库存" />
      <el-table-column show-overflow-tooltip align="center" prop="createTime" label="日期" />
      <el-table-column align="center" label="状态">
        <template slot-scope="{ row }">
          <el-tag size="mini" type="success" v-if="row.status == 0">正常</el-tag>
          <el-tag size="mini" type="warning" v-else>异常</el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column align="center" prop="creator" label="创建人" />-->
<!--      <el-table-column align="center" prop="remark" label="备注" />-->
      <el-table-column align="center" label="操作">
        <template slot-scope="{ row }">
          <el-button size="mini" type="text" @click="previewLogsDetails(row)">详情</el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 详情弹框 -->
    <el-dialog title="日志详情" :visible.sync="logsDetailsDialogVisible" width="60%">
      <el-descriptions
        class="margin-top"
        :column="3"
        size="medium"
        border
        :labelStyle="commonStyle"
        :contentStyle="commonStyle"
      >
        <el-descriptions-item v-for="(item, v) in detailsLabelList" :key="v">
          <template slot="label">{{ item.label }}</template>
          {{ logsDetailsInfo[item.content] || '' }}
        </el-descriptions-item>
      </el-descriptions>
    </el-dialog>
  </div>
</template>

<script>
import { getLogsListApi, getLogsDetailsApi } from '@/api/inventoryLogs'
import { getDeviceTypeTree } from '@/api/claimAndRefund/receive'
export default {
  name: 'myInventoryLogs',
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        modelTitle: '', // 模块名称
        typeName: '', // 机具名称
      },
      tableList: [],
      total: 5,
      logsDetailsInfo: null /* 日志详情 */,
      logsDetailsDialogVisible: false,
      commonStyle: {
        'text-align': 'center',
      },
      /* 日志详情label以及内容 */
      detailsLabelList: [
        { label: '创建人', content: 'creator' },
        { label: '模块名称', content: 'modelTitle' },
        { label: '任务ID', content: 'taskId' },
        { label: '机具类型', content: 'typeParentName' },
        { label: '机具规格', content: 'typeName' },
        { label: '日期', content: 'createTime' },
        // { label: '申请数量', content: 'preNum' },
        // { label: '审核数量', content: 'auditNum' },
        { label: '操作前库存', content: 'preStoreNum' },
        { label: '入库数量', content: 'inNum' },
        { label: '出库数量', content: 'outNum' },
        { label: '(退库)总数量', content: 'backNum' },
        { label: '(退库)合格数量', content: 'passNum' },
        { label: '(退库)维修数量', content: 'maintenanceNum' },
        { label: '(退库)报废数量', content: 'scrapNum' },
        { label: '操作后库存', content: 'postStoreNum' },
        { label: '请求地址', content: 'method' },
      ],
      logsDetailsInfo: {},
      deviceTypeTree: [], // 设备 树结构数据
    }
  },
  methods: {
    /* 查询列表 */
    async getList() {
      const res = await getLogsListApi(this.queryParams)
      this.tableList = res.rows
      this.total = res.total
    },
    /* 搜索按钮 */
    handleQuery() {
      this.getList()
    },
    /* 重置按钮 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.queryParams.typeName = ''
      this.getList()
    },
    /* 查看详情 */
    async previewLogsDetails(row) {
      console.log(row, '列表id')
      const res = await getLogsDetailsApi(row.id)
      this.logsDetailsInfo = res
      this.logsDetailsDialogVisible = true
    },
    async GetDeviceTypeTree() {
      const params = {
        level: 4,
      }
      const res = await getDeviceTypeTree(params)
      this.deviceTypeTree = res.data
    },
    /* 获取最后一级 */
    handleBaseValue(val) {
      this.queryParams.typeName = val[val.length - 1]
    }
  },
  created() {
    this.getList()
    this.GetDeviceTypeTree()
  },
}
</script>

<style scoped>
    .margin-top {
        margin-top: -15px;
    }
</style>

