<template>
  <div class="app-container">
    <!-- 操作按钮 -->
    <el-row class="mb10" :gutter="10">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['wms:material-summary:create']"
          type="primary"
          size="mini"
          plain
          :loading="saveLoading"
          icon="el-icon-check"
          @click="handleSave"
        >保存</el-button>
      </el-col>
    </el-row>

    <!-- 搜索工作栏 -->
    <el-form
      v-show="showSearch"
      ref="queryForm"
      :model="queryParams"
      size="small"
      label-width="68px"
    >
      <QueryFormWrapper>
        <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
              @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>
        <el-col :span="6">
          <el-form-item label="备注" prop="remark">
            <el-input
              v-model="queryParams.remark"
              placeholder="请输入备注"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </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
          v-loading="loading"
          :data="list"
          :height="height"
          border
          :row-key="getOnlyRowKey"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="55"
            :reserve-selection="true"
            fixed="left"
          />
          <el-table-column
            label="生产订单"
            align="center"
            prop="erpOrderNo"
            width="150"
            fixed="left"
            show-overflow-tooltip
          />
          <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="materialAmount"
            width="100"
          />
          <el-table-column
            label="规格"
            align="center"
            prop="materialModels"
            width="80"
          />
          <el-table-column label="单位" align="center" prop="unitName" />
          <el-table-column
            label="生产车间"
            align="center"
            prop="workShopName"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            label="计划开工日期"
            align="center"
            prop="planStartTime"
            width="160"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.planStartTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="计划完工日期"
            align="center"
            prop="planCompleteTime"
            width="160"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.planCompleteTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="工艺版本"
            align="center"
            prop="mesRoutingVersion"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            label="bom版本"
            align="center"
            prop="mesBomVersion"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            label="表头备注"
            width="120"
            show-overflow-tooltip
            align="center"
            prop="fdescription"
          />
          <el-table-column
            label="备注"
            align="center"
            prop="remark"
            width="120"
            show-overflow-tooltip
          />
          <el-table-column
            label="创建时间"
            align="center"
            prop="createTime"
            width="180"
          >
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</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>
</template>

<script>
import { getWorkOrderPageList, createByWorkOrder } from '@/api/wms/inLibrary/materialSummary'

export default {
  name: 'MaterialSummaryAdd',
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 保存遮罩层
      saveLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 材料汇总列表
      list: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 选中的数据
      currentRows: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        summaryOrderNo: null,
        remark: null,
        createTime: [],
        erpOrderNo: null,
        materialNo: null,
        materialName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true
      // 执行查询
      getWorkOrderPageList(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
    },
    // 创建材料汇总单
    async handleSave() {
      try {
        const detailErpIds = this.currentRows.map(item => {
          const { detailErpId } = item
          return detailErpId
        })
        const { remark } = this.queryParams
        const params = {
          detailErpIds,
          remark
        }
        if (this.currentRows.length === 0) {
          this.$modal.msgWarning('请选择您要汇总材料的数据!')
          return
        }
        this.saveLoading = true
        const data = await createByWorkOrder(params)
        this.$router.replace({
          path: '/inLibrary/material-summary-detail',
          query: { summaryOrderNo: data.data }
        })
      } catch (error) {
        console.log(error)
      } finally {
        this.saveLoading = false
      }
    }
  }
}
</script>
