<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <searchBar :show-more="false" :collapse-span="12">
        <template #header>
          <el-col :span="6">
            <el-form-item label="分切单号" prop="ebillNo">
              <el-input
                v-model="queryParams.ebillNo"
                placeholder="请输入分切单号"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="过账日期" prop="createTime">
              <el-date-picker
                v-model="queryParams.createTime"
                value-format="yyyy-MM-dd HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="['00:00:00', '23:59:59']"
              />
            </el-form-item>
          </el-col>
        </template>

        <template #buttons>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </template>
      </searchbar>
    </el-form>

    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <el-row :gutter="10">
        <el-col :span="8">
          <el-form-item label="分切单号" prop="ebillNo">
            <el-input
              ref="barcode"
              v-model="scanForm.ebillNo"
              disabled
              placeholder="请输/扫入分切单号"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="标签码" prop="qrCode">
            <el-input
              ref="barcode"
              v-model="scanForm.qrCode"
              placeholder="请输/扫入标签码"
              clearable
              @keyup.enter.native="handleFeeding"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8" class="text-right">
          <el-button
            type="primary"
            plain
            size="small"
            icon="el-icon-check"
            @click="handleFeeding"
          >
            分切上料
          </el-button>
          <el-button
            type="primary"
            plain
            size="small"
            icon="el-icon-thumb"
            @click="handleExecutions"
          >
            分切执行
          </el-button>
        </el-col>
      </el-row>
    </el-form>

    <!-- 列表 -->
    <el-table
      ref="multipleTable"
      v-loading="loading"
      :data="list"
      border
      show-summary
      :summary-method="getSummaries"
      @selection-change="handleSelectionChange"
      @select="select"
      @select-all="selectAll"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        label="物料编码"
        align="center"
        prop="materialNo"
        fixed="left"
        width="130"
        show-overflow-tooltip
      />
      <el-table-column
        label="物料名称"
        align="center"
        prop="materialName"
        width="130"
        show-overflow-tooltip
      />
      <el-table-column
        label="批次号"
        align="center"
        prop="batchNumber"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="物料规格"
        align="center"
        prop="size"
        width="100"
        show-overflow-tooltip
      />
      <el-table-column label="单位" align="center" prop="unitCode" />
      <el-table-column label="长度(m)" align="center" prop="length" />
      <el-table-column
        label="宽度(mm)"
        align="center"
        prop="width"
        width="120"
      />
      <el-table-column label="批次宽度" align="center" prop="widthLots" />
      <el-table-column
        label="厚度(mm)"
        align="center"
        prop="thickness"
        width="120"
      />
      <el-table-column label="批次宽度" align="center" prop="widthLots" />
      <el-table-column label="可用状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.WMS_SLITTING_MATERIAL_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="分切数量" align="center" prop="qty" />
      <el-table-column label="是否上料" align="center" prop="loadStatus">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.WMS_LOAD_STATUS" :value="scope.row.loadStatus" />
        </template>
      </el-table-column>
      <el-table-column
        label="物料批次"
        align="center"
        prop="materialLots"
        width="130"
      />
      <el-table-column
        label="生产日期"
        align="center"
        prop="produceDate"
        width="160"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.produceDate) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <!-- <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->

  </div>
</template>

<script>
import { getSlittedLotsPage, loadSlitting, executeSlit } from '@/api/wms/inLibrary/slittingOrder'

export default {
  name: 'SlittingExecution',
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工厂管理列表
      list: [],
      currentRows: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        eBillNo: null,
        createTime: [],
        creator: null,
        ebillNo: null
      },
      scanForm: {

      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.queryParams.eBillNo = this.$route.query.ebillNo
    this.scanForm.ebillNo = this.$route.query.ebillNo
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getSlittedLotsPage(this.queryParams).then(response => {
        this.list = response.data
      }).finally(() => {
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        factoryCode: undefined,
        factoryName: undefined,
        tag: undefined,
        address: undefined,
        detailed: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.queryParams.createTime = []
      this.queryParams.ebillNo = null
      this.handleQuery()
    },
    // 分切上料
    handleFeeding() {
      if (this.scanForm.qrCode === undefined || this.scanForm.ebillNo === undefined) {
        this.$modal.msgWarning('请先输入/扫入分切单号/标签码！')
        return
      }
      const { ebillNo, qrCode } = this.scanForm
      const params = {
        ebillNo,
        qrCode
      }
      loadSlitting(params).then(res => {
        this.$modal.msgSuccess('上料成功!')
        this.getList()
        this.scanForm.qrCode = undefined
      }).finally(() => {
        this.scanForm.qrCode = undefined
      })
    },
    // 选中的
    handleSelectionChange(val) {
      this.currentRows = val
    },
    select(selection, row) {
      if (selection.length > 1) {
        const del_row = selection.shift()
        this.$refs.multipleTable.toggleRowSelection(del_row, false)
      }
    },
    selectAll(selection) {
      if (selection.length > 1) {
        selection.length = 1
      }
    },
    // 分切执行
    handleExecutions() {
      const params = {
        slittingOrderNo: this.queryParams.eBillNo,
        id: this.currentRows[0] ? this.currentRows[0].id : null
      }
      executeSlit(params).then(res => {
        this.$modal.msgSuccess('执行成功!')
        this.getList()
      })
    },
    // 小数问题处理
    getHandleNumber(val) {
      return Math.round((parseFloat(val) * 10000000)) / 10000000
    },
    // 总计
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '总计'
          return
        }

        const values = data.map(item => Number(item[column.property]))
        if (column.property === 'width') {
          sums[index] = values.reduce((acc, cur) => {
            const value = Number(cur)
            if (!isNaN(value)) {
              return this.getHandleNumber(acc + cur)
            } else {
              return acc
            }
          }, 0)
          sums[index]
        }
      })
      return sums
    }
  }
}
</script>
