<template>
  <div class="tenant">
    <div class="content">
      <byTable
        :source="sourceList.data"
        :pagination="sourceList.pagination"
        :config="config"
        :loading="loading"
        :selectConfig="selectConfig"
        highlight-current-row
        :action-list="[
          {
            text: '新增调仓',
            action: () => openModal(),
          },
        ]"
        @get-list="getList"
      >
      </byTable>
    </div>

    <el-dialog
      title="新增调仓"
      v-if="dialogVisible"
      v-model="dialogVisible"
      width="1000"
      v-loading="loadingDialog"
    >
      <byForm
        :formConfig="formConfig"
        :formOption="formOption"
        v-model="formData.data"
        :rules="rules"
        ref="submit"
      >
        <template #details>
          <div style="width: 100%">
            <el-button type="primary" @click="openProduct = true"
              >添加明细</el-button
            >
            <el-table
              :data="formData.data.stockTransferDetailsList"
              style="width: 100%; margin-top: 16px"
            >
              <el-table-column
                prop="productCode"
                label="物品编码"
                width="140"
              />
              <el-table-column
                prop="productName"
                label="物品名称"
                min-width="220"
              />
              <el-table-column
                prop="productSpec"
                label="规格型号"
                min-width="220"
              />
              <!-- <el-table-column prop="productUnit" label="单位" width="100" /> -->
              <el-table-column label="调仓数量" width="160">
                <template #default="{ row, $index }">
                  <div style="width: 100%">
                    <el-form-item
                      :prop="
                        'stockTransferDetailsList.' + $index + '.outQuantity'
                      "
                      :rules="rules.outQuantity"
                      :inline-message="true"
                    >
                      <el-input-number
                        v-model="row.outQuantity"
                        placeholder="请输入数量"
                        style="width: 100%"
                        :precision="0"
                        :controls="false"
                        :min="0"
                      />
                    </el-form-item>
                  </div>
                </template>
              </el-table-column>
              <el-table-column
                align="center"
                label="操作"
                width="80"
                fixed="right"
              >
                <template #default="{ row, $index }">
                  <el-button type="primary" link @click="handleDelete($index)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </div>
        </template>
      </byForm>
      <template #footer>
        <el-button @click="dialogVisible = false" size="large">取 消</el-button>
        <el-button type="primary" @click="submitForm()" size="large"
          >确 定</el-button
        >
      </template>
    </el-dialog>

    <el-dialog
      v-model="openProduct"
      title="选择商品"
      width="70%"
      append-to-body
    >
      <SelectGoods
        @cancel="openProduct = false"
        @pushGoods="pushGoods"
      ></SelectGoods>
    </el-dialog>
  </div>
</template>

<script setup>
import { computed, ref } from "vue";
import byTable from "@/components/byTable/index";
import byForm from "@/components/byForm/index";
import { ElMessage } from "element-plus";
import SelectGoods from "@/components/product/SelectGoods";

