<template>
<el-dialog :visible.sync="showDialog" :title="title" :modal-append-to-body="false" style="text-align: left" width="80%" @close="closeDialog" :close-on-click-modal="false">
  <div>
    <el-form ref="queryForm" :model="queryModel" inline class="demo-form-inline">
      <el-form-item label="名称" prop="ingredientName">
        <el-input type="text" size="mini" v-model="queryModel.ingredientName"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" size="mini" icon="ios-search" @click="changePage(1)" :loading="loading">查询</el-button>&nbsp;
        <el-button type="info" size="mini" style="margin-left: 8px" @click="handleReset('queryForm')">重置</el-button>&nbsp;
      </el-form-item>
    </el-form>
    <el-divider></el-divider>
    <el-table :data="tableData" style="min-height: 400px" v-loading="loading" stripe>
      <el-table-column prop="ingredientName" label="名称" width="180"></el-table-column>
      <el-table-column prop="unitName" label="单位" width="180"></el-table-column>
      <el-table-column prop="minStock" label="最小库存" width="180"></el-table-column>
      <el-table-column prop="nowStock" label="当前库存" width="180"></el-table-column>
      <el-table-column prop="maxStock" label="最大库存" width="180"></el-table-column>
      <el-table-column label="是否生效" width="150">
        <template slot-scope="{row}">
          <img v-if="row.delFlag == false" src="../../assets/tick.png" height="16" width="16"/>
          <img v-else src="../../assets/cross.png" height="16" width="16"/>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button size="mini" type="warning" @click="handleEdit(row)">配置</el-button>
          <el-button v-if="row.delFlag == 0" size="mini" type="danger" @click="changeState(row,'失效')">失效</el-button>
          <el-button v-else size="mini" type="success" @click="changeState(row,'生效')">生效</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :current-page.sync="pageIndex"
      :total="totalElements"
      :page-sizes="pageSizeList"
      @current-change="changePage"
      @size-change="pageSizeChange"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
    <inventory-detail
      v-if="showStock"
      :stockKey="stockKey"
      :title="modalTitle"
      @close="onStockClose"
    ></inventory-detail>
  </div>
</el-dialog>
</template>
<script>
import Constant from "@/constant";
import InventoryDetail from "./inventory-detail";
import inventoryApi from "@/api/base/inventory";
import NProgress from "nprogress"; // progress bar
import "nprogress/nprogress.css"; // progress bar style

export default {
  props: ["warehouseKey", "title"],
  data() {
    var self = this;

    return {
      queryModel: {
        ingredientName: "",
      },
      loading: false,
      tableData: [],
      pageIndex: 1,
      pageSize: 10,
      totalPages: 0,
      totalElements: 0,
      field: "",
      direction: "",
      pageSizeList: [10, 20, 30],
      multipleSelection: [],
      showStock: false,
      modalTitle: "",
      stockKey: "",
      showDialog: true,
    };
  },
  methods: {
    closeDialog() {
      this.$emit("close", false);
    },
    changePage(pageIndex) {
      var self = this;

      self.loading = true;

      self.pageIndex = pageIndex;
      var formData = new FormData();

      formData.append("pageIndex", self.pageIndex);
      formData.append("pageSize", self.pageSize);

      formData.append("ingredientName", self.queryModel.ingredientName);

      if (this.field != null) {
        formData.append("field", this.field);
      }

      if (this.direction != null) {
        formData.append("direction", this.direction);
      }

      inventoryApi
        .pageList(formData)
        .then(function (response) {
          self.loading = false;

          var jsonData = response.data.data;

          self.tableData = jsonData.data;
          self.totalPages = jsonData.totalPages;
          self.totalElements = jsonData.recordsTotal;
        })
        .catch((error) => {
          self.loading = false;
          // self.$message.error(error + "");
        });
    },
    pageSizeChange(pageSize) {
      this.pageSize = pageSize;

      this.$nextTick(() => {
        this.changePage(this.pageIndex);
      });
    },
    handleReset(name) {
      this.$refs[name].resetFields();
    },
    handleEdit(record) {
      this.modalTitle = "配置";
      this.stockKey = record.id;
      this.showStock = true;
    },
    onStockClose(refreshed) {
      //保存成功后回调
      this.showStock = false;

      if (refreshed) {
        this.changePage(this.pageIndex);
      }
    },
    changeState(record,msg) {
      var self = this;

      self.$confirm("是否确认"+msg+"?", "提示", {confirmButtonText: "确定", cancelButtonText: "取消", type: "warning"}).then(() => {
        inventoryApi.changeState(record.id).then(function (response) {
          var jsonData = response.data;

          if (jsonData.result) {
            self.changePage(self.pageIndex);
            self.$message({type: "success", message: "提交成功!"});
          }
        });
      });
    },
    initialize() {
      var self = this;
      self.loading = true;

      var formData = new FormData();
      formData.append("warehouseId",self.warehouseKey);
      inventoryApi.initialize(formData).then(function (response) {
        self.loading = false;

        var jsonData = response.data;
        if(jsonData.result){
          // console.log(jsonData);
        }
      })
      .catch((error) => {
        self.loading = false;
      });
    },
  },
  mounted: function () {
    this.initialize();
    this.changePage(1);
  },
  components: {
    "inventory-detail": InventoryDetail,
  },
};
</script>
<style lang="scss" scoped>
.el-breadcrumb {
  margin: 10px;
  line-height: 20px;
}

.el-divider {
  margin: 5px 0;
}

.demo-form-inline {
  margin-left: 10px;
  text-align: left;
}

.button-group {
  margin-left: 10px;
  text-align: left;
}
</style>