<template>
  <div style="width: 100%; padding: 0px 15px">
    <byForm
      :formConfig="formConfig"
      :formOption="formOption"
      v-model="formData.data"
      :rules="rules"
      ref="formDom"
    >
      <template #details>
        <div style="width: 100%">
          <el-button
            type="primary"
            @click="openProduct = true"
            style="margin-bottom: 10px"
          >
            添加物品
          </el-button>
          <el-table :data="formData.data.subscribeDetailList">
            <el-table-column
              prop="goodType"
              label="物品类型"
              :formatter="(row) => (row.goodType == 1 ? '产品' : '物料')"
            />
            <el-table-column prop="productCode" label="物品编码" />
            <el-table-column prop="productName" label="物品名称" />
            <el-table-column prop="productSpec" label="规格型号" />
            <el-table-column prop="productUnit" label="单位" />
            <el-table-column prop="count" label="申购数量" min-width="150">
              <template #default="{ row, $index }">
                <el-form-item
                  :prop="'subscribeDetailList.' + $index + '.count'"
                  :rules="rules.count"
                  :inline-message="true"
                >
                  <el-input-number
                    v-model="row.count"
                    :precision="4"
                    :controls="false"
                    :min="0"
                  />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="remark" label="备注" min-width="150">
              <template #default="{ row, $index }">
                <el-form-item
                  :prop="'subscribeDetailList.' + $index + '.remark'"
                  :rules="rules.remark"
                  :inline-message="true"
                >
                  <el-input v-model="row.remark" placeholder="请输入" />
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column prop="zip" label="操作" width="100">
              <template #default="{ $index }">
                <el-button type="primary" link @click="handleRemove($index)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </div>
      </template>
    </byForm>
    <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 byForm from "@/components/byForm/index";
import { ElMessage, ElMessageBox } from "element-plus";
import SelectGoods from "@/components/product/SelectGoods";
import useUserStore from "@/store/modules/user";
const { proxy } = getCurrentInstance();

let formData = reactive({
  data: {
    subscribeDetailList: [],
  },
});
let rules = ref({
  receiptWarehouseId: [
    { required: true, message: "请选择收获仓库", trigger: "change" },
  ],
  planArrivalTime: [
    { required: true, message: "请选择要求到货时间", trigger: "change" },
  ],
  count: [{ required: true, message: "请输入申购数量", trigger: "blur" }],
  remark: [{ required: true, message: "请输入申购备注", trigger: "blur" }],
});
const formOption = reactive({
  inline: true,
  labelWidth: 100,
  itemWidth: 100,
});
const formConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "deptName",
      label: "申购部门",
      itemWidth: 25,
      disabled: true,
      style: {
        "margin-right": "10px",
      },
    },
    {
      type: "input",
      prop: "subcribeName",
      label: "申购人",
      itemWidth: 25,
      disabled: true,
      style: {
        "margin-right": "10px",
      },
    },
    {
      type: "date",
      prop: "subcribeTime",
      label: "申购时间",
      itemWidth: 26,
      disabled: true,
      style: {
        "margin-right": "10px",
      },
    },
    {
      type: "select",
      prop: "receiptWarehouseId",
      label: "收获仓库",
      itemWidth: 25,
      isLoad: {
        url: "/warehouse/page",
        req: {
          pageNum: 1,
          pageSize: 9999,
        },
        labelKey: "name",
        labelVal: "id",
        method: "post",
        resUrl: "rows",
      },
    },
    {
      type: "date",
      prop: "planArrivalTime",
      label: "要求到货时间",
      itemWidth: 25,
      format: "YYYY-MM-DD",
      style: {
        "margin-left": "10px",
      },
    },
    {
      type: "input",
      itemType: "textarea",
      prop: "subcribeContent",
      label: "申购说明",
    },
    {
      type: "slot",
      slotName: "details",
      label: "申购明细",
    },
  ];
});
const formDom = ref(null);
const handleSubmit = async () => {
  const vaild = await formDom.value.handleSubmit(() => {}); //拿到内部表单是否验证通过
  if (vaild) {
    if (formData.data.subscribeDetailList.length > 0) {
      const list = formData.data.subscribeDetailList;
      for (let i = 0; i < list.length; i++) {
        const e = list[i];
        if (e.count == 0) {
          ElMessage({
            message: "申购数量不能为0！",
            type: "info",
          });
          return false;
        }
      }
      return true;
    }
    ElMessage({
      message: "请添加申购明细！",
      type: "info",
    });
    return false;
  }
};
let openProduct = ref(false);
const handleRemove = (index) => {
  formData.data.subscribeDetailList.splice(index, 1);
  return ElMessage({
    message: "删除成功！",
    type: "success",
  });
};
const pushGoods = (goods) => {
  const arr = goods.map((x) => ({
    goodType: x.goodType,
    productCode: x.code,
    productName: x.name,
    productSpec: x.spec,
    productUnit: x.unit,
    count: 0,
    bussinessId: x.id,
    remark: "",
  }));
  formData.data.subscribeDetailList =
    formData.data.subscribeDetailList.concat(arr);
  return ElMessage({
    message: "添加成功！",
    type: "success",
  });
};

// 接收父组件的传值
const props = defineProps({
  queryData: String,
});

// 获取用户信息并赋默认值
const userInfo = useUserStore().user;
onMounted(() => {
  formData.data.subcribeTime = proxy.parseTime(new Date());
  formData.data.deptName = userInfo.dept.deptName;
  formData.data.subcribeName = userInfo.nickName;
});
// 向父组件暴露
defineExpose({
  submitData: formData.data,
  handleSubmit,
});
</script>

<style lang="scss" scoped>
</style>