<template>
  <el-dialog title="选择原料" :visible.sync="open" width="700px" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="原料名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入原料名称" clearable @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-form-item>
    </el-form>
    <el-table v-loading="loading" :data="materialList" ref="table" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="原料名称" align="center" prop="name" />
      <el-table-column label="原料数量" align="center" prop="quantity">
        <template slot-scope="scope">
          <div style="display: flex;align-items: center;">
            <el-input v-model="scope.row.quantity" placeholder="请输入原料数量" />
            <div style="margin-left: 5px">
              <span> {{scope.row.unit}}</span>
            </div>            
          </div>          
        </template>
      </el-table-column>
    </el-table>

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

<script>
import { listMaterial } from "@/api/system/material";

export default {
  name: "Material",
  data() {
    return {
      open: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      materialList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        state: 1,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listMaterial(this.queryParams).then((response) => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    openModal() {
      this.getList()
      this.open = true;
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 提交按钮 */
    submitForm() {
      let subTag=true;
      this.ids.map((item) => {     
        if(item.quantity==="" || item.quantity==undefined){          
          let name=item.name;
          this.$message.error(''+name+"原料名称的原料数量不能为空!");
          subTag=false;
          return;
        }else{                         
          const regular = /^([0-9]*)+(.[0-9]{1,2})?$/;  //正数(包括小数)        
          if (!regular.test(item.quantity)) {
            this.$message.error(''+item.name+"原料名称的原料数量不是大于等于0的数字,最多保留2位小数!")
            subTag=false;
            return;         
          }     
        }
      }); 
      if(subTag){
        this.$emit(
          "callback",
          this.ids.map((item) => {
            item.materialId = item.id;
            item.materialName = item.name;
            return item;
          })
        );
        this.$refs.table.clearSelection();
        this.open = false;
      }
    },
  },
};
</script>