<template>
  <div class="app-container">
    <!-- 筛选条件 -->
    <div>
      <el-form ref="searchForm" :model="searchForm" label-width="100px" size="mini" label-position="left">
        <el-row :gutter="20">
          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="批次号:" prop="batch">
              <el-input v-model="searchForm.batch" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :lg="6">
            <el-form-item label="外机条码:" prop="barCode">
              <el-input v-model="searchForm.barCode" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="12" :lg="12">
            <el-form-item label="核验日期:" prop="time">
              <el-date-picker
                @change="changeTime"
                v-model="searchForm.time"
                    type="datetimerange"
                    :default-time="['00:00:00','23:59:59']"
                    range-separator="至"
                    style="width: 100%"
                    value-format="yyyy-MM-dd HH:mm:ss"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :xs="24" :sm="24" :lg="24">
            <el-form-item label="" class="fr">
              <el-button size="mini" @click="emptyFn">清空</el-button>
              <el-button size="mini" type="primary" @click="searchFn">搜索</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <!-- 按钮 -->
    <div class="btn-group clearfix">
      <div class="fl">
        <!-- <el-button type="primary" size="mini">导入</el-button> -->
        <el-upload
          style="display: inline-block; margin-right: 10px"
          class="import-btn"
          action=""
          :http-request="handleImport"
          :file-list="importFileList"
          :show-file-list="false"
        >
          <el-button type="primary" size="mini">导入</el-button>
        </el-upload>
        <el-button type="danger" size="mini" @click="downloadFn">下载模板</el-button>
        <el-button type="primary" size="mini" @click="exportFn">导出</el-button>
      </div>
    </div>
    <br />
    <div class="table">
      <el-table
        v-loading="listLoading"
        :data="dataList"
        element-loading-text="Loading"
        border
        fit
        highlight-current-row
        stripe
      >
        <el-table-column
          align="center"
          label="条形码"
          prop="barCode"
          min-width="140"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          label="产品名称"
          prop="productName"
          min-width="220"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column align="center" label="出库采集时间" prop="deliveryTime" min-width="160"></el-table-column>
        <el-table-column align="center" label="设备仓库" prop="warehouse" min-width="100"></el-table-column>
        <el-table-column
          align="center"
          label="收货地址"
          prop="address"
          min-width="220"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column align="center" label="发货日期" prop="consignerTime" min-width="160"></el-table-column>
        <el-table-column
          align="center"
          label="订单号"
          prop="orderNumber"
          min-width="140"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column align="center" label="最新核验状态" prop="verificationStatus" min-width="120">
          <template slot-scope="scope">
            {{ scope.row.verificationStatus | CODE_CHECK_STATUS_FILTER }}
          </template>
        </el-table-column>
        <el-table-column align="center" label="最新核验时间" prop="verificationTime" min-width="160"></el-table-column>
        <el-table-column align="center" label="核验人员" prop="workerNumber" min-width="160" show-overflow-tooltip>
          <template slot-scope="scope"> {{ scope.row.workerName }}（{{ scope.row.workerNumber }}） </template>
        </el-table-column>
        <el-table-column align="center" label="核验人员手机号" prop="workerPhone" min-width="130"></el-table-column>
        <el-table-column
          align="center"
          label="所属网点名称"
          prop="websitName"
          min-width="160"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column
          align="center"
          label="所属网点编号"
          prop="websitNumber"
          min-width="120"
          show-overflow-tooltip
        ></el-table-column>
        <el-table-column align="center" label="上传人" prop="createByNumber" min-width="160" show-overflow-tooltip>
          <template slot-scope="scope"> {{ scope.row.createBy }}（{{ scope.row.createByNumber }}） </template>
        </el-table-column>
        <el-table-column align="center" label="上传时间" prop="createTime" min-width="160"></el-table-column>
        <el-table-column align="center" label="批次" prop="batch" min-width="160"></el-table-column>
      </el-table>
    </div>

    <div class="pagination clearfix">
      <div class="fr">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          :total="listTotal"
        >
        </el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { downloadFiles, handleImport } from '@/utils/util'
import { getList } from '@/api/barcodeVerification'
export default {
  data() {
    return {
      searchForm: {
        barCode: '',
        batch: '',
        startTime: '',
        endTime: '',
        time: []
      },
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数
      dataList: [], // 列表数据
      importFileList: [],
      listLoading: false // 列表加载loading
    }
  },
  created() {
    this.getDataList()
  },
  methods: {
    changeTime(val) {
      if (val) {
        this.searchForm.startTime = val[0]
        this.searchForm.endTime = val[1]
      } else {
        this.searchForm.startTime = ''
        this.searchForm.endTime = ''
      }
    },
    //下载模板
    downloadFn() {
      downloadFiles('externalCode/equipment/downloadBatch')
    },
    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.getDataList()
    },

    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getDataList()
    },
    //清除
    async emptyFn() {
      this.searchForm = {
        barCode: '',
        batch: '',
        startTime: '',
        endTime: '',
        time: []
      }
    },
    //搜索
    searchFn() {
      if (this.searchForm.time.length === 0) {
        this.$message.error('请选择核验日期')
        return
      } else {
        this.currentPage = 1
        this.getDataList()
      }
    },
    //导出
    exportFn() {
      if (this.searchForm.time.length === 0) {
        this.$message.error('请选择导出的时间')
        return
      }
      let params = {
        pageNo: 1,
        pageSize: -1,
        barCode: this.searchForm.barCode,
        batch: this.searchForm.batch,
        endTime: this.searchForm.endTime,
        startTime: this.searchForm.startTime
      }

      downloadFiles('externalCode/equipment/exportBatch', params)
    },
    // 导入
    async handleImport(param) {
      this.listLoading = true
      const file = param.file
      const formData = new FormData()
      formData.append('file', file)
      let result = await handleImport('externalCode/equipment/importBatch', formData)
      this.importFileList = []
      if (result.code == 200) {
        this.$message.success('导入成功')
        this.searchForm.batch = result.data
        this.getDataList()
      } else {
        this.$message.error(result.message)
      }
      this.listLoading = false
    },
    //获取列表数据
    async getDataList() {
      let params = {
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        barCode: this.searchForm.barCode,
        batch: this.searchForm.batch,
        startTime: this.searchForm.startTime,
        endTime: this.searchForm.endTime
      }
      let res = await getList(params)
      this.dataList = res.data.records
      this.listTotal = res.data.total
    }
  }
}
</script>

<style></style>
