<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" size="mini" :inline="true">
        <el-form-item class="query-form-item" label="仓库:">
          <el-select v-model="filterQuery.invCode" placeholder="请选择仓库" clearable="true"
                     @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-form-item class="query-form-item" label="货位:">
          <el-select v-model="filterQuery.invSpaceCode" placeholder="请选择货位" clearable="true"
          >
            <el-option
              v-for="item in spaceList"
              :key="item.name"
              :label="item.name"
              :value="item.code">
              <span style="float: left">{{ item.name }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button-group style="margin-left: 10px;display:flex;">
            <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-box" @click="invPlace">摆放</el-button>
          </el-button-group>
        </el-form-item>
      </el-form>

      <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="invName" width="180"></el-table-column>
        <el-table-column label="货位" prop="invSpaceName" width="180"></el-table-column>
        <el-table-column label="产品标识" prop="nameCode"></el-table-column>
        <el-table-column label="产品名称" prop="productName"></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="zczbhhzbapzbh" v-if="showSup" show-overflow-tooltip
                         width="120"></el-table-column>
        <el-table-column label="生产厂家" prop="ylqxzcrbarmc" 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>

    <el-dialog
      title="库存摆放"
      :visible.sync="placeDialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="60%"
      v-if="placeDialogVisible"
    >
      <el-card style="margin: 5px;margin-top: -20px">
        <el-form :model="formData" label-width="100px" style="margin-bottom: -15px">
          <el-button-group style="display: flex;margin: 0px 0 10px 80%; height: 35px">
            <el-button
              size="mini"
              type="primary"
              @click.native="saveData()"
            >提交
            </el-button
            >
          </el-button-group>
          <el-row>
            <el-col :span="10">
              <el-form-item prop="invWarehouseCode" label="当前仓库:">
                <el-select v-model="formData.invCode"
                           placeholder="当前仓库信息"
                           :disabled="codeArray.length>0"
                           @change="formInvChange"
                           style="width: 90%"
                           clearable>
                  <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="10">
              <el-form-item class="query-form-item" label="当前货位:">
                <el-select v-model="formData.invSpaceCode"
                           placeholder="当前货位信息"
                           :disabled="codeArray.length>0"
                           style="width: 90%"
                           clearable>
                  <el-option
                    v-for="item in formSpaceList"
                    :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-row>
            <el-col :span="18">
              <el-form-item class="query-form-item" label="扫码录入:">
                <el-input
                  id="inputer"
                  @focus="getInputFocus($event)"
                  @keypress.enter.native="enterKey($event)"
                  ref='inputRef'
                  style="ime-mode:disabled"
                  type="tel"
                  v-model="formData.code"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="2">
              <el-button
                type="primary"
                size="mini"
                @click.native.stop="addCode()"
                style="margin-left: 15px"
              >添加
              </el-button
              >
            </el-col>
          </el-row>

          <el-table v-loading="codeTableLoading" :data="codeArray" style="width: 100%;margin-top: 20px;"
                    highlight-current-row
                    border max-height="300" height="300">
            <el-table-column label="序号" type="index" width="50"></el-table-column>
            <el-table-column label="条码" prop="code" show-overflow-tooltip>
            </el-table-column>
            <el-table-column label="操作"  width="150">
              <template slot-scope="scope">
                <el-button
                  type="text"
                  size="small"
                  @click.stop="true"
                  @click.native="deleteCodeArray(scope.$index, scope.row)"
                >删除
                </el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-form>
      </el-card>
    </el-dialog>

  </div>
</template>

<script>
import {getInvListByUser} from "@/api/system/invWarehouse";
import {getInvSpaceList} from "@/api/inventory/invSpace";
import {getPlaceDetailList, bindInvSpace, checkCodeSpace} from "@/api/inventory/invPlace";
import {isBlank} from "@/utils/strUtil";

export default {
  name: "InvProducts",
  data() {
    return {
      filterQuery: {
        invCode: this.$store.getters.locInvCode,
        invSpaceCode: null,
        page: 1,
        limit: 20,
      },
      list: [],
      total: 0,
      invList: [],
      spaceList: [],
      loading: false,
      fromOptions: [],
      formData: {
        invCode: null,
        invSpaceCode: null,
        code: null,
        codeArray: []
      },
      placeDialogVisible: false,
      codeTableLoading: false,
      codeArray: [],
      formInvList: [],
      formSpaceList: []
    };
  },
  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        invCode: this.$store.getters.locInvCode,
        invSpaceCode: null,
        page: 1,
        limit: 20,
      };
      this.getList();
    },
    onSubmit() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getPlaceDetailList(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;
      })
    },
    invChange() {
      this.spaceList = [];
      this.getSpaceList();
      this.getList();
    },
    getInvList() {
      getInvListByUser()
        .then((response) => {
          this.invList = response.data || [];
          this.getSpaceList();
          this.getList();
        })
        .catch(() => {
        });
    },
    getSpaceList() {
      let params = {
        invWarehouseCode: this.filterQuery.invCode,
        status: 1
      };
      getInvSpaceList(params).then((res) => {
        this.spaceList = res.data.list || [];
      })
    },
    formInvChange() {
      this.formData.invSpaceCode = null;
      let params = {
        invWarehouseCode: this.formData.invCode,
        status: 1
      };
      getInvSpaceList(params).then((res) => {
        this.formSpaceList = res.data.list || [];
      })
    },
    invPlace() {
      this.placeDialogVisible = true;
      this.formData = {
        invCode: null,
        invSpaceCode: null,
        code: null,
        codeArray: []
      };
      this.codeArray = [];
    },
    deleteCodeArray(index, row) {
      for (let i = 0; i < this.codeArray.length; i++) {
        if (this.codeArray[i] === row) {
          this.codeArray.splice(i, 1);
          return;
        }
      }
    },
    addCode(event) {
      if (event != null) {
        event.target.select();
      }
      this.$refs.inputRef.select();
      if (isBlank(this.formData.invCode)) {
        this.$message.error('当前仓库不能为空');
        return;
      }

      if (isBlank(this.formData.invSpaceCode)) {
        this.$message.error('当前货位不能为空');
        return;
      }

      this.formData.code = this.formData.code.trim();
      if (this.$isBlank(this.formData.code)) return;
      let tQuery = Object.assign(JSON.parse(JSON.stringify(this.formData)));
      this.checkCode(tQuery);
      this.$refs.inputRef.select();
    },
    getInputFocus(event) {
      event.currentTarget.select();
    },
    enterKey(event) {
      this.checkSuccess = true;
      this.addCode(event);
      this.$refs.inputRef.focus();
      this.$refs.inputRef.select();
    },
    checkCode(tQuery) {
      //检验是否重复添加
      let flag = false;
      this.codeArray.forEach(item => {
        if (item.code === this.formData.code) {
          this.$message.error("重复条码！");
          flag = true;
          return;
        }
      });
      if (flag) {
        return;
      }
      //校验此条码是否已经被绑定到其他货位，如已被绑定，则提示，点击确定之后，重置此库存数据的仓库和分库数据
      checkCodeSpace(tQuery).then((res) => {
        if (res.code === 20000) {
          this.codeArray.push({code: this.formData.code});
        } else if (res.code === 8) {
          this.loading = false;
          this.$message.error(res.message);
        } else if (res.code === 7) {
          this.$confirm(res.message, '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.codeArray.push({code: this.formData.code});
          }).catch(() => {
            this.loading = false;
          });
        }
      });
    },
    saveData() {
      if (isBlank(this.formData.invCode) || isBlank(this.formData.invSpaceCode)) {
        this.$message.warning("请选择仓库和货位信息");
        return;
      }
      if (this.codeArray.length === 0) {
        this.$message.warning("请扫描需要摆放的货物");
        return;
      }
      this.formData.codeArray = [];
      this.codeArray.forEach(item => {
        this.formData.codeArray.push(item.code);
      });
      bindInvSpace(this.formData).then((res) => {
        if (res.code === 20000) {
          this.$message.success("绑定成功");
          this.placeDialogVisible = false;
          this.getList();
        } else {
          this.$message.error("绑定失败");
        }
      })
    },
  },
  mounted() {
    document.body.ondrop = function (event) {
      event.preventDefault();
      event.stopPropagation();
    };
  },
  created() {
    this.getInvList();
  },
};
</script>

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

