<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
            <el-form-item label="表单编号" prop="formCode">
              <el-input v-model="queryParams.formCode" placeholder="请输入表单编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="工程名称" prop="projectName">
              <el-input v-model="queryParams.projectName" placeholder="请输入工程名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="材料名称" prop="materialName">
              <el-input v-model="queryParams.materialName" placeholder="请输入设备材料名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="合同名称" prop="contractName">
              <el-input v-model="queryParams.contractName" placeholder="请输入合同名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="订货单位" prop="orderingUnit">
              <el-input v-model="queryParams.orderingUnit" placeholder="请输入订货单位" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="供货单位" prop="supplierUnit">
              <el-input v-model="queryParams.supplierUnit" placeholder="请输入供货单位" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['materials:materialReceive:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="Delete"
              :disabled="multiple"
              @click="handleDelete()"
              v-hasPermi="['materials:materialReceive:remove']"
              >删除</el-button
            >
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="materialReceiveList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="表单编号" align="center" prop="formCode" />
        <el-table-column label="工程名称" align="center" prop="projectName" />
        <el-table-column label="设备材料名称" align="center" prop="materialName" />
        <el-table-column label="合同名称" align="center" prop="contractName" />
        <el-table-column label="订货单位" align="center" prop="orderingUnit" />
        <el-table-column label="供货单位" align="center" prop="supplierUnit" />
        <el-table-column label="设备材料入库/移交" align="center" prop="storageType">
          <template #default="scope">
            <dict-tag :options="storage_type" :value="scope.row.storageType ? scope.row.storageType.split(',') : []" />
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="查看" placement="top">
              <el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['materials:materialReceive:query']"></el-button>
            </el-tooltip>
            <!-- <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['materials:materialReceive:edit']"></el-button>
            </el-tooltip> -->
            <el-tooltip content="删除" placement="top">
              <el-button
                link
                type="primary"
                icon="Delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['materials:materialReceive:remove']"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
    </el-card>
    <!-- 添加或修改物料接收单对话框 -->
    <el-dialog draggable :title="dialog.title" v-model="dialog.visible" width="800px" append-to-body>
      <el-form ref="materialReceiveFormRef" :model="form" :rules="rules" label-width="110px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="材料来源" prop="materialSource">
              <el-select v-model="form.materialSource" filterable placeholder="请选择材料来源" style="width: 100%">
                <el-option label="甲供材料" value="1"></el-option>
                <el-option label="已供材料" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="表单编号" prop="formCode">
              <el-input v-model="form.formCode" placeholder="请输入表单编号" />
            </el-form-item>
          </el-col>
          <el-col :span="12"
            ><el-form-item label="采购单编号" prop="docId"
              ><el-select @change="handleSelect" v-model="form.docId" filterable placeholder="请选择采购单" style="width: 100%">
                <el-option v-for="item in purchaseDocList" :key="item.id" :label="item.docCode" :value="item.id"></el-option> </el-select
            ></el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供货单位" prop="supplierUnit">
              <el-input disabled v-model="form.supplierUnit" placeholder="请输入供货单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="订货单位" prop="orderingUnit">
              <el-input v-model="form.orderingUnit" placeholder="请输入订货单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12"
            ><el-form-item label="工程名称" prop="projectName">
              <el-input v-model="form.projectName" placeholder="请输入工程名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合同名称" prop="contractName">
              <el-input v-model="form.contractName" placeholder="请输入合同名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="缺陷情况" prop="defectDescription">
              <el-input v-model="form.defectDescription" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <div class="detail">
              <div class="detail-header">
                <span>数量验收</span>
                <!-- <el-button type="primary" link @click="addItem" icon="Plus">添加数量验收</el-button> -->
              </div>
              <div v-for="(item, index) in form.itemList" :key="index" class="detail-item">
                <el-row>
                  <el-col :span="12">
                    <el-form-item label="名称" :prop="`itemList.${index}.name`" :rules="{ required: true, message: '名称不能为空', trigger: 'blur' }">
                      <el-input v-model="item.name" placeholder="请输入名称" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item
                      label="规格"
                      :prop="`itemList.${index}.specification`"
                      :rules="{ required: true, message: '规格不能为空', trigger: 'blur' }"
                    >
                      <el-input v-model="item.specification" placeholder="请输入规格" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="单位" :prop="`itemList.${index}.unit`" :rules="{ required: true, message: '单位不能为空', trigger: 'blur' }">
                      <el-input v-model="item.unit" placeholder="请输入单位" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item
                      label="数量"
                      :prop="`itemList.${index}.quantity`"
                      :rules="{ required: true, message: '数量不能为空', trigger: 'blur' }"
                    >
                      <el-input type="number" v-model="item.quantity" placeholder="请输入数量" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item
                      label="验收"
                      :prop="`itemList.${index}.acceptedQuantity`"
                      :rules="{ required: true, message: '验收数量不能为空', trigger: 'blur' }"
                    >
                      <el-input type="number" v-model="item.acceptedQuantity" placeholder="请输入验收" />
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item
                      label="缺件"
                      :prop="`itemList.${index}.shortageQuantity`"
                      :rules="{ required: true, message: '缺件数量不能为空', trigger: 'blur' }"
                    >
                      <el-input type="number" v-model="item.shortageQuantity" placeholder="自动计算（数量-验收数量）" readonly />
                      <span class="tips">*自动计算（数量-验收数量）</span>
                    </el-form-item>
                  </el-col>
                  <el-col :span="12">
                    <el-form-item label="备注" prop="remark">
                      <el-input v-model="item.remark" placeholder="请输入备注" />
                    </el-form-item>
                  </el-col>
                  <!-- <el-col :span="12" v-if="form.itemList.length > 1">
                    <div class="item-actions">
                      <el-button type="danger" link @click="removeItem(index)" icon="Delete">删除</el-button>
                    </div>
                  </el-col> -->
                </el-row>
              </div>
            </div>
          </el-col>
          <el-col :span="12">
            <el-form-item label="合格证文件" prop="certCountFileId">
              <file-upload :isShowTip="false" v-model="form.certCountFileId" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出厂报告文件" prop="reportCountFileId">
              <file-upload :isShowTip="false" v-model="form.reportCountFileId" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="技术资料文件" prop="techDocCountFileId">
              <file-upload :isShowTip="false" v-model="form.techDocCountFileId" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="厂家资质文件" prop="licenseCountFileId">
              <file-upload :isShowTip="false" v-model="form.licenseCountFileId" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <span style="color: #ff0000ab; margin-bottom: 10px; display: block">注意：请上传doc/xls/ppt/txt/pdf/png/jpg/jpeg/zip格式文件</span>
          </el-col>
          <el-col :span="24">
            <el-form-item label="设备材料入库/移交" prop="storageType">
              <el-checkbox-group v-model="form.storageType">
                <el-checkbox v-for="dict in storage_type" :key="dict.value" :label="dict.value">
                  {{ dict.label }}
                </el-checkbox>
              </el-checkbox-group>
            </el-form-item> </el-col
          ><el-col :span="24"
            ><el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
    <wordllReceive ref="wordllReceiveRef"></wordllReceive>
  </div>
</template>

<script setup name="MaterialReceive" lang="ts">
import {
  listMaterialReceive,
  getMaterialReceive,
  delMaterialReceive,
  addMaterialReceive,
  updateMaterialReceive
} from '@/api/materials/materialReceive';
import { MaterialReceiveVO, MaterialReceiveQuery, MaterialReceiveForm } from '@/api/materials/materialReceive/types';
import { useUserStoreHook } from '@/store/modules/user';
import wordllReceive from './word/index.vue';
import { listPurchaseDoc, purchaseDocPlanList } from '@/api/materials/purchaseDoc';
import { watch } from 'vue';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { storage_type } = toRefs<any>(proxy?.useDict('storage_type'));
// 获取用户 store
const userStore = useUserStoreHook();
// 从 store 中获取项目列表和当前选中的项目
const currentProject = computed(() => userStore.selectedProject);
const wordllReceiveRef = ref<InstanceType<typeof wordllReceive>>();
const materialReceiveList = ref<MaterialReceiveVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const materialReceiveFormRef = ref<ElFormInstance>();
const purchaseDocList = ref([]); //物资采购单
const purchaseMap = new Map();
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const getInitFormData = () => {
  return {
    id: undefined,
    projectId: currentProject.value?.id,
    materialSource: '1',
    formCode: undefined,
    projectName: undefined,
    materialName: undefined,
    contractName: undefined,
    orderingUnit: undefined,
    supplierUnit: undefined,
    defectDescription: undefined,
    certCount: undefined,
    certCountFileId: undefined,
    reportCount: undefined,
    reportCountFileId: undefined,
    techDocCount: undefined,
    techDocCountFileId: undefined,
    licenseCount: undefined,
    licenseCountFileId: undefined,
    storageType: [],
    remark: undefined,
    docId: undefined,
    docCode: undefined,
    itemList: [
      {
        name: undefined,
        specification: undefined,
        unit: undefined,
        quantity: undefined,
        acceptedQuantity: undefined,
        shortageQuantity: undefined,
        remark: undefined
      }
    ]
  };
};
const initFormData: MaterialReceiveForm = {};
const data = reactive({
  form: getInitFormData(),
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: currentProject.value?.id,
    materialSource: '1',
    formCode: undefined,
    projectName: undefined,
    materialName: undefined,
    contractName: undefined,
    orderingUnit: undefined,
    supplierUnit: undefined,
    params: {}
  },
  rules: {}
});

