<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 ref="multipleTable" v-loading="loading" :data="materialList" @selection-change="handleSelectionChange" :row-key="getRowKey">
      <el-table-column type="selection" width="55" align="center" :reserve-selection="true"/>
      <el-table-column label="原料名称" align="center" prop="name" />
      <el-table-column label="原料数量" align="center">
        <template slot-scope="scope">
          <div style="display: flex;align-items: center;">
            <el-input v-model="scope.row.quantity" placeholder="请输入原料数量" @change="((val)=>{quanChange(val, scope.row)})"/>
            <div style="margin-left: 5px">
              <span> {{scope.row.unit}}</span>
            </div>            
          </div>          
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryParams.pageNum"
      :page-sizes="[10, 20, 30, 40,50]"
      :page-size="queryParams.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <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: {
    handleCurrentChange(val){
      this.queryParams.pageNum=val;
      this.queryParams.pageSize=10;  
      listMaterial(this.queryParams).then((response) => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
        if(this.ids.length>0){
          for(let i=0;i<this.ids.length;i++){
            for(let j=0;j<this.materialList.length;j++){
              if(this.ids[i].quantity!=""){
                if(this.ids[i].id==this.materialList[j].id){
                  this.materialList[j].quantity=this.ids[i].quantity
                }
              }
            }
          }
        }
      });
    },
    handleSizeChange(val){
      this.queryParams.pageSize=val;
      this.queryParams.pageNum=1; 
      listMaterial(this.queryParams).then((response) => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
        if(this.ids.length>0){
          for(let i=0;i<this.ids.length;i++){
            for(let j=0;j<this.materialList.length;j++){
              if(this.ids[i].quantity!=""){
                if(this.ids[i].id==this.materialList[j].id){
                  this.materialList[j].quantity=this.ids[i].quantity
                }
              }
            }
          }
        }
      });
    },
    getRowKey(row){
      return row.id
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listMaterial(this.queryParams).then((response) => {
        this.materialList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    openModal() {
      //this.getList();
      this.handleQuery();
      this.open = true;
    },
    // 取消按钮
    cancel() {
      this.$refs.multipleTable.clearSelection();
      this.open = false;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      listMaterial(this.queryParams).then((response) => {
      for(let j=0;j<response.rows.length;j++){
        let row = response.rows[j]; 
        row.quantity="";
        if(this.ids.length>0){ 
          for(let i=0;i<this.ids.length;i++){
              if(this.ids[i].quantity!=""){
                if(this.ids[i].id==row.id){
                  row.quantity=this.ids[i].quantity
                }
              }
             
          }
        }
      }
      this.materialList = response.rows;
      this.total = response.total;
      this.loading = false;
        
        
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {       
      this.ids = selection;
    },
    quanChange(val,row){
      for(let i=0;i<this.ids.length;i++){
        if(row.id==this.ids[i].id){
          row.quantity = val;
          this.ids[i].quantity = val
        }
      }
    },
    /** 提交按钮 */
    submitForm() {
      let subTag=true;
      this.ids.map((item) => {     
        if(item.quantity==="" || item.quantity==undefined){

          //selection 没有变化时，this.ids值还是上一次选择变化的值，将新的变化的值重新赋值
          for(let i=0;i<this.materialList.length;i++){
            if(item.id==this.materialList[i].id){
              item.quantity = this.materialList[i].quantity;
            }
          }

          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.multipleTable.clearSelection();
        this.open = false;
      }
    },
  },
};
</script>
