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

    <!-- 操作栏 -->
    <el-row :gutter="10">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['wms:slitting-order-master:create']"
          type="primary"
          size="mini"
          plain
          icon="el-icon-check"
          :loading="ensureLoading"
          @click="submitForm"
        >提交</el-button>
      </el-col>
    </el-row>

    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="88px"
    >
      <QueryFormWrapper>
        <el-col :span="8">
          <el-form-item label="物料编码" prop="materialNo">
            <MaterialSelect
              ref="materialSelect"
              v-model="queryParams.materialNo"
              filterable
              :disabled="selectDisabled"
              placeholder="请选择物料"
              @change="materialChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="单号" prop="workOrderNo">
            <el-input
              v-model="queryParams.workOrderNo"
              placeholder="请输入单号"
              clearable
              :disabled="orderDisabled"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <div slot="right">
          <el-form-item label-width="0" class="text-right" style="word">
            <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </div>
      </QueryFormWrapper>

    </el-form>

    <!-- 列表 -->
    <AutoResizer :given-height="177">
      <template #default="{height}">
        <el-table
          ref="table"
          v-loading="loading"
          :data="list"
          :height="height"
          border
          size="mini"
          max-height="950px"
          :row-key="getOnlyRowKey"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
            :reserve-selection="true"
            :selectable="selected"
            fixed="left"
          />
          <el-table-column
            label="产品编码"
            align="center"
            prop="productCode"
            width="150"
          />
          <el-table-column
            label="产品名称"
            align="center"
            prop="productName"
            width="130"
            show-overflow-tooltip
          />
          <el-table-column
            label="生产订单"
            align="center"
            prop="workOrderNo"
            width="160"
          />
          <el-table-column
            label="用料清单号"
            align="center"
            prop="workOrderBomNo"
            width="100"
            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="130"
            show-overflow-tooltip
          />
          <el-table-column
            label="物料规格"
            align="center"
            prop="materialModels"
            show-overflow-tooltip
          />
          <el-table-column
            label="需求数量"
            align="center"
            prop="needQty"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            label="生产数量"
            align="center"
            prop="productAmount"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            label="宽度(mm)"
            align="center"
            prop="width"
            width="60"
            show-overflow-tooltip
          />
          <el-table-column
            label="步长(mm)"
            align="center"
            prop="stepLength"
            width="60"
            show-overflow-tooltip
          />
          <el-table-column
            label="调机米数"
            align="center"
            prop="testScrapLength"
            width="80"
          />
          <el-table-column
            label="损耗"
            align="center"
            prop="pcsScrap"
            width="80"
          />
          <el-table-column
            label="总长度"
            align="center"
            prop="sumLength"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            label="标准卷数"
            align="center"
            prop="volumeNumber"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            label="穴数"
            align="center"
            prop="hillQty"
            width="80"
          />
          <el-table-column
            label="需求分切数"
            align="center"
            prop="needArea"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            label="已分切量"
            align="center"
            prop="slitArea"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            label="剩余分切量"
            align="center"
            prop="remainSlitArea"
            width="100"
            show-overflow-tooltip
          />
          <el-table-column
            label="单据日期"
            align="center"
            prop="erpOrderTime"
            width="160"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.erpOrderTime) }}</span>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </AutoResizer>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- <div slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button
        v-hasPermi="['wms:slitting-order-master:create']"
        type="primary"
        :loading="ensureLoading"
        @click="submitForm"
      >确定</el-button>
    </div> -->
  </div>
</template>

<script>
import { getDetailAndMainPage, createSlittingOrderMaster } from '@/api/wms/inLibrary/slittingOrder'
import MaterialSelect from './MaterialSelect.vue'

export default {
  name: 'AddInspectionItems',
  components:
  {
    MaterialSelect
  },
  props: {
    inspectionType: {
      type: Number,
      default: 0
    },
    caselist: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      ensureLoading: false,
      selectDisabled: false,
      orderDisabled: false,
      // 是否是第一次加载
      isFirst: true,
      // 总条数
      total: 0,
      // WMS 品质管理-检验项管理列表
      list: [],
      currentRows: [],
      // 选中的
      selectList: [],
      // 弹出层标题
      title: '',
      caseTitle: '',
      // 物料编码，modify时传
      materialNo: '',
      // 是否显示弹出层
      open: false,
      dom: '',
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        materialNo: null,
        workOrderNo: null,
        eBillNo: null
      },
      //
      modifyParams: {},
      // 表单参数
      form: {},
      isLoading: false,
      // 表单校验
      rules: {
      }
    }
  },
  watch: {
    currentRows(newVal, oldVal) {
      if (newVal.length > 0) {
        this.$refs.materialSelect.currentRows = newVal
        this.$refs.materialSelect && this.$refs.materialSelect.fetchSameMaterialList()
      } else {
        this.$refs.materialSelect && this.$refs.materialSelect.fetchslitMaterialList()
      }
    }
  },
  created() {
    if (this.queryParams.materialNo === undefined || this.queryParams.materialNo === null) {
      this.orderDisabled = true
    } else {
      this.orderDisabled = false
    }
    this.$nextTick(() => {
      this.$refs.materialSelect.fetchslitMaterialList()
    })
    this.getList()
    // this.scroll()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getDetailAndMainPage(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.$refs.table.clearSelection()
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
      if (this.queryParams.materialNo === null) {
        this.orderDisabled = true
      } else {
        this.orderDisabled = false
      }
    },
    // dialogOpen() {
    //   this.$nextTick(() => {
    //     if (this.queryParams.materialNo === undefined || this.queryParams.materialNo === null) {
    //       this.orderDisabled = true
    //     } else {
    //       this.orderDisabled = false
    //     }
    //     this.getList()
    //     this.scroll()
    //   })
    // },
    // 选中的
    handleSelectionChange(val) {
      this.currentRows = val
    },
    async submitForm() {
      if (this.currentRows.length <= 0) {
        this.$modal.msgWarning('分切物料不能为空!')
        return
      }
      const submitList = this.currentRows.map(item => {
        const { wmsProductionWorkOrderBomDetailId } = item
        return {
          wmsProductionWorkOrderBomDetailId
        }
      })
      const params = {
        list: submitList,
        slittingOrderNo: this.queryParams.eBillNo
      }
      createSlittingOrderMaster(params).then(() => {
        this.$modal.msgSuccess('新增成功！')
        this.open = false
        // 关闭当前窗口
        this.$tab.closeOpenPage()
        this.$router.go(-1)
        this.$emit('MultipleComplete')
        this.currentRows = []
      })
    },
    scroll() {
      // 获取需要绑定的table
      this.$nextTick(() => {
        this.dom = this.$refs.table.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.queryParams.materialNo = undefined
      this.getList()
      this.$refs.table.clearSelection()
    },
    selected(row) {
      const { remainSlitArea } = row
      if (remainSlitArea <= 0) {
        return false
      } else {
        return true
      }
    },
    materialChange(v) {
      if (v) {
        this.orderDisabled = false
      }
    },
    getOnlyRowKey(row) {
      return row.id
    }
  }
}
</script>

