<template>
  <!-- 新增入库转预留作业页面 -->
  <div class="addCheckWarehouse">
    <page-title>预留转入库作业</page-title>
    <div class="material from">
      <span class="text">物资规格型号：</span>
      <el-input class="input" v-model="materialName" disabled> </el-input>
      <el-button type="primary" @click="ondrawer(1)">浏览</el-button>
    </div>
    <div class="materialNumber from">
      <span class="text">最大入库件数：</span>
      <el-input class="input" v-model="max" disabled> </el-input>
    </div>
    <div class="materialNumber from">
      <span class="text">件数：</span>
      <el-input class="input" v-model.lazy="materialNumber"> </el-input>
    </div>
    <div class="materialNumber from">
      <span class="text">理重(磅重)</span>
      <el-input class="input" v-model="materialTheWeight" disabled> </el-input>
    </div>
    <div class="materialNumber from">
      <span class="text">实重</span>
      <el-input class="input" v-model="actuallyWeight"> </el-input>
    </div>
    <div class="materialNumber from">
      <span class="text">备注</span>
      <el-input class="input" v-model="remark"> </el-input>
    </div>
    <!-- 模态窗口 -->
    <el-drawer :visible.sync="drawer" :direction="direction" size="50%">
      <div style="margin-bottom: 10px">
        <el-input
          placeholder="请输入内容"
          v-model="input"
          style="margin-top: 10px; margin-right: 10px; width: 250px"
          clearable
        ></el-input>
        <el-button type="primary" class="btn" @click="onclick(a)">
          <i class="el-icon-search"></i>查询
        </el-button>
      </div>
      <div v-show="a == 1">
        <dilTable v-bind.sync="first" @radio-change="currentRadioChange1">
          <el-table-column label="操作" width="140px">
            <template slot-scope="scope">
              <el-input v-model.number="scope.row.quantity" style="width:60px">
              </el-input>
              <el-button
                type="text"
                size="mini"
                @click="deleteInboundMaterial(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </dilTable>
      </div>
      <div v-show="a == 2">
        <dilTable
          v-bind.sync="second"
          @radio-change="currentRadioChange2"
        ></dilTable>
      </div>
    </el-drawer>
    <div class="button_box">
      <el-button @click="onClickCancel">返回</el-button>
      <el-button type="primary" @click="onClickConfirm" :disabled="disabled"
        >确认</el-button
      >
    </div>
  </div>
