<template>
  <div v-loading="loading">
    <div class="form-box">
      <el-form
        label-position="top"
        :model="form"
        ref="form"
        :rules="formRules"
        label-width="100px"
      >
        <el-row>
          <el-col :span="6">
            <el-form-item label="仓库名称" prop="warehouseId">
              <el-select
                v-model="form.warehouseId"
                placeholder="请选择"
                style="width: 100%"
                @change="handleChangeWarehouse"
              >
                <el-option
                  v-for="item in warehouseSelectList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                  <span style="float: left">{{
                    `${item.name}（${dictDataEcho(
                      item.type,
                      warehouseTypeList
                    )}）`
                  }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="10">
            <el-form-item label="备注" prop="remarks">
              <el-input
                placeholder="请输入"
                v-model="form.remarks"
                type="textarea"
                :rows="4"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <div style="margin-bottom: 20px">
          <labelTitle content="入库明细"></labelTitle>
        </div>
        <el-form-item label-width="0px">
          <el-button type="primary" size="mini" @click="handleAddProduct">
            添加明细</el-button
          >
        </el-form-item>
        <el-form-item>
          <el-table :data="form.changeProductList">
            <el-table-column label="物品编码" prop="productCode">
            </el-table-column>
            <el-table-column label="物品名称" prop="productName">
            </el-table-column>
            <el-table-column label="规格" prop="specs"> </el-table-column>

            <el-table-column label="本次入库" prop="quantity">
              <template slot-scope="scope">
                <el-form-item
                  :prop="'changeProductList.' + scope.$index + '.quantity'"
                  :rules="formRules.quantity"
                  :inline-message="true"
                  label-width="0"
                >
                  <el-input
                    v-model="scope.row.quantity"
                    placeholder="请输入"
                    size="mini"
                  >
                  </el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="100" align="left">
              <template slot-scope="scope">
                <el-button type="text" @click="deleteRow(scope.$index)">
                  删除
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
      </el-form>
    </div>
    <div style="text-align: center; margin-top: 15px">
      <el-button size="small" @click="handleCancel">取消 </el-button>
      <el-button type="primary" size="small" @click="handleSubmit">
        确定</el-button
      >
    </div>

    <el-dialog
      title="物品选择"
      v-if="selectDialog"
      :visible.sync="selectDialog"
      width="80%"
      top="60px"
    >
      <selectProduct @select="handleSelect"></selectProduct>
    </el-dialog>
  </div>
</template>

<script>
import labelTitle from "@/components/label-title/index.vue";
import selectProduct from "@/components/select-product/index.vue";

export default {
  name: "addInbound",
  components: { labelTitle, selectProduct },
  props: {
    form: {
      type: Object,
      default: () => {},
    },
    warehouseSelectList: {
      type: Array,
      default: () => [],
    },
    warehouseTypeList: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      selectLit: [],
      loading: false,
      formRules: {
        warehouseId: [
          {
            required: true,
            message: "请选择仓库",
            trigger: "change",
          },
        ],
        quantity: [
          {
            required: true,
            message: "请输入本次入库数量",
            trigger: "blur",
          },
        ],
      },

      selectDialog: false,
      warehouseName: "",
    };
  },
  created() {},
  methods: {
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.form.changeProductList.length < 1) {
            return this.msgInfo("请添加入库明细！");
          }
          this.loading = true;
          this.form.changeProductList = this.form.changeProductList.map(
            (x) => ({
              productId: x.productId,
              quantity: x.quantity,
            })
          );
          this.$emit("submit");
        }
      });
    },
    handleCancel() {
      this.$emit("cancel");
    },

    handleChangeWarehouse(id) {
      // const current = this.warehouseSelectList.find((x) => x.id === id);
      // if (current) {
      //   this.warehouseName = current.name;
      // }
      this.form.changeProductList = [];
    },
    handleAddProduct() {
      if (!this.form.warehouseId) return this.msgInfo("请先选择仓库");
      this.selectDialog = true;
    },
    handleSelect(row) {
      const flag = this.form.changeProductList.some(
        (x) => x.productId === row.id
      );
      if (flag) return this.msgInfo("该物品已经选择");
      const product = {
        productCode: row.code,
        productName: row.name,
        productId: row.id,
        specs: row.specs,
        quantity: "",
      };
      this.form.changeProductList.push(product);
      this.msgSuccess("选择成功");
    },
    deleteRow(index) {
      this.form.changeProductList.splice(index, 1);
      this.msgSuccess("删除成功");
    },
  },
};
</script>

<style lang="scss" scoped>
.form-box {
  height: calc(100vh - 280px);
  overflow: auto;
  box-sizing: border-box;
  padding: 10px;
}
::v-deep {
  .el-form-item {
    margin-bottom: 3px;
  }
  .el-form--label-top .el-form-item__label {
    padding: 8px 0 0 0;
  }
}
</style>