<template>
  <div class="app-container">
    <el-card>
      <div>
      <el-input
              v-model="listQuery.search"
              placeholder="仓库名称/仓库编号"
              style="width: 300px"
              class="filter-item"
              @keyup.enter.native="handleFilter"
            />
            <el-button
              class="filter-item"
              type="primary"
              icon="el-icon-search"
              @click="handleFilter"
              >搜索</el-button
            >
            <el-button
              class="filter-item"
              type="primary"
              icon="el-icon-refresh-left"
              @click="resetFilter"
              >重置</el-button
            >
    </div>
    </el-card>
    <el-card style="margin-top: 10px">
        <div style="margin-top: 10px">
       <el-button type="primary" icon="el-icon-plus" @click="handleCreate"
        >新增物料</el-button
      >
    </div>
      <el-table
        v-loading="listLoading"
        :data="fifoList.results"
        border
        fit
        stripe
        highlight-current-row
        max-height="600"
      >
      
     
        <el-table-column type="index" width="50" />
        <el-table-column label="仓库">
          <template slot-scope="scope">{{ scope.row.warehouse_.name }}</template>
        </el-table-column>
       
        <el-table-column label="出库入人员">
          <template slot-scope="scope">{{ scope.row.operator_.username }}</template>
        </el-table-column>
          <el-table-column label="出入库时间">
          <template slot-scope="scope">{{ scope.row.inout_date }}</template>
        </el-table-column>
         <el-table-column label="出入库类型">
          <template slot-scope="scope">{{ types_[scope.row.type] }}</template>
        </el-table-column>
  
        <el-table-column
          align="center"
          label="操作"
          width="220px"
        >
          <template slot-scope="scope">
           
            <el-link
              v-if="checkPermission(['warehouse_update'])"
              @click="handleDetail(scope)"
              >查看详情</el-link
            >
            <el-link
              v-if="checkPermission(['warehouse_delete'])"
              type="danger"
              @click="handleDelete(scope)"
              >删除</el-link
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="fifoList.count > 0"
        :total="fifoList.count"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.page_size"
        @pagination="getList"
      />
    </el-card>
     <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'edit' ? '编辑仓库' : '新增仓库'"
    >
      <el-form
        ref="Form"
        :model="inventory"
        label-width="80px"
        label-position="right"
        :rules="rule1"
      >
        <el-form-item label="仓库" prop="warehouse">
      <el-select v-model="inventory.warehouse" placeholder="请选择" filterable >
      <el-option v-for="item in warehouseData" :key="item.value" :label="item.label" :value="item.value" />
    </el-select>
      </el-form-item>
   <el-form-item label="操作人" prop="operator">
      <el-select v-model="inventory.operator" placeholder="请选择" filterable >
      <el-option v-for="item in userData" :key="item.value" :label="item.label" :value="item.value" />
    </el-select>
      </el-form-item>
     <el-form-item label="入库时间" prop="inout_date">
          <el-date-picker
            v-model="inventory.inout_date"
            type="date"
            placeholder="选择日期"
            value-format="yyyy-MM-dd"
            style="width:100%"
          >
          </el-date-picker>
        </el-form-item>

        <div v-for="(item,index) in inventory.details" :key="index">
                                <el-row>
                                    <el-col :span="8" style="margin-right:10px;">
                                        <el-form-item class="material" label="物料：" :prop="'details.'+index+'.material'"  >
                                            <el-select  v-model="item.material"  size="small" >
                                                <el-option v-for="item in materialoptions" 
                                                :key="item.value" 
                                                :value="item.value" 
                                                :label="item.label">
                                                </el-option>
                                            </el-select>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="8" style="margin-right:10px;">
                                        <el-form-item class="count" label="数量" :prop="'details.'+index+'.count'"  >
                                              <el-input-number v-model="item.count" :min="0"></el-input-number>
                                        </el-form-item>
                                    </el-col>
                                     <el-col :span="6" style="margin-right:10px;">
                                        <el-form-item class="batch" label="批次" :prop="'details.'+index+'.batch'"  >
                                              <el-input v-model="item.batch" placeholder="批次号" ></el-input>
                                        </el-form-item>
                                    </el-col>
                                    <!-- 删除按钮 -->
                                    <el-col :span="1" >
                                        <el-tooltip class="item" effect="dark" content="删除" placement="top">
                                            <el-button @click="deleteItem(index)" style='border:none;background:none;padding:0px;margin-top:4px;'>
                                                <i class="el-icon-remove-outline"  style='font-size:20px;'></i>
                                            </el-button>
                                        </el-tooltip>
                                    </el-col>
                                </el-row>
                            </div>
                            <div>
                                <el-tooltip class="item" effect="dark" content="添加条件" placement="top">
                                    <el-button  @click="addConditions" style='cursor:pointer;width:95%;color:#fe000c;border:1px dashed #fe000c;height:40px;padding:0px;margin-top:10px;margin-left:20px;font-size:26px'>
                                        +
                                    </el-button>
                                </el-tooltip> 
                        </div>
      </el-form>
      <div style="text-align: right">
        <el-button type="danger" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm('Form')">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {getfifoList,createInventory,getWarehouseList} from "@/api/inm";
import checkPermission from "@/utils/permission";
import { getMaterialList} from "@/api/mtm";
 import { getUserList} from "@/api/user";
import { genTree } from "@/utils";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
const defaulteinventory = {
  details:[
                    {
                        material:'',  // 物料
                        count:'',//数量
                        batch:'',//批次
                        
                    }
                ],
};
export default {
  components: { Pagination },
  data() {
    return {
      inventory: defaulteinventory,
     fifoList: {
        count: 0,
      },
     types_:{
        
        1:'生产领料',
        2:'销售提货',
        3:'采购入库',
        4:'生产入库',
      
       
      },
      listQuery: {
        page: 1,
        page_size: 20,
      },
      warehouseData:[],
      materialoptions:[],
      userData:[],
      listLoading: true,
      dialogVisible: false,
      dialogType: "new",
      rule1: {
        name: [{ required: true, message: "请输入", trigger: "blur" }],
        
      },
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getlists()
    this.getList();
    this.getUser();
    this.getmaterialList();
  },
  methods: {
    checkPermission,
    
    getList() {
      this.listLoading = true;
     
        getfifoList(this.listQuery).then((response) => {
        if (response.data) {
          this.fifoList = response.data;
        }
        this.listLoading = false;
      });
      
    },
 //出入库详情
 handleDetail(scope){
       this.$router.push({name: "fifodetail", params: { id: scope.row.id }, })
    },

   getlists(){
       getWarehouseList({page:0}).then((response) => {
        if (response.data) {
          this.warehouseData = genTree(response.data);
        }
        this.listLoading = false;
      });
   },
    getUser() {
        getUserList({page:0}).then(res => {
          if (res.data) {
            this.userData = genTree(res.data);
          }
        })
      },
       addConditions () {
         
            if(this.inventory.details.length<=10) {
                this.inventory.details.push({
                    material:'',  // 物料
                    count:'', //数量
                    batch:'',  // 批次
                   
                });
            } else {
                this.$message('最多可添加十项条件');
            }
        },
        deleteItem (index) {
      this.inventory.details.splice(index, 1)
    },
         //物料
    getmaterialList() {
      this.listLoading = true;
      getMaterialList({pageoff:true}).then((response) => {
        if (response.data) {
          this.materialoptions = genTree(response.data);
        }
        this.listLoading = false;
      });
    },
   handleCreate()
   {
     this.inventory.details=[
                    {
                        material:'',  // 物料
                        count:'',//数量
                        batch:'',//批次
                        
                    }
                ];
     this.inventory = Object.assign({}, defaulteinventory);
      this.dialogType = "new";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
   },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    resetFilter() {
      this.listQuery = {
        page: 1,
        page_size: 20,
      }
      this.getList();
    },
    
   
    async confirm(form) {
      console.log(this.inventory)
      
            createInventory(this.inventory).then((res) => {
              if (res.code >= 200) {
                this.getList();
                 this.dialogVisible = false;
                this.$message.success("成功");
              }
            });
         
    
    },
   
  },
};
</script>
