<template>
  <!-- 基础页面 -->
  <div class="app-container">
    <el-form
      v-show="showSearch"
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      label-width="70px"
      @submit.native.prevent
    >
      <el-form-item label="退料单位" prop="unitName">
        <el-input
          v-model="queryParams.unitName"
          placeholder="请输入退料单位"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="退料工程" prop="proName">
        <el-input
          v-model="queryParams.proName"
          placeholder="请输入退料工程"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="物资名称" prop="typeName">
        <el-input
          v-model="queryParams.typeName"
          placeholder="请输入物资名称"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 240px"
        />
      </el-form-item>
      <el-form-item label="退料人" prop="backPerson">
        <el-input
          v-model="queryParams.backPerson"
          placeholder="请输入退料人"
          clearable
          @keyup.enter.native="handleQuery"
          style="width: 240px"
        />
      </el-form-item>
      <!-- 日期范围 -->
      <el-form-item label="退料时间" prop="startTime">
        <el-date-picker
          v-model="queryParams.startTime"
          type="date"
          placeholder="开始日期"
          clearable
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          @change="() => queryParams.endTime = ''"
          style="width: 130px"
        />
      </el-form-item>
      <el-form-item label="">-</el-form-item>
      <el-form-item label="" prop="endTime">
        <el-date-picker
          v-model="queryParams.endTime"
          type="date"
          placeholder="结束日期"
          clearable
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          :picker-options="{ disabledDate: (t) => t.getTime() < new Date(queryParams.startTime).getTime() - 86400000}"
          style="width: 130px"
        />
      </el-form-item>
      <el-form-item label="退料单号" prop="code">
        <el-input
          v-model="queryParams.code"
          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">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增退料预约</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出数据</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table :data="tableList" fit highlight-current-row style="width: 100%" v-loading="isLoading" :max-height="650">
      <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"
        :show-overflow-tooltip="!column.unShowTooltip"
        :key="index"
        :label="column.label"
        :prop="column.prop"
        :width="column.width"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center" width="180">
        <template slot-scope="{ row }">
          <el-button
            v-if="row.status == 0"
            type="text"
            size="mini"
            icon="el-icon-edit"
            @click="handleEdit(row)"
            style="color: #409eff"
          >
            编辑
          </el-button>
          <el-button
            v-if="row.status == 0"
            type="text"
            size="mini"
            icon="el-icon-delete"
            @click="handleDelete(row)"
            style="color: #f56c6c"
          >
            删除
          </el-button>
          <el-button type="text" size="mini" icon="el-icon-zoom-in" @click="handleDetails(row)" style="color: #409eff">
            查看
          </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"
    />
  </div>
</template>

<script>
import { getBackApplyListApi, deleteTask } from '@/api/materialsStation'
import { formatTime } from '@/utils/bonus.js'

export default {
  name: 'BackAppointment',
  data() {
    return {
      isLoading: false,
      showSearch: true,
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        unitName: '', // 退料单位
        proName: '', // 退料工程
        typeName: '', // 物资名称
        backPerson: '', // 退料人
        startTime: '', // 开始时间
        endTime: '', // 结束时间
        code: '',
        timeRange: [] // 创建日期范围
      },
      total: 0, // 总条数
      // 表头
      tableColumns: [
        { label: '退料单位', prop: 'unitName', unShowTooltip: true },
        { label: '退料工程', prop: 'proName', unShowTooltip: true },
        { label: '物资名称', prop: 'typeName' },
        { label: '退料人', prop: 'backPerson', width: 100 },
        { label: '退料时间', prop: 'backTime', width: 150 },
        { label: '退料单号', prop: 'code', width: 150 }
      ],
      // 表格数据
      tableList: [],
      isAdd: true,
      isDetails: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 查询
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.$refs.queryForm.resetFields()
      // const date = new Date()
      // this.queryParams.timeRange = [formatDate(date), formatDate(date)]
      this.queryParams.pageNum = 1
      this.queryParams.pageSize = 10
      this.getList()
    },
    // 获取列表
    async getList() {
      console.log('列表-查询', this.queryParams)
      this.isLoading = true
      try {
        const params = {
          ...this.queryParams,
          // startTime: this.queryParams.timeRange ? this.queryParams.timeRange[0] : '',
          // endTime: this.queryParams.timeRange ? this.queryParams.timeRange[1] : ''
        }
        delete params.timeRange
        console.log('🚀 ~ getList ~ params:', params)
        const res = await getBackApplyListApi(params)
        console.log('🚀 ~ 获取列表 ~ res:', res)
        this.tableList = res.data.rows || []
        this.total = res.data.total || 0
      } catch (error) {
        console.log('🚀 ~ 获取列表 ~ error:', error)
        this.tableList = []
        this.total = 0
      } finally {
        this.isLoading = false
      }
    },
    handleAdd() {
      this.isAdd = true
      this.isDetails = false
      const params = {
        isAdd: true,
        isDetails: false
      }
      this.$router.push({
        path: '/materialsStation/equipment/addAndDetails',
        query: params
      })
    },
    handleEdit(row) {
      this.isAdd = false
      this.isDetails = false
      const params = {
        isAdd: false,
        isDetails: false,
        id: row.id
      }
      this.$router.push({
        path: '/materialsStation/equipment/addAndDetails',
        query: params
      })
    },
    handleDelete(row) {
      // 二次确认
      this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          this.isLoading = true
          const res = await deleteTask({ id: row.id })
          this.$message({
            type: 'success',
            message: '删除成功'
          })
        } catch (error) {
          console.log('🚀 ~ handleDelete ~ error:', error)
        } finally {
          this.isLoading = false
          this.getList()
        }
      })
    },
    handleDetails(row) {
      this.isAdd = false
      this.isDetails = true
      const params = {
        isAdd: false,
        isDetails: true,
        id: row.id
      }
      this.$router.push({
        path: '/materialsStation/equipment/addAndDetails',
        query: params
      })
    },
    // 导出数据
    handleExport() {
      try {
        let fileName = `材料站退料预约_${formatTime(new Date())}.xLsx`
        let url = '/material/material_back_appointment/export'
        const params = { ...this.queryParams }
        console.log('🚀 ~ 导出 ~ params:', params)
        this.download(url, params, fileName)
      } catch (error) {
        console.log('导出数据失败', error)
      }
    }
  }
}
</script>

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