<template>
  <!-- 基础页面 -->
  <div class="app-container">
    <el-form v-show="showSearch" :model="queryParams" ref="queryForm" size="small" inline @submit.native.prevent>
      <!-- 日期范围 -->
      <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 label="关键字" prop="keyWord">
        <el-input
          v-model="queryParams.keyWord"
          placeholder="请输入关键字"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 240px"
        />
      </el-form-item>

      <!-- 表单按钮 -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" @click="handleReset">重置</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 :data="tableList" fit highlight-current-row style="width: 100%" :max-height="650">
      <!-- 多选 -->
      <!-- <el-table-column type="selection" width="55" align="center" @selection-change="selectionChange" /> -->
      <el-table-column
        type="index"
        width="55"
        label="序号"
        align="center"
        :index="index => (queryParams.pageNum - 1) * queryParams.pageSize + index + 1"
      />
      <el-table-column
        v-for="(column, index) in tableColumns"
        :key="index"
        :label="column.label"
        :prop="column.prop"
        align="center"
        :width="column.width"
      ></el-table-column>
      <!-- 操作 -->
      <el-table-column label="操作" align="center" width="220">
        <template slot-scope="{ row }">
          <el-button type="text" size="mini" icon="el-icon-zoom-in" @click="handleView(row)">查看</el-button>
          <!-- <el-button
            type="text"
            size="mini"
            icon="el-icon-document"
            style="color: #67c23a"
            @click="handleMaterial(row)"
          >
            领料单
          </el-button>
          <el-button type="text" size="mini" icon="el-icon-document" style="color: #67c23a" @click="handleCheck(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="dialogTitle" :visible.sync="dialogVisible" width="60%">
      <el-table :data="dialogList" fit highlight-current-row style="width: 100%">
        <el-table-column
          type="index"
          width="55"
          label="序号"
          align="center"
          :index="index => (dialogForm.pageNum - 1) * dialogForm.pageSize + index + 1"
        />
        <el-table-column
          v-for="(column, index) in dialogColumns"
          show-overflow-tooltip
          :key="index"
          :label="column.label"
          :prop="column.prop"
          align="center"
        ></el-table-column>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="dlgTotal > 0"
        :total="dlgTotal"
        :page.sync="dialogForm.pageNum"
        :limit.sync="dialogForm.pageSize"
        @pagination="getDetailsList"
      />
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关 闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getTotalListApi, getDetailsListApi } from '@/api/materialsStation'

export default {
  data() {
    return {
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyWord: '', // 关键字
        proStatus: '', // 状态
        timeRange: [], // 日期范围
        startTime: '', // 开始时间
        endTime: '' // 结束时间
      },
      total: 0, // 总条数
      // 表头
      tableColumns: [
        { label: '租赁单位', prop: 'leaseUnit' },
        { label: '租赁工程', prop: 'leaseProject' },
        { label: '领用物资类型', prop: 'maTypeNames' },
        { label: '领料人', prop: 'leasePerson', width: '90' },
        { label: '领料时间', prop: 'leaseTime', width: '100' },
        { label: '领料单号', prop: 'code' }
      ],
      // 表格数据
      tableList: [],
      dlgTotal: 0, // 弹框总条数
      dialogTitle: '查看',
      dialogVisible: false,
      dialogForm: {
        pageNum: 1,
        pageSize: 10,
        keyWord: '' // 关键字
      },
      dialogColumns: [
        { label: '类型名称', prop: 'typeCode' },
        { label: '规格型号', prop: 'modelCode' },
        { label: '计量单位', prop: 'unitNames' },
        { label: '设备编码', prop: 'maCode' },
        { label: '预领数量', prop: 'preCountNum' },
        { label: '出库数量', prop: 'preCountNum' },
        { label: '备注', prop: 'remark' }
      ],
      dialogList: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 查询
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.$refs.queryForm.resetFields()
      this.getList()
    },
    // 获取列表
    async getList() {
      console.log('列表-查询', this.queryParams)
      const loading = this.$loading({ text: '加载中...' })
      this.queryParams.startTime = this.queryParams.timeRange ? this.queryParams.timeRange[0] : ''
      this.queryParams.endTime = this.queryParams.timeRange ? this.queryParams.timeRange[1] : ''
      try {
        const params = { ...this.queryParams }
        const res = await getTotalListApi(params)
        console.log('🚀 ~ 获取列表 ~ res:', res)
        this.tableList = res.data.rows
        this.total = res.data.total
        loading.close()
      } catch (error) {
        console.log('🚀 ~ 获取列表 ~ error:', error)
        this.tableList = []
        this.total = 0
        loading.close()
      }
    },
    // 获取详情列表
    async getDetailsList() {
      try {
        const res = await getDetailsListApi({ ...this.dialogForm })
        console.log('🚀 ~ 获取详情列表 ~ res:', res)
        this.dialogList = res.data.rows
        this.dlgTotal = res.data.total
      } catch (error) {
        console.log('🚀 ~ 获取详情列表 ~ error:', error)
        this.dialogList = []
        this.dlgTotal = 0
      }
    },
    // 查看
    handleView(row) {
      console.log('查看', row)
      this.dialogTitle = '查看'
      this.dialogVisible = true
      setTimeout(() => {
        this.dialogForm.code = row.code
        this.getDetailsList()
      }, 100)
    },
    // 领料单
    handleMaterial(row) {
      console.log('领料单', row)
      this.$message({
        type: 'warning',
        message: '领料单功能开发中，敬请期待！'
      })
    },
    // 出库检验单
    handleCheck(row) {
      console.log('出库检验单', row)
      this.$message({
        type: 'warning',
        message: '出库检验单功能开发中，敬请期待！'
      })
    },
    // 导出数据
    handleExport() {
      // 提示
      this.$message({
        type: 'warning',
        message: '导出功能开发中，敬请期待！'
      })
      try {
        let fileName = `数据_${new Date().getTime()}.xLsx`
        let url = ''
        const params = { ...this.queryParams }
        console.log('🚀 ~ 导出 ~ params:', params)
        // this.derive(url, params, fileName)
        // this.download(url, params, fileName)
      } catch (error) {
        console.log('导出数据失败', error)
      }
    }
  }
}
</script>

<style lang="scss" scoped></style>
