<template>
  <el-dialog title="物料选择"
    v-if="showFlag"
    :visible.sync="showFlag"
    :modal= false
    width="1000px"
  >
    <el-row :gutter="20">

      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px" align="left">
          <el-form-item label="物料编码">
            <el-input
              v-model="queryParams.component"
              placeholder="物料编码"
              clearable
              style="width: 200px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="物料名称">
            <el-input
              v-model="queryParams.componentName"
              placeholder="物料名称"
              clearable
              style="width: 200px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="物料组名称">
            <el-input
              v-model="queryParams.productGroupName"
              placeholder="物料组名称"
              clearable
              style="width: 200px"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <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>
        </el-form>
        <el-table v-loading="loading" :data="itemList" @selection-change="handleBomSelectionChange" ref="myTable" >
          <el-table-column width="50" align="center" type="selection">
          </el-table-column>
          <!-- 序号 -->
          <el-table-column label="物料编码" width="100" align="left" prop="component" :show-overflow-tooltip="true" />
          <el-table-column label="物料名称" width="200" align="left" prop="componentName" :show-overflow-tooltip="true" />
          <el-table-column label="物料组" width="80" align="left" prop="productGroup" :show-overflow-tooltip="true" /> 
          <el-table-column label="物料组名称" width="100" align="left" prop="productGroupName" :show-overflow-tooltip="true" /> 
          <el-table-column label="损耗数量" align="center" prop="quantity">
            <template slot-scope="scope">
                <el-input type="number" size="small" v-model="scope.row.quantity" @change="handleEdit(scope.$index,scope.row)"></el-input>
            </template>
          </el-table-column>
          <el-table-column label="单位" align="center" prop="unit">
            <template slot-scope="scope">
                <el-select v-model="scope.row.unit" placeholder="请选择单位" width="50%" @change="handleEdit(scope.$index,scope.row)">
                  <el-option
                    v-for="dict in dict.type.unit"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  ></el-option>
                </el-select>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>
    <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitBomForm">确 定</el-button>
              <el-button @click="showFlag=false">取 消</el-button>
       </div>
  </el-dialog>
</template>
<script>

import { getMesListBom } from "@/api/mes/reportWork";
export default {
  name: "itemSelectMaterial",
  dicts: ["unit"],
  data() {
    return {
      showFlag:false,
      // 选中数组
      selectedRows: {},
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // BOM产品表格数据
      itemList: null,

      //树名称
      bomCode: undefined,
      defaultProps: {
        id: "id",
        label: "label"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productCode: '',
        workorderCode:'',
        component : '',
        componentName : '',
        productGroupName:''
      },
      selectionRows:[]
    };
  },
  created() {
    //this.getList();
  },

  methods: {
    init(productCode,workorderCode){
      this.queryParams.productCode = productCode;
      this.queryParams.workorderCode = workorderCode;
      this.getList();
    },
    /** 查询表格列表*/
    getList() {
      this.loading = true;
      getMesListBom(this.queryParams).then(response => {
          this.itemList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    
    // 多选框选中数据
		handleBomSelectionChange(selection) {
		  this.selectionRows = selection
		},
    handleEdit(index, row) {
     console.log('row:',index, row);
    },
    submitBomForm() {
        if ( this.selectionRows==0) {
          this.$modal.msgError(`请选择数据`);
          return false;
        }

      this.$emit('onSelected', this.$refs.myTable.selection);
      this.showFlag = false;
    },
  }
};
</script>
