<template>
  <el-dialog
    v-dialogDrag
    title="客户订单信息"
    :visible.sync="open"
    width="1200px"
    append-to-body
    @open="dialogOpen"
    @close="dialogClose"
  >

    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="88px"
    >
      <SearchBar :show-more="false" :collapse-span="6">
        <template #header>
          <el-col :span="6">
            <el-form-item label="销售订单" prop="erpOrderNo">
              <el-input
                v-model="queryParams.erpOrderNo"
                placeholder="请输入销售订单"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="物料编码" prop="materialNo">
              <el-input
                v-model="queryParams.materialNo"
                placeholder="请输入物料编码"
                clearable
                :disabled="materialNoDisable"
                @keyup.enter.native="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="物料名称" prop="materialName">
              <el-input
                v-model="queryParams.materialName"
                placeholder="请输入物料名称"
                clearable
                @keyup.enter.native="handleQuery"
              />
            </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-table
      ref="multipleTable"
      v-loading="loading"
      :data="list"
      border
      max-height="400px"
      :row-key="getOnlyRowKey"
      @row-dblclick="submitForm"
    >
      <!-- <el-table-column
        type="selection"
        width="55"
        :reserve-selection="true"
      /> -->
      <el-table-column
        label="销售订单"
        align="center"
        prop="erpOrderNo"
        width="150"
        fixed="left"
        show-overflow-tooltip
      />
      <el-table-column
        label="客户名称"
        align="center"
        prop="customerName"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="物料编码"
        align="center"
        prop="materialNo"
        width="130"
        show-overflow-tooltip
      />
      <el-table-column
        label="物料名称"
        align="center"
        prop="materialName"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column
        label="规格"
        align="center"
        prop="materialModels"
        width="120"
        show-overflow-tooltip
      />
      <el-table-column label="单位" align="center" prop="unitName" />
      <el-table-column
        label="已出货数量"
        align="center"
        prop="deliveredQty"
        width="120"
      />
      <el-table-column
        label="应出货数量"
        align="center"
        prop="shouldDeliveredQty"
        width="120"
      />
      <el-table-column label="交货地点" align="center" prop="deliveryAddress" />
      <el-table-column
        label="交货时间"
        align="center"
        prop="deliveryTime"
        width="120"
      />
    </el-table>
    <!-- <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button
        type="primary"
        :loading="ensureLoading"
        @click="submitForm"
      >确定</el-button>
    </div> -->
  </el-dialog>
</template>

<script>
import { getSaleOrderPage } from '@/api/wms/orders/sale'

export default {
  components:
  {
  },
  props: {
    state: {
      type: [Number],
      default: 0
    },
    rowDetails: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 导出遮罩层
      exportLoading: false,
      orderDisabled: false,
      // 显示搜索条件
      showSearch: true,
      ensureLoading: false,
      // 总条数
      total: 0,
      // 搜索条件-生产订单禁用
      materialNoDisable: false,
      // WMS 品质管理-检验项管理列表
      list: [],
      currentRows: [],
      // 选中的
      selectList: [],
      // 弹出层标题
      title: '',
      caseTitle: '',
      // 是否显示弹出层
      open: false,
      // 明细新增
      detailAdd: '',
      dom: '',
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 500,
        materialNo: null,
        workOrderNo: null,
        materialName: null,
        masterId: null,
        voucherTimeBegin: null,
        voucherTimeEnd: null,
        erpWorkOrderNo: null,
        customerCode: null
      },
      //
      modifyParams: {},
      // 表单参数
      form: {},
      isLoading: false,
      // 表单校验
      rules: {
      }
    }
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getSaleOrderPage(this.queryParams).then(response => {
        this.list = response.data.list
        this.total = response.data.total
      }).finally(() => {
        this.loading = false
      })
    },
    /** 取消按钮 */
    cancel() {
      this.open = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        inspectionNo: undefined,
        inspectionName: undefined,
        inspectionModel: undefined,
        inspectionType: undefined,
        minValue: undefined,
        standValue: undefined,
        maxValue: undefined,
        code: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    dialogOpen() {
      Object.assign(this.queryParams, { materialNo: this.rowDetails.materialNo })
      this.$nextTick(() => {
        this.getList()
        this.scroll()
      })
    },
    scroll() {
      // 获取需要绑定的table
      this.dom = this.$refs.multipleTable.bodyWrapper
      this.dom.addEventListener('scroll', () => {
      // 表格滚动条滚动的距离
        const scrollTop = this.dom.scrollTop
        // 变量windowHeight是可视区的⾼度
        // let windowHeight = this.dom.clientHeight
        // 变量scrollHeight是滚动条的总⾼度
        const scrollHeight = this.dom.scrollHeight
        const clientHeight = this.dom.clientHeight
        const scrollBottom = scrollHeight - scrollTop - clientHeight
        if (scrollBottom < 1 && this.list.length < this.total) { // 这里就是滚动条滚动到底部的时候
          this.queryParams.pageSize += 10
          this.getList()
          if (this.list.length + 1 === this.total) {
            return
          }
          setTimeout(() => {
            this.loading = false
          }, 500)
        }
      })
    },
    dialogClose() {
      const { queryParams } = this
      queryParams.pageNo = 1
      queryParams.pageSize = 10
      this.selectList = []
      queryParams.idList = []
      queryParams.idListDeleted = []
      this.getList()
      this.$refs.multipleTable.clearSelection()
    },
    getOnlyRowKey(row) {
      return row.id
    },
    qtyChange(v) {
      if (typeof v === 'string') {
        v = Number(v)
      }
    },
    // 选中的
    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
      }
    },
    // 确定
    submitForm(row, column) {
      this.$modal.msgSuccess('选择成功！')
      this.$emit('send-data', { ...row, state: this.state })
      this.open = false
      // if (this.currentRows.length <= 0) {
      //   this.$modal.msgWarning('订单不能为空!')
      //   return
      // }
      // this.$modal.msgSuccess('操作成功!')
      // this.open = false
      // this.$emit('send-data', this.currentRows)
    }
  }
}
</script>