const { queryParams, form, rules } = toRefs(data);

/** 查询物料接收单列表 */
const getList = async () => {
  loading.value = true;
  const res = await listMaterialReceive(queryParams.value);
  materialReceiveList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = getInitFormData();
  materialReceiveFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: MaterialReceiveVO[]) => {
  ids.value = selection.map((item) => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加物料接收单';
  // 为初始条目添加监听
  if (form.value.itemList.length > 0) {
    watchItemChanges(0);
  }
};

/** 修改按钮操作 */
const handleUpdate = async (row?: MaterialReceiveVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getMaterialReceive(_id);
  Object.assign(form.value, res.data);
  if (form.value.storageType && form.value.storageType.length) {
    form.value.storageType = form.value.storageType.split(',');
  } else {
    form.value.storageType = [];
  }

  // 为每个条目添加监听
  form.value.itemList.forEach((_, index) => {
    watchItemChanges(index);
  });

  dialog.visible = true;
  dialog.title = '修改物料接收单';
};

/** 提交按钮 */
const submitForm = () => {
  materialReceiveFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      form.value.storageType = form.value.storageType.join(',');
      if (form.value.id) {
        await updateMaterialReceive(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addMaterialReceive(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: MaterialReceiveVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除物料接收单编号为"' + _ids + '"的数据项？').finally(() => (loading.value = false));
  await delMaterialReceive(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

// 添加数量验收条目
const addItem = () => {
  const newItem = {
    name: undefined,
    specification: undefined,
    unit: undefined,
    quantity: undefined,
    acceptedQuantity: undefined,
    shortageQuantity: undefined,
    remark: undefined
  };
  form.value.itemList.push(newItem);
  // 监听新条目数据变化
  watchItemChanges(form.value.itemList.length - 1);
};

// 监听条目数据变化，自动计算缺件数量
const watchItemChanges = (index: number) => {
  watch(
    () => [form.value.itemList[index].quantity, form.value.itemList[index].acceptedQuantity],
    ([quantity, acceptedQuantity]) => {
      // 确保数量和验收数量都是数字
      const qty = Number(quantity) || 0;
      const acceptedQty = Number(acceptedQuantity) || 0;
      // 计算缺件数量（数量 - 验收数量）
      form.value.itemList[index].shortageQuantity = qty - acceptedQty;
    },
    { immediate: true }
  );
};

// 删除数量验收条目
const removeItem = (index: number) => {
  if (form.value.itemList.length > 1) {
    form.value.itemList.splice(index, 1);
  } else {
    proxy?.$modal.msgWarning('至少需要保留一条数量验收记录');
  }
};

const handleView = (row) => {
  // 查看详情
  wordllReceiveRef.value?.openDialog(row);
};

/** 查询物资-采购联系单列表 */
const getlistPurchase = async () => {
  const res = await listPurchaseDoc({
    projectId: currentProject.value?.id,
    status: 'finish'
  });

  purchaseDocList.value = res.rows;
  if (purchaseDocList.value && purchaseDocList.value.length > 0) {
    purchaseDocList.value.forEach((item) => {
      purchaseMap.set(item.id, item);
    });
  }
};

// 通过采购单获取需求信息
const getdemandInfo = async (docId: string) => {
  let res = await purchaseDocPlanList(docId);
  if (res.code == 200) {
    // 需求表单赋值
    form.value.itemList = [];
    //  form.value.itemList 清空
    console.log(form.value.itemList);
    res.data.forEach((item, index) => {
      let obj = {
        quantity: item.demandQuantity,
        acceptedQuantity: 0,
        shortageQuantity: item.demandQuantity, // 初始化缺件数量为总数量
        planId: item.id,
        ...item
      };
      obj.id = null;
      form.value.itemList.push(obj);
      // 监听每个条目的变化
      watchItemChanges(form.value.itemList.length - 1);
    });
  }
};

const handleSelect = (val) => {
  // 选择设备
  let obj = purchaseMap.get(val);
  getdemandInfo(val);
  form.value.docCode = obj?.docCode || '';
  form.value.supplierUnit = obj?.supplier || '';
  form.value.materialName = obj?.name || '';
};

onMounted(() => {
  getList();
  getlistPurchase();
  // 为初始条目添加监听
  if (form.value.itemList.length > 0) {
    watchItemChanges(0);
  }
});

// 监听项目id刷新数据
const listeningProject = watch(
  () => currentProject.value?.id,
  (nid, oid) => {
    queryParams.value.projectId = nid;
    form.value.projectId = nid;
    getList();
  }
);

onUnmounted(() => {
  listeningProject();
});
</script>
<style scoped lang="scss">
.detail {
  border-bottom: 1px solid #ececec;
  border-top: 1px solid #ececec;
  margin: 10px 0;
  padding: 10px 0;

  &-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 15px;
    font-weight: bold;
    color: #1eaaff;
  }

  &-item {
    padding: 10px;
    margin-bottom: 15px;
    border-radius: 4px;
    background-color: #f8f9fa;
    position: relative;

    &:last-child {
      margin-bottom: 0;
    }
  }
}

.item-actions {
  display: flex;
  justify-content: flex-end;
  padding-top: 6px;
}

.tips {
  font-size: 12px;
  color: #666;
  margin-left: 8px;
}
</style>