const { proxy } = getCurrentInstance();
const warehouseList = ref([]);
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    keyword: "",
    outWarehouseId: "",
    inWarehouseId: "",
    type: "3,11",
  },
});
const loading = ref(false);
const selectConfig = computed(() => {
  return [
    {
      label: "调出仓库",
      prop: "outWarehouseId",
      data: warehouseList.value,
    },
    {
      label: "调入仓库",
      prop: "inWarehouseId",
      data: warehouseList.value,
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "单号",
        prop: "code",
        width: 160,
      },
    },
    {
      attrs: {
        label: "调出仓库",
        prop: "outWarehouseName",
        width: 220,
      },
      // render(type) {
      //   return proxy.dictValueLabel(type, warehouseList.value);
      // },
    },
    {
      attrs: {
        label: "调入仓库",
        prop: "inWarehouseName",
        width: 220,
      },
      // render(type) {
      //   return proxy.dictValueLabel(type, warehouseList.value);
      // },
    },
    {
      attrs: {
        label: "物品编码",
        prop: "productCode",
        width: 160,
      },
    },
    {
      attrs: {
        label: "物品名称",
        prop: "productName",
        "min-width": 220,
      },
    },
    {
      attrs: {
        label: "规格型号",
        prop: "productSpec",
        width: 160,
      },
    },
    {
      attrs: {
        label: "单位",
        prop: "productUnit",
        width: 120,
      },
    },
    {
      attrs: {
        label: "调仓数量",
        prop: "outQuantity",
        width: 140,
      },
    },
    {
      attrs: {
        label: "操作人",
        prop: "opUserName",
        width: 140,
      },
    },
    {
      attrs: {
        label: "操作时间",
        prop: "createTime",
        width: 160,
      },
    },
  ];
});
const getDict = () => {
  proxy.post("/warehouse/page", { pageNum: 1, pageSize: 999 }).then((res) => {
    if (res.rows && res.rows.length > 0) {
      warehouseList.value = res.rows.map((item) => {
        return {
          label: item.name,
          value: item.id,
        };
      });
    }
  });
};
const getList = async (req) => {
  sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  loading.value = true;
  proxy
    .post("stockTransferDetails/page", sourceList.value.pagination)
    .then((res) => {
      sourceList.value.data = res.rows;
      sourceList.value.pagination.total = res.total;
      setTimeout(() => {
        loading.value = false;
      }, 200);
    });
};
getDict();
getList();
const dialogVisible = ref(false);
const loadingDialog = ref(false);
const submit = ref(null);
const openProduct = ref(false);
const formOption = reactive({
  inline: true,
  labelWidth: 100,
  itemWidth: 100,
  rules: [],
});
const formData = reactive({
  data: {},
});
const formConfig = computed(() => {
  return [
    {
      label: "调仓信息",
    },
    {
      type: "select",
      prop: "outWarehouseId",
      label: "调出仓库",
      required: true,
      data: warehouseList.value,
      itemWidth: 30,
    },
    {
      type: "select",
      prop: "inWarehouseId",
      label: "调入仓库",
      required: true,
      data: warehouseList.value,
      itemWidth: 30,
    },
    {
      type: "input",
      prop: "remark",
      label: "调拨说明",
      itemType: "textarea",
    },
    {
      type: "slot",
      slotName: "details",
      label: "调仓明细",
    },
  ];
});
const rules = ref({
  outWarehouseId: [
    { required: true, message: "请选择调出仓库", trigger: "change" },
  ],
  inWarehouseId: [
    { required: true, message: "请选择调入仓库", trigger: "change" },
  ],
  outQuantity: [{ required: true, message: "请输入调仓数量", trigger: "blur" }],
});
const openModal = () => {
  formData.data = {
    type: "1",
    stockTransferDetailsList: [],
  };
  loadingDialog.value = false;
  dialogVisible.value = true;
};
const pushGoods = (goods) => {
  if (goods && goods.length > 0) {
    formData.data.stockTransferDetailsList =
      formData.data.stockTransferDetailsList.concat(
        goods.map((item) => {
          return {
            productCode: item.code,
            productId: item.id,
            productName: item.name,
            productSpec: item.spec,
            productUnit: item.unit,
            outQuantity: undefined,
          };
        })
      );
    ElMessage({
      message: "添加成功！",
      type: "success",
    });
    openProduct.value = false;
  } else {
    ElMessage("请选择至少一件物品");
  }
};
const submitForm = () => {
  submit.value.handleSubmit(() => {
    if (formData.data.outWarehouseId === formData.data.inWarehouseId)
      return ElMessage({
        message: "调出仓库和调入仓库不可一致！",
        type: "info",
      });

    if (!formData.data.stockTransferDetailsList.length > 0)
      return ElMessage({
        message: "请添加调仓明细",
        type: "info",
      });
    loadingDialog.value = true;
    proxy.post("/stockTransfer/addSt", formData.data).then(
      () => {
        ElMessage({
          message: "提交成功",
          type: "success",
        });
        dialogVisible.value = false;
        getList();
      },
      (err) => {
        console.log(err);
        loadingDialog.value = false;
      }
    );
  });
};
const handleDelete = (index) => {
  formData.data.stockTransferDetailsList.splice(index, 1);
};
</script>

<style lang="scss" scoped>
.tenant {
  padding: 20px;
}
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}
</style>
