<template>
  <div>
    <el-card class="box-card">
      <byForm :formConfig="formConfig" :formOption="formOption" v-model="formData.data" :rules="rules" ref="submit">
        <template #basicInformation>
          <div style="width: 100%">
            <el-row>
              <el-col :span="12">
                <el-form-item label="事业部" prop="departmentId" style="width: 100%; margin-bottom: 18px">
                  <el-select v-model="formData.data.departmentId" placeholder="请选择事业部" clearable style="width: 100%">
                    <el-option v-for="item in departmentList" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey" />
                  </el-select>
                </el-form-item>
                <el-form-item label="出库仓库" prop="outWarehouseId" style="width: 100%; margin-bottom: 18px">
                  <el-select v-model="formData.data.outWarehouseId" placeholder="请选择出库仓库" clearable style="width: 100%">
                    <el-option v-for="item in warehouseList" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey" />
                  </el-select>
                </el-form-item>
                <el-form-item label="入库仓库" prop="inWarehouseId" style="width: 100%; margin-bottom: 18px">
                  <el-select v-model="formData.data.inWarehouseId" placeholder="请选择入库仓库" clearable style="width: 100%">
                    <el-option v-for="item in warehouseList" :key="item.dictKey" :label="item.dictValue" :value="item.dictKey" />
                  </el-select>
                </el-form-item>
                <el-form-item label="申请人" prop="applicant" style="width: 100%; margin-bottom: 18px">
                  <el-input v-model="formData.data.applicant" placeholder="请输入申请人" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="备注" prop="remark" style="width: 100%; margin-bottom: 18px">
                  <el-input v-model="formData.data.remark" :rows="4" type="textarea" placeholder="请输入备注" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </template>
        <template #inOutStorageBomList>
          <div style="width: 100%; padding: 0 20px">
            <div style="margin-bottom: 10px">
              <el-button type="primary" size="small" @click="clickAddBOM()">选择BOM</el-button>
            </div>
            <el-table :data="formData.data.inOutStorageBomList" :row-style="{ height: '35px' }" header-row-class-name="tableHeader">
              <el-table-column label="品号" prop="code" width="180" />
              <el-table-column label="品名" prop="name" min-width="220" />
              <el-table-column label="单品尺寸（L*W*H）" align="center" width="300">
                <template #default="{ row }">
                  <span>{{ `${row.length} * ${row.width} * ${row.height}` }}</span>
                </template>
              </el-table-column>
              <el-table-column label="入库数量" width="180">
                <template #default="{ row, $index }">
                  <el-form-item :prop="'inOutStorageBomList.' + $index + '.quantity'" :rules="rules.quantity" :inline-message="true" style="width: 100%">
                    <el-input-number
                      onmousewheel="return false;"
                      v-model="row.quantity"
                      placeholder="入库数量"
                      style="width: 100%"
                      :controls="false"
                      :min="0"
                      :precision="0" />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center" fixed="right" width="60">
                <template #default="{ $index }">
                  <el-button type="danger" @click="clickDelete($index)" text>删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </div>
        </template>
      </byForm>
      <div style="text-align: center; margin: 10px">
        <el-button @click="clickCancel()" size="large">取 消</el-button>
        <el-button type="primary" @click="submitForm()" size="large" v-preReClick>确 定</el-button>
      </div>
    </el-card>

    <el-dialog title="选择BOM" v-if="openBOM" v-model="openBOM" width="90%">
      <SelectBOM :selectStatus="true" @selectBOM="selectBOM"></SelectBOM>
      <template #footer>
        <el-button @click="openBOM = false" size="large">关 闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import byForm from "@/components/byForm/index";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
import useTagsViewStore from "@/store/modules/tagsView";
import SelectBOM from "@/views/group/BOM/management/index";

const { proxy } = getCurrentInstance();
const departmentList = ref([{ dictKey: "0", dictValue: "胜德体育" }]);
const warehouseList = ref([]);
const router = useRouter();
const submit = ref(null);
const formOption = reactive({
  inline: true,
  labelWidth: "100px",
  itemWidth: 100,
  rules: [],
  labelPosition: "right",
});
const formData = reactive({
  data: {
    applicant: proxy.useUserStore().user.nickName,
    inOutStorageBomList: [],
  },
});
const formConfig = computed(() => {
  return [
    {
      type: "title",
      title: "入库信息",
      label: "",
    },
    {
      type: "slot",
      slotName: "basicInformation",
      label: "",
    },
    {
      type: "title",
      title: "物料信息",
      label: "",
    },
    {
      type: "slot",
      slotName: "inOutStorageBomList",
      label: "",
    },
  ];
});
const rules = ref({
  departmentId: [{ required: true, message: "请选择事业部", trigger: "change" }],
  outWarehouseId: [{ required: true, message: "请选择出库仓库", trigger: "change" }],
  inWarehouseId: [{ required: true, message: "请选择入库仓库", trigger: "change" }],
  applicant: [{ required: true, message: "请输入申请人", trigger: "blur" }],
  quantity: [{ required: true, message: "请输入调拨数量", trigger: "blur" }],
});
const getDemandData = () => {
  proxy.post("/department/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      departmentList.value = departmentList.value.concat(
        res.rows.map((item) => {
          return {
            dictKey: item.id,
            dictValue: item.name,
          };
        })
      );
    }
  });
  proxy.post("/warehouse/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      warehouseList.value = res.rows.map((item) => {
        return {
          dictKey: item.id,
          dictValue: item.name,
        };
      });
    }
  });
};
getDemandData();
const openBOM = ref(false);
const clickAddBOM = () => {
  openBOM.value = true;
};
const selectBOM = (item) => {
  if (formData.data.inOutStorageBomList && formData.data.inOutStorageBomList.length > 0) {
    let list = formData.data.inOutStorageBomList.filter((itemFilter) => itemFilter.bomSpecId === item.id);
    if (list && list.length > 0) {
      return ElMessage("该BOM已添加");
    }
  }
  if (item.id) {
    formData.data.inOutStorageBomList.push({
      bomSpecId: item.id,
      code: item.code,
      name: item.name,
      length: item.length,
      width: item.width,
      height: item.height,
      quantity: undefined,
    });
    ElMessage({ message: "选择完成", type: "success" });
  }
};
const clickDelete = (index) => {
  formData.data.inOutStorageBomList.splice(index, 1);
};
const submitForm = () => {
  submit.value.handleSubmit(() => {
    if (formData.data.inOutStorageBomList && formData.data.inOutStorageBomList.length > 0) {
      proxy.post("/inOutStorage/add", formData.data).then(() => {
        ElMessage({
          message: "提交成功",
          type: "success",
        });
        clickCancel();
      });
    } else {
      return ElMessage("请添加BOM");
    }
  });
};
const clickCancel = () => {
  const useTagsStore = useTagsViewStore();
  useTagsStore.delVisitedView(router.currentRoute.value);
  router.replace({
    path: "/production/warehouse/warehouse-putInStorage",
  });
};
</script>

<style lang="scss" scoped>
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}
:deep(.el-dialog) {
  margin-top: 10px !important;
  margin-bottom: 10px !important;
}
</style>
