<template>
  <div class="app-container">
    <!-- 操作按钮 -->
    <el-row class="mb10" :gutter="10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          size="mini"
          plain
          :loading="exchangeLoading"
          @click="handleToExchangeSlitting"
        >明细转分切任务</el-button>
      </el-col>
    </el-row>

    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <QueryFormWrapper>
        <el-col :span="8">
          <el-form-item label="汇总单号" prop="summaryOrderNo">
            <el-input
              v-model="queryParams.summaryOrderNo"
              placeholder="请输入汇总单号"
              clearable
              disabled
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="queryParams.remark"
              placeholder="请输入备注"
              clearable
              disabled
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="物料编码" prop="materialNo">
            <MaterialSelect
              ref="materialSelect"
              v-model="queryParams.materialNo"
              filterable
              placeholder="请选择物料"
            />
          </el-form-item>
        </el-col>
        <div slot="right">
          <el-form-item class="text-right">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >查询</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </div>
      </QueryFormWrapper>
    </el-form>

    <!-- 列表 -->
    <AutoResizer :given-height="188">
      <template #default="{height}">
        <el-table
          ref="multipleTable"
          v-loading="loading"
          :data="list"
          :height="height"
          border
          :row-key="getOnlyRowKey"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
            :reserve-selection="true"
            :selectable="selectedTable"
            fixed="left"
          />
          <el-table-column
            label="生产订单"
            align="center"
            prop="workOrderNo"
            width="150"
            fixed="left"
            show-overflow-tooltip
          />
          <el-table-column
            label="用料清单号"
            align="center"
            prop="workOrderBomNo"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            label="产品编码"
            align="center"
            prop="productCode"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            label="产品名称"
            align="center"
            prop="productName"
            width="140"
            show-overflow-tooltip
          />
          <el-table-column
            label="计划生产数量"
            align="center"
            prop="productAmount"
            width="100"
          />
          <el-table-column
            label="物料编码"
            align="center"
            prop="materialNo"
            width="150"
            show-overflow-tooltip
          />
          <el-table-column
            label="物料名称"
            align="center"
            prop="materialName"
            width="130"
            show-overflow-tooltip
          />
          <el-table-column
            label="规格"
            align="center"
            prop="materialModels"
            width="120"
          />
          <el-table-column
            label="需求数量"
            align="center"
            prop="needQty"
            width="80"
            show-overflow-tooltip
          />
          <el-table-column
            label="宽度(mm)"
            align="center"
            prop="width"
            width="100"
          />
          <el-table-column
            label="长度(mm)"
            align="center"
            prop="stepLength"
            width="100"
          />
          <el-table-column
            label="总长度(mm)"
            align="center"
            prop="sumLength"
            width="100"
          />
          <el-table-column
            label="穴数"
            align="center"
            prop="hillQty"
            width="80"
          />
          <el-table-column
            label="调机米数(m)"
            align="center"
            prop="testScrapLength"
            width="100"
          />
          <el-table-column
            label="损耗(%)"
            align="center"
            prop="pcsScrap"
            width="80"
          />
          <el-table-column
            label="标准卷数"
            align="center"
            prop="volumeNumber"
            width="100"
          />
          <el-table-column
            label="需求分切量(㎡)"
            align="center"
            prop="needArea"
            width="100"
          />
          <el-table-column
            label="已分切量"
            align="center"
            prop="slitArea"
            width="80"
          />
          <el-table-column
            label="剩余分切量"
            align="center"
            prop="remainSlitArea"
            width="100"
          />
        </el-table>
      </template>
    </AutoResizer>

    <!-- 分页组件 -->
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

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

export default {
  name: 'MaterialSummaryDetail',
  components: {
    MaterialSelect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 明细转分切任务遮罩层
      exchangeLoading: false,
      // 总条数
      total: 0,
      // 材料汇总列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 选中的数据
      currentRows: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        summaryOrderNo: null,
        remark: null,
        createTime: []
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.queryParams.summaryOrderNo = this.$route.query.summaryOrderNo
    this.queryParams.remark = this.$route.query.remark
    this.$nextTick(() => {
      this.$refs.materialSelect.summaryOrderNo = this.$route.query.summaryOrderNo
      this.$refs.materialSelect.fetchslitMaterialList()
    })
    this.getList()
  },
  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,
        summaryOrderNo: undefined,
        remark: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 选中的
    handleSelectionChange(val) {
      this.currentRows = val
    },
    getOnlyRowKey(row) {
      return row.id
    },
    // 明细转分切任务单
    handleToExchangeSlitting() {
      if (this.currentRows.length === 0) {
        this.$modal.msgWarning('请先选择您要转分切任务单的数据!')
        return
      }
      this.exchangeLoading = true
      const list = this.currentRows.map(item => {
        const { wmsProductionWorkOrderBomDetailId } = item
        return { wmsProductionWorkOrderBomDetailId }
      })
      const params = {
        list
      }
      createSlittingOrderMaster(params).then(() => {
        this.$modal.msgSuccess('操作成功！')
        this.open = false
        this.getList()
        this.$refs.multipleTable.clearSelection()
        this.currentRows = []
        this.$router.push({ path: '/inLibrary/slitting-order-master' })
      }).finally(() => {
        this.exchangeLoading = false
      })
    },
    // 剩余分切数量为0禁选
    selectedTable(row) {
      const { validFlag } = row
      if (validFlag <= 0) {
        return false
      } else {
        return true
      }
    }
  }
}
</script>
