<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%">
      <!-- 多选 -->
      <!-- <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"
      ></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> -->
        </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"
          :key="index"
          :label="column.label"
          :prop="column.prop"
          :width="column.width"
          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 { getBackTotalListApi, getBackDetailsListApi } from '@/api/materialsStation'

export default {
  data() {
    return {
      showSearch: true,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyWord: '', // 关键字
        proStatus: '', // 状态
        timeRange: [], // 日期范围
        startTime: '', // 开始时间
        endTime: '' // 结束时间
      },
      statusOptions: [], // 工程状态
      total: 0, // 总条数
      // 表头
      tableColumns: [
        { label: '退料单位', prop: 'unitName' },
        { label: '退料工程', prop: 'proName' },
        { label: '退料物资类型', prop: 'typeName' },
        { label: '退料人', prop: 'backPerson', width: 90 },
        { label: '退料时间', prop: 'backTime', 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: 'backNum' },
        { label: '出库数量', prop: 'backNum' },
        { 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 getBackTotalListApi(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 getBackDetailsListApi({ ...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: '退料单功能开发中，敬请期待！'
      })
    },
    // 导出数据
    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>
