<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" size="mini" label-width="100px" v-show="showSearch">
        <el-row>
          <el-col :span="18">
            <el-form-item label="扫码查询:">
              <el-input v-model="filterQuery.udiCode"
                        style="width: 97%"
                        placeholder="请扫描或输入UDI码"
                        clearable="true"
                        @keyup.enter.native="keyup_submit($event)"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="order-el-row">
          <el-col :span="6">
            <el-form-item label="当前仓库:">
              <el-select v-model="filterQuery.curInvCode" placeholder="请选择当期仓库" clearable="true"
                         style="width: 90%"
                         @change="invChange"
              >
                <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-col :span="6">
            <el-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="6">
            <el-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="6">
            <el-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="6">
            <el-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="6">
            <el-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="6">
            <el-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%"
                @clear="getcorp"
              >
                <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-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 type="primary" icon="el-icon-plus" @click="onAddProduct">添加产品
          </el-button>

          <el-button type="primary" icon="el-icon-plus" @click="buildOrder">生成单据
          </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
                :row-key="row=>row.id"
                @selection-change="handleSelectionChange"
                ref="multipleTable"
                border>


        <el-table-column type="selection" :reserve-selection="true"></el-table-column>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="仓库" prop="invName" 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="DI/物资编码" prop="nameCode" width="150"></el-table-column>
        <el-table-column label="物资名称" prop="cpmctymc" width="180" show-overflow-tooltip></el-table-column>
        <el-table-column label="规格型号" prop="ggxh" show-overflow-tooltip width="180"></el-table-column>
        <el-table-column label="在库数量" prop="invCount" width="120">
        </el-table-column>
        <el-table-column label="定额数量" prop="rationCount" width="120"></el-table-column>
        <el-table-column label="剩余数量" width="120">
          <template slot-scope="scope">
            <span>{{ scope.row.invCount - scope.row.rationCount }}</span>
          </template>
        </el-table-column>
        <el-table-column label="计量单位" prop="measname" width="120"></el-table-column>
        <el-table-column label="生产企业" prop="ylqxzcrbarmc" show-overflow-tooltip
                         width="120"></el-table-column>
        <el-table-column label="注册/备案号" prop="zczbhhzbapzbh" width="160" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" width="180" fixed="right">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click.native.stop="invRemindSet(scope.row)"
            >编辑
            </el-button
            >
            <el-button
              type="text"
              size="small"
              @click.native.stop="detailDialog(scope.row)"
            >详情
            </el-button
            >
            <el-button
              type="text"
              size="small"
              @click.native.stop="deleteDialog(scope.row)"
            >删除
            </el-button
            >

          </template>
        </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>

    <el-dialog
      title="库存定量设置"
      :visible.sync="addInvRemindSetDialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="85%"
      v-if="addInvRemindSetDialogVisible"
    >
      <editRaationSetDialog
        :setFormDat="inputQuery"
        :closeDialog="closeDetailDialog"
      ></editRaationSetDialog>
    </el-dialog>

    <el-dialog
      title="产品录入"
      :visible.sync="selectProductVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="80%"
      v-if="selectProductVisible"
      :append-to-body='true'
    >
      <purApplyProducts
        :closeDialog="closeDetailDialog"
        :invCode="filterQuery.curInvCode"
        :listType="1"
      ></purApplyProducts>
    </el-dialog>


  </div>
</template>

<script>
import {getInvListByUser} from "@/api/system/invWarehouse";
import {getBasicUnitMaintains} from "@/api/basic/basicUnitMaintain";
import {isBlank} from "@/utils/strUtil";
import editRaationSetDialog from "@/views/system/ration/editRaationSetDialog.vue";
import addInvRemindSetDialog from "@/views/inventory/addInvRemindSetDialog.vue";
import {getInvSpaceList} from "@/api/inventory/invSpace";
import {selectUser} from "@/api/auth/authAdmin";
import purApplyProducts from "@/views/purchase/purApply/purOrderSelectProduct";
import {deleteSet, genOrder, getSetList} from "@/api/system/InvRationSet";

export default {
  name: "InvProducts",
  data() {
    return {
      showSearch: true,
      filterQuery: {
        nameCode: null,
        cpmctymc: null,
        ggxh: null,
        batchNo: null,
        supId: null,
        zczbhhzbapzbh: null,
        ylqxzcrbarmc: null,
        curInvCode: this.$store.getters.locInvCode,
        invSpaceCode: null,
        page: 1,
        limit: 10,
        udiCode: null,
      },
      list: [],
      total: 0,
      invList: [],
      loading: false,
      deleteData: {
        id: "",
        status: 10,
      },
      mutlselecteds: [],
      selectProductVisible: false,

      dialogVisible: false,
      corpLoading: false,
      fromOptions: [],
      showSup: false,
      inputQuery: {
        nameCode: null,
        cpmctymc: null,
        ggxh: null,
        measname: null,
        ylqxzcrbarmc: null,
        zczbhhzbapzbh: null,
        invCount: null,
        rationCount: null,
        scanType: null,
        fromInvCode: null,
        auditStatus: null,
        busType: null,
        busInvCode: null,
        busAuditStatus: null,
        remark: null,
      },
      addInvRemindSetDialogVisible: false,
      invId: null,
      spaceList: [],
    };
  },
  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        nameCode: null,
        cpmctymc: null,
        ggxh: null,
        supId: null,
        zczbhhzbapzbh: null,
        ylqxzcrbarmc: null,
        curInvCode: null,
        page: 1,
        limit: 10,
      };
      this.getList();
      this.findMethod();
    },

    keyup_submit(event) {
      this.filterQuery.page = 1;
      this.getList();
      event.target.select();
    },
    onSubmit() {
      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;
      getSetList(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() {
      let query = {};
      getInvListByUser(query)
        .then((response) => {
          this.invList = response.data || [];
          this.getList();
          this.getSpaceList();
        })
        .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);
      }
    },
    getcorp() {
      this.findMethod();
    },

    detailDialog(row) {
      this.inputQuery = row;
      this.addInvRemindSetDialogVisible = true;
    },

    onAddProduct() {
      this.selectProductVisible = true;
    },

    buildOrder() {
      if (this.mutlselecteds.length > 0) {
        let query = {
          rationEntities: this.mutlselecteds
        }
        genOrder(query).then((res) => {
          if (res.code === 20000) {
            this.$message.success("生成成功！");
            this.getList();
            this.mutlselecteds = [];
            this.$refs.multipleTable.clearSelection();
          } else {
            this.$message.error(res.message);
          }
        }).catch((error) => {
          this.$message.error(error.message);
        });
      } else {
        this.$message.warning("请选择要生成单据产品");
      }

    },


    closeDetailDialog() {
      this.addInvRemindSetDialogVisible = false;
      this.selectProductVisible = false;
      this.getList();
    },
    deleteDialog(row) {
      this.$confirm("此操作将永久删除此配置信息，是否继续？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.deleteRationSet(row.id);
      }).catch(() => {
      });
    },
    deleteRationSet(id) {
      let params = {id: id};
      deleteSet(params).then((res) => {
        if (res.code === 20000) {
          this.$message.success("删除成功！");
          this.getList();
        } else {
          this.$message.error(res.message);
        }
      }).catch((error) => {
        this.$message.error(error.message);
      });
    },

    invRemindSet(row) {
      this.inputQuery = {
        id: row.id,
        relId: row.relId,
        supId: row.supId,
        curInvCode: row.curInvCode,
        lyInvCode: row.lyInvCode,
        sgInvCode: row.sgInvCode,
        jhInvCode: row.jhInvCode,
        cgInvCode: row.cgInvCode,
        invName: row.invName,
        supName: row.supName,
        nameCode: row.nameCode,
        cpmctymc: row.cpmctymc,
        ggxh: row.ggxh,
        measname: row.measname,
        ylqxzcrbarmc: row.ylqxzcrbarmc,
        zczbhhzbapzbh: row.zczbhhzbapzbh,
        invCount: row.invCount,
        rationCount: row.rationCount,
        scanType: row.scanType,
        fromInvCode: row.fromInvCode,
        auditStatus: row.auditStatus == null ? 1 : row.auditStatus,
        busType: row.busType,
        busInvCode: row.busInvCode,
        busAuditStatus: row.busAuditStatus == null ? 1 : row.busAuditStatus,
        remark: row.remark,
      }
      this.addInvRemindSetDialogVisible = true;
    },

    closeRemindSetDialog() {
      this.invId = null;
      this.addInvRemindSetDialogVisible = false;
    },
    getSpaceList() {

      if (isBlank(this.filterQuery.curInvCode)) {
        return;
      }
      let params = {
        invWarehouseCode: this.filterQuery.curInvCode,
        status: 1
      };
      getInvSpaceList(params).then((res) => {
        this.spaceList = res.data.list || [];
      })
    },
    querySearchAsync(queryString, cb) {
      let restaurants = this.spaceList;
      restaurants.forEach((item) => {
        item.value = item.name;
      })

      let results = queryString ? restaurants.filter(this.createStateFilter(queryString)) : restaurants;
      if (!isBlank(queryString)) {
        this.spaceSearchResult = results;
      }
      cb(results);
    },
    invChange() {
      this.getSpaceList();
    },

    handleSelectionChange(val) {
      this.mutlselecteds = val;
      console.log(this.mutlselecteds)
    },
  },
  components: {
    purApplyProducts,
    editRaationSetDialog,
    addInvRemindSetDialog
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.filterQuery.curInvCode = this.$store.getters.locInvCode
    selectUser().then((res) => {
      let supId = res.data.customerId
      if (supId == "110") {
        this.showSup = true;
      }
    }).catch(() => {
      this.corpLoading = false;
    });
    this.findMethod();
    this.getInvList();
    this.getList();
  },
};
</script>

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

