<template>
  <div style="width: 100%; padding: 0px 15px">
    <el-form :model="formData.data" :rules="rules" ref="formDom" label-position="top">
      <div class="_t">申购信息</div>
      <el-row :gutter="10">
        <el-col :span="6">
          <el-form-item label="申购部门" prop="deptName">
            <el-input v-model="formData.data.deptName" placeholder="请输入"> </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="申购人" prop="subcribeName">
            <el-input v-model="formData.data.subcribeName" placeholder="请输入"> </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="申购时间" prop="subcribeTime">
            <el-date-picker v-model="formData.data.subcribeTime" type="datetime" placeholder="请选择" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="申购说明" prop="subcribeContent">
        <el-input v-model="formData.data.subcribeContent" placeholder="请输入" type="textarea"> </el-input>
      </el-form-item>
      <div class="_t">申购明细</div>
      <el-form-item>
        <el-button type="primary" @click="openProduct = true" style="margin: 10px 0"> 添加货品 </el-button>
        <el-table :data="formData.data.subscribeDetailList">
          <el-table-column prop="goodType" label="货品类型" :formatter="(row) => (row.goodType == 1 ? '产品' : '物料')" />
          <el-table-column prop="code" label="货品编码" />
          <el-table-column prop="name" label="货品名称" min-width="150" />
          <el-table-column prop="spec" label="规格型号" />
          <el-table-column prop="unit" 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 onmousewheel="return false;" v-model="row.count" :precision="4" :controls="false" :min="1" />
              </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>
      </el-form-item>
    </el-form>
    <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 SelectGoods from "@/components/product/SelectGoods";
import { ElMessage, ElMessageBox } from "element-plus";
import useUserStore from "@/store/modules/user";

const { proxy } = getCurrentInstance();

let formData = reactive({
  data: {
    subcribeTime: "",
    subscribeDetailList: [],
  },
});
let rules = ref({
  deptName: [{ required: true, message: "请输入申购部门", trigger: "blur" }],
  subcribeName: [{ required: true, message: "请输入申购人名称", trigger: "blur" }],
  subcribeTime: [{ required: true, message: "请选择申购时间", trigger: "change" }],
  // subcribeContent: [
  //   { required: true, message: "请输入申购事由", trigger: "blur" },
  // ],
  count: [{ required: true, message: "请输入申购数量", trigger: "blur" }],
  // remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
});

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) => ({
    ...x,
    bussinessId: x.id,
    count: "",
    remark: "",
  }));
  formData.data.subscribeDetailList = formData.data.subscribeDetailList.concat(arr);
  openProduct.value = false;
  return ElMessage({
    message: "添加成功！",
    type: "success",
  });
};

const formDom = ref(null);
const handleSubmit = async () => {
  const vaild = await formDom.value.validate();
  if (vaild) {
    if (formData.data.subscribeDetailList.length > 0) {
      return true;
    }
    ElMessage({
      message: "请添加申购明细！",
      type: "info",
    });
    return false;
  }

  return false;
};

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>
._t {
  margin-bottom: 5px;
  font-size: 14px;
}
</style>
