<template>
  <div v-loading="loading">
    <div class="form-box">
      <el-form
        label-position="top"
        :model="form"
        ref="form"
        :rules="formRules"
        label-width="100px"
      >
        <div v-if="submitType === '10'">
          <el-row>
            <el-form-item label="组合名称" prop="productId">
              <el-select
                v-model="form.productId"
                placeholder="请选择"
                style="width: 100%"
                @change="handleGetCanQuantity"
              >
                <el-option
                  v-for="item in combinationNameList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-row>
          <!-- <el-row> 规格：xxxxxxxx </el-row> -->
          <el-form-item label="组合后放置仓库" prop="combinationWarehouseId">
            <el-select
              v-model="form.combinationWarehouseId"
              placeholder="请选择"
              style="width: 100%"
            >
              <el-option
                v-for="item in warehouseSelectList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="半成品所在仓库" prop="sourceWarehouseId">
            <el-select
              v-model="form.sourceWarehouseId"
              placeholder="请选择"
              style="width: 100%"
              @change="handleGetCanQuantity"
            >
              <el-option
                v-for="item in warehouseSelectList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <el-row>
            <el-col :span="8">
              <el-form-item label="组合数量" prop="combinationQuantity">
                <el-input
                  v-model="form.combinationQuantity"
                  :disabled="canQuantity === 0"
                  placeholder="请输入"
                >
                </el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <div style="color: #999999">可组合数量：{{ canQuantity }}</div>
        </div>
        <div v-else>
          <el-row :gutter="10">
            <el-col :span="12">
              <el-form-item
                label="组合后放置仓库"
                prop="combinationWarehouseId"
              >
                <el-select
                  v-model="form.combinationWarehouseId"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in warehouseSelectList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="半成品所在仓库" prop="sourceWarehouseId">
                <el-select
                  v-model="form.sourceWarehouseId"
                  placeholder="请选择"
                  style="width: 100%"
                  @change="handleGetCanQuantity"
                >
                  <el-option
                    v-for="item in warehouseSelectList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <div style="margin-top: 20px">
            <labelTitle content="组合明细"></labelTitle>
          </div>
          <div style="margin: 10px 0">
            <el-button size="small" @click="selectDialog = true">
              添加产品</el-button
            >
          </div>
          <el-table :data="form.productList">
            <el-table-column label="产品名称" prop="productName">
            </el-table-column>
            <!-- <el-table-column label="可组合数量" prop="name"> </el-table-column> -->
            <el-table-column label="组合数量" prop="combinationQuantity">
              <template slot-scope="scope">
                <el-form-item
                  :prop="'productList.' + scope.$index + '.combinationQuantity'"
                  :rules="formRules.combinationQuantity"
                  :inline-message="true"
                  label-width="0"
                >
                  <el-input
                    v-model="scope.row.combinationQuantity"
                    placeholder="请输入"
                    size="mini"
                  >
                  </el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="100" align="center">
              <template slot-scope="scope">
                <el-button type="text" @click="deleteRow(scope.$index)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </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
        :isCombination="isCombination"
        @select="handleSelect"
      ></selectProduct>
    </el-dialog>
  </div>
</template>

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

import {
  getCombinationNameList,
  getCanQuantity,
} from "@/api/inventory-management/combination.js";

import { warehouseSelectList } from "@/api/product-material/warehouse/index.js";

export default {
  name: "addCombination",
  components: { labelTitle, selectProduct },
  props: {
    form: {
      type: Object,
      default: () => {},
    },
    submitType: {
      type: String,
      default: "10",
    },
  },
  data() {
    return {
      loading: false,
      warehouseSelectList: [],
      combinationNameList: [],
      canQuantity: 0,
      formRules: {
        productId: [
          {
            required: true,
            message: "请选择组合名称",
            trigger: "change",
          },
        ],
        combinationWarehouseId: [
          {
            required: true,
            message: "请选择组合后放置仓库",
            trigger: "change",
          },
        ],
        sourceWarehouseId: [
          {
            required: true,
            message: "请选择半成品所在仓库",
            trigger: "change",
          },
        ],
        combinationQuantity: [
          {
            required: true,
            message: "请输入组合数量",
            trigger: "blur",
          },
        ],
      },
      selectDialog: false,
      isCombination: true, //默认让选择产品组件请求是组合的产品
    };
  },
  created() {
    this.getData();
  },
  methods: {
    getData() {
      getCombinationNameList().then((res) => {
        this.combinationNameList = res.data.data;
      });
      warehouseSelectList().then((res) => {
        this.warehouseSelectList = res.data.data;
      });
    },
    handleGetCanQuantity() {
      if (this.form.productId && this.form.sourceWarehouseId) {
        getCanQuantity({
          productId: this.form.productId,
          sourceWarehouseId: this.form.sourceWarehouseId,
        }).then((res) => {
          this.canQuantity = res.data.data;
        });
      }
    },
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          if (this.submitType === "20") {
            this.form.productList = this.form.productList.map((x) => ({
              productId: x.productId,
              combinationQuantity: x.combinationQuantity,
              combinationWarehouseId: this.form.combinationWarehouseId,
              sourceWarehouseId: this.form.sourceWarehouseId,
            }));
          }
          this.$emit("submit");
        }
      });
    },
    handleCancel() {
      this.$emit("cancel");
    },
    handleSelect(row) {
      const flag = this.form.productList.some((x) => x.productId === row.id);
      if (flag) return this.msgInfo("该产品已经选择");
      const product = {
        productId: row.id,
        productName: row.name,
        combinationQuantity: "",
      };
      this.form.productList.push(product);
      this.msgSuccess("选择成功");
    },
    deleteRow(index) {
      this.form.productList.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>