<template>
  <div class="app-container">
    <el-card>
      <div>
           <el-button type="primary" icon="el-icon-plus" @click="handleCreate"
          >新增出入库记录</el-button
        >
        <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: 2px">
     
      <el-table
        v-loading="listLoading"
        :data="fifoList.results"
        border
        fit
        stripe
        highlight-current-row
        max-height="700"
         height="100"
        v-el-height-adaptive-table="{bottomOffset: 50}"
      >
        <el-table-column type="index" width="50" />
        <el-table-column label="出入记录ID">
          <template slot-scope="scope">{{
            scope.row.id
          }}</template>
        </el-table-column>

        <el-table-column label="操作人员">
          <template slot-scope="scope" v-if="scope.row.create_by">{{
            scope.row.create_by_.name
          }}</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 label="是否审核">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.is_audited == false">未审核</el-tag>
            <el-tag v-else>已审核</el-tag>
          </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="scope.row.is_audited == false"
              @click="handleAudit(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"
      :close-on-click-modal="false"
      :title="dialogType === 'edit' ? '编辑出/如库记录' : '新增出/如库记录'"
    >
      <el-form
        ref="Form"
        :model="inventory"
        label-width="80px"
        label-position="right"
        :rules="rule1"
      >
        
        <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="6" style="margin-right: 10px">
              <el-form-item
                class="warehouse"
                label="仓库："
                :prop="'details.' + index + '.warehouse'"
              >
                <el-select v-model="item.warehouse" size="small">
                  <el-option
                    v-for="item in warehouseData"
                    :key="item.value"
                    :value="item.value"
                    :label="item.label"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            
            <el-col :span="6" style="margin-right: 10px">
              <el-form-item
                class="material"
                label="物料："
                :prop="'details.' + index + '.material'"
              >
                <el-select v-model="item.material" filterable 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="6" 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: 2px;
                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,
  deleteFifoitem,
  audit,
} 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: [
    {
      warehouse:"",//仓库
      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: [],
     
      listLoading: true,
      dialogVisible: false,
      dialogType: "new",
      rule1: {
        name: [{ required: true, message: "请输入", trigger: "blur" }],
      },
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getlists();
    this.getList();
  
    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;
      });
    },
   
    addConditions() {
      if (this.inventory.details.length <= 10) {
        this.inventory.details.push({
          warehouse:"",//仓库
          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 = [
        {
          warehouse:"", // 仓库
          material: "", // 物料
          count: "", //数量
          batch: "", //批次
        },
      ];
      this.inventory = Object.assign({}, defaulteinventory);
      this.dialogType = "new";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
    //审核
    handleAudit(scope) {
      this.$confirm("是否通过?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: 'success',
      })
        .then(async () => {
          await audit(scope.row.id);
          this.getList();
          this.$message.success("已审核");
        })
        .catch((err) => {
          console.error(err);
        });
    },
    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("成功");
        }
      });
    },
     handleDelete(scope) {
      this.$confirm("确认删除?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(async () => {
          await deleteFifoitem(scope.row.id);
          this.getList();
          this.$message.success("成功");
        })
        .catch((err) => {
          console.error(err);
        });
    },
   
  },
};
</script>
