<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" size="mini" label-width="100px" v-show="showSearch">
        <el-row>
          <el-col :span="4">
            <el-form-item class="query-form-item" label="产品标识DI:">
              <el-input v-model="filterQuery.nameCode" placeholder="产品标识DI"
                        style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item class="query-form-item" label="产品名称:">
              <el-input v-model="filterQuery.cpmctymc" placeholder="产品名称"
                        style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item class="query-form-item" label="批次号:">
              <el-input v-model="filterQuery.batchNo" placeholder="批次号"
                        style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item class="query-form-item" label="供应商名称:">
              <el-select
                v-model="filterQuery.supId"
                filterable
                remote
                clearable="true"
                reserve-keyword
                placeholder="供应商名称"
                :remote-method="findMethod"
                size="mini"
                :loading="corpLoading"
                @change="corpChange"
                style="width: 90%"
              >
                <el-option
                  v-for="item in fromOptions"
                  :key="item.name"
                  :label="item.name"
                  :value="item.erpId"
                >
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="4">
            <el-form-item class="query-form-item" label="规格型号:">
              <el-input v-model="filterQuery.ggxh" placeholder="规格型号"
                        style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item class="query-form-item" label="批准文号:">
              <el-input v-model="filterQuery.zczbhhzbapzbh" placeholder="批准文号"
                        style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item class="query-form-item" label="生产厂家:">
              <el-input v-model="filterQuery.ylqxzcrbarmc" placeholder="生产厂家"
                        style="width: 90%"
                        clearable="true"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="4">
            <el-form-item class="query-form-item" label="所属仓库:">
              <el-select v-model="filterQuery.invCode" placeholder="请选择所属仓库" clearable="true"
                         style="width: 90%"
              >
                <el-option
                  v-for="item in invList"
                  :key="item.name"
                  :label="item.name"
                  :value="item.code">
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="top-right-btn">
        <el-button-group>
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button
            type="primary"
            icon="el-icon-refresh"
            @click="onReset"
          >重置
          </el-button>
          <el-button type="primary" icon="el-icon-search" @click="onSubmit"
          >统计
          </el-button
          >
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>
      <el-table v-loading="loading" :data="list" style="width: 100%" highlight-current-row
                border>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="产品标识" prop="nameCode"></el-table-column>
        <el-table-column label="产品名称" prop="cpmctymc"></el-table-column>
        <el-table-column label="规格型号" prop="ggxh" show-overflow-tooltip></el-table-column>
        <el-table-column label="批次号" prop="batchNo" width="120"></el-table-column>
        <el-table-column label="生产日期" prop="productionDate" width="120"></el-table-column>
        <el-table-column label="失效日期" prop="expireDate" width="120"></el-table-column>
        <el-table-column label="入库数量" prop="inCount" width="120"></el-table-column>
        <el-table-column label="出库数量" prop="outCount" width="120"></el-table-column>
        <el-table-column label="结余数量" prop="reCount" width="120"></el-table-column>
        <el-table-column label="生产厂家" prop="ylqxzcrbarmc" v-if="showSup" show-overflow-tooltip
                         width="120"></el-table-column>
        <el-table-column label="供应商" prop="supName" v-if="showSup" show-overflow-tooltip
                         width="120"></el-table-column>
        <el-table-column label="部门" prop="deptName" v-if="showSup"
                         show-overflow-tooltip width="120"></el-table-column>
        <el-table-column label="仓库" prop="invName" v-if="showSup"
                         show-overflow-tooltip width="120"></el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :limit.sync="filterQuery.limit"
        :page.sync="filterQuery.page"
        @pagination="getList"
      ></pagination>
    </el-card>
  </div>
</template>

<script>
import {getInvListByUser} from "@/api/system/invWarehouse";
import {stockStatistics} from "@/api/inventory/invPorduct";
import {getBasicUnitMaintains} from "@/api/basic/basicUnitMaintain";
import {isBlank} from "@/utils/strUtil";
import invProductsDetail from "@/views/inventory/InvProductsDetail.vue";

export default {
  name: "InvStatistics",
  data() {
    return {
      showSearch: true,
      filterQuery: {
        nameCode: null,
        cpmctymc: null,
        ggxh: null,
        batchNo: null,
        supId: null,
        zczbhhzbapzbh: null,
        ylqxzcrbarmc: null,
        invCode: this.$store.getters.locInvCode,
        page: 1,
        limit: 10,
      },
      list: [],
      total: 0,
      invList: [],
      loading: false,
      corpLoading: false,
      fromOptions: [],
      showSup: false,
    };
  },
  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        nameCode: null,
        cpmctymc: null,
        ggxh: null,
        batchNo: null,
        supId: null,
        zczbhhzbapzbh: null,
        ylqxzcrbarmc: null,
        invCode: this.$store.getters.locInvCode,
        page: 1,
        limit: 10,
      };
      this.getList();
    },
    onSubmit() {
      if (isBlank(this.filterQuery.invCode)) {
        this.$message.warning("请先选择当前仓库！");
        return;
      }
      //判断统计条件是否全部为空
      if (isBlank(this.filterQuery.cpmctymc) && isBlank(this.filterQuery.ggxh) && isBlank(this.filterQuery.zczbhhzbapzbh) && isBlank(this.filterQuery.ylqxzcrbarmc)
        && isBlank(this.filterQuery.invCode) && isBlank(this.filterQuery.nameCode) && isBlank(this.filterQuery.batchNo) && isBlank(this.filterQuery.supId)) {
        this.$message.warning("统计条件不能为空！");
      } else {
        this.filterQuery.page = 1;
        this.getList();
      }
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    handleSizeChange(val) {
      this.filterQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.filterQuery.page = val;
      this.getList();
    },
    getList() {
      this.loading = true;
      stockStatistics(this.filterQuery).then((res) => {
        this.loading = false;
        if (res.code === 20000) {
          this.list = res.data.list || [];
          this.total = res.data.total || 0;
        } else {
          this.$message.error(res.message);
          this.list = [];
          this.total = 0;
        }
      }).catch((error) => {
        this.loading = false;
        this.$message.error(error.message);
        this.list = [];
        this.total = 0;
      })
    },
    getInvList() {
      getInvListByUser()
        .then((response) => {
          this.invList = response.data || [];
          this.getList();
        })
        .catch(() => {
        });
    },
    findMethod(key) {
      this.corpLoading = true;
      this.fromOptions = [];
      let params = {
        key: key,
        corpType: 2,
        page: 1,
        limit: 20
      };
      getBasicUnitMaintains(params).then((res) => {
        this.corpLoading = false;
        this.fromOptions = res.data.list || [];
      }).catch(() => {
        this.corpLoading = false;
      })
    },
    corpChange(value) {
      if (!isBlank(value)) {
        this.findMethod(value);
      }
    },
  },
  components: {
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.findMethod();
    this.getInvList();
    this.getList();
  },
};
</script>

<style type="text/scss" lang="scss">
</style>