</template>
<script>
import PageTitle from "@/components/Page/Title";
import { sjTime, isNumber, isIntegerNumber } from "@/utils/sharedJsFile";
import { getCookie } from "@/utils/util.js";
export default {
  components: { PageTitle },
  data() {
    return {
      startTime: null,
      endTime: null,
      max: null,
      disabled: false,
      //仓库名称
      warehouseName: null,
      //物资名称
      materialName: null,
      //物资编码
      materialCode: null,
      // 物资规格
      materialSpecification: null,
      // 物资型号
      materialModel: null,
      //盘点数量
      materialNumber: null,
      //物资id
      materialId: null,
      //是否需要打开模态窗口
      drawer: false,
      a: 1,
      direction: "rtl",
      input: "",
      first: {
        requestUrl: "",
        selectionType: "radio",
        mapList1: []
      },
      second: {
        requestUrl: "",
        selectionType: "radio"
      },
      materialTheWeight: 0,
      actuallyWeight: 0,
      remark: null
    };
  },
  watch: {
    drawer(val) {
      if (!val) {
        this.input = null;
      }
    },
    materialNumber() {
      this.getTheoreticalByInfo();
    }
  },
  created() {
    let strTime = this.formatDate(new Date(), "yyyy-MM-dd hh:mm:ss");
    this.startTime = Date.parse(new Date(strTime));
    this.endTime = new Date(this.startTime + 86400000);
  },
  methods: {
    deleteInboundMaterial(row) {
      if (Number(row.quantity) > 0) {
        this.$confirm("确定删除?继续", "提示", {
          cancelButtonText: "取消",
          confirmButtonText: "确定"
        }).then(() => {
          console.log(row);
          this.axios
            .post(
              "/api/v1/wms/deleteInboundMaterial?warehouseId=3&materialCode=" +
                row.materialCode +
                "&materialSpecification=" +
                row.materialSpecification +
                "&materialModel=" +
                row.materialModel +
                "&quantity=" +
                row.quantity
            )
            .then(res => {
              if (res.data.code == 200) {
                this.$message.success("删除成功");
                this.first.requestUrl =
                  "/api/v1/wms/getSteelMaterialModelList?apiId=447&warehouseId=" +
                  3 +
                  "&status=" +
                  1 +
                  "&con=" +
                  "" +
                  "&startTime=" +
                  sjTime(this.startTime) +
                  "&endTime=" +
                  sjTime(this.endTime) +
                  "&i=" +
                  new Date();
              } else {
                this.$message.error("删除失败");
              }
            });
        });
      } else {
        this.$message.warning("请输入需要删除的物资件数");
      }
    },
    getTheoreticalByInfo() {
      this.axios
        .post(
          "/api/v1/wms/getTheoreticalByInfo?warehouseId=" +
            3 +
            "&materialCode=" +
            this.materialCode +
            "&materialSpecification=" +
            this.materialSpecification +
            "&materialModel=" +
            this.materialModel +
            "&num=" +
            this.materialNumber +
            "&status=1" +
            "&i=" +
            new Date()
        )
        .then(res => {
          if (res.data.data == -1) {
            this.$message.error("数量超出");
            this.materialTheWeight = null;
          } else {
            this.materialTheWeight = parseFloat(res.data.data);
          }
        });
    },
    formatDate(date, fmt) {
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(
          RegExp.$1,
          (date.getFullYear() + "").substr(4 - RegExp.$1.length)
        );
      }
      let o = {
        "M+": date.getMonth() + 1,
        "d+": date.getDate(),
        "h+": 0,
        "m+": 0,
        "s+": 0
      };
      for (let k in o) {
        if (new RegExp(`(${k})`).test(fmt)) {
          let str = o[k] + "";
          fmt = fmt.replace(
            RegExp.$1,
            RegExp.$1.length === 1 ? str : this.padLeftZero(str)
          );
        }
      }
      return fmt;
    },
    padLeftZero(str) {
      return ("00" + str).substr(str.length);
    },
    onclick(a) {
      if (a == 1) {
        console.log(this.input);
        this.first.requestUrl =
          "/api/v1/wms/getSteelMaterialModelList?apiId=447&warehouseId=" +
          3 +
          "&status=" +
          1 +
          "&con=" +
          this.input +
          "&startTime=" +
          sjTime(this.startTime) +
          "&endTime=" +
          sjTime(this.endTime) +
          "&i=" +
          new Date();
      }
    },
    ondrawer(num) {
      this.drawer = true;
      this.a = num;
      if (num == 1) {
        this.first.requestUrl =
          "/api/v1/wms/getSteelMaterialModelList?apiId=447&warehouseId=" +
          3 +
          "&status=" +
          1 +
          "&con=" +
          "" +
          "&startTime=" +
          sjTime(this.startTime) +
          "&endTime=" +
          sjTime(this.endTime) +
          "&i=" +
          new Date();
      }
    },
    // 返回
    onClickCancel() {
      this.$router.push("/steel_inbound");
    },
    currentRadioChange1(selection) {
      console.log(selection);
      this.materialName = selection.materialName;
      this.materialCode = selection.materialCode;
      this.materialSpecification = selection.materialSpecification;
      this.materialModel = selection.materialModel;
      this.max = selection.max;
    },
    currentRadioChange2(selection) {
      this.warehouseId = selection.warehouseId;
      this.warehouseName = selection.warehouseName;
    },
    // 确认
    onClickConfirm() {
      this.disabled = true;
      // if(isNumber(this.actuallyWeight))
      if (!isNumber(this.actuallyWeight)) {
        this.$message.error("实重填写不正确");
        return;
      }
      this.axios
        .post(
          "/api/v1/wms/insertReserved?warehouseId=" +
            3 +
            "&materialCode=" +
            this.materialCode +
            "&materialSpecification=" +
            this.materialSpecification +
            "&materialModel=" +
            this.materialModel +
            "&quantity=" +
            this.materialNumber +
            "&actuallyWeight=" +
            Number(parseFloat(this.actuallyWeight)) +
            "&materialTheWeight=" +
            Number(parseFloat(this.materialTheWeight)) +
            "&remark=" +
            this.remark
        )
        .then(res => {
          console.log(res.data);
          if (res.data.code == "200") {
            this.$message({
              message: "入库成功!",
              type: "success"
            });
            this.disabled = false;
            this.onClickCancel();
            this.$router.go(-1);
          } else if (res.data.code == "201") {
            this.$message({
              message: "入库的数量不正确",
              type: "faile"
            });
            this.disabled = false;
          }
          this.$router.go(-1);
        })
        .catch(() => {
          this.disabled = false;
        });
    }
  }
};
</script>
<style lang="scss" scope>
.addCheckWarehouse {
  .contractDetails {
    width: 100%;
  }
  .from {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-top: 5px;
    margin-bottom: 20px;
  }
  .warehouse {
    display: flex;
    justify-content: center;
    margin-top: 20px;
    margin-right: 70px;
    .text {
      display: inline-block;
      width: 170px;
      text-align: right;
    }
    .input {
      width: 250px;
      margin-right: 20px;
    }
  }
  .material {
    display: flex;
    justify-content: center;
    margin-top: 20px;
    .text {
      display: inline-block;
      width: 170px;
      text-align: right;
    }
    .input {
      width: 250px;
      margin-right: 20px;
    }
  }
  .materialNumber {
    display: flex;
    justify-content: center;
    margin-top: 20px;
    margin-right: 70px;
    .text {
      display: inline-block;
      width: 170px;
      text-align: right;
    }
    .input {
      width: 250px;
      margin-right: 20px;
    }
  }
  .button_box {
    display: flex;
    justify-content: center;
    align-items: center;
    width: 100%;
    height: 100px;
  }
}
</style>
