<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" label-width="100px">
            <el-form-item label="采购单编号" prop="docCode">
              <el-input v-model="queryParams.docCode" placeholder="请输入采购单编号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="设备统称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入设备统称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="到货日期" prop="arrivalDate">
              <el-date-picker clearable v-model="queryParams.arrivalDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择到货日期" />
            </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="['cailiaoshebei:purchaseDoc:add']">新增</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>
      <el-table v-loading="loading" :data="purchaseDocList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="采购单编号" align="center" prop="docCode" width="90" />
        <el-table-column label="批次号" align="center" prop="mrpBaseId">
          <template #default="scope">
            {{ batchOptions.find((item) => item.id == scope.row.mrpBaseId)?.planCode }}
          </template>
        </el-table-column>
        <el-table-column label="供应商" align="center" prop="supplier" />
        <el-table-column label="设备统称" align="center" prop="name" />
        <el-table-column label="到货日期" align="center" prop="arrivalDate" width="120">
          <template #default="scope">
            <span>{{ parseTime(scope.row.arrivalDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="收货地址" align="center" prop="receivingAddress" />
        <el-table-column label="联系人" align="center" prop="contacts" />
        <el-table-column label="物流单号" align="center" prop="remark" width="150">
          <template #default="scope">
            <el-button link type="primary" icon="View" @click="handleView(scope.row)" v-hasPermi="['out:monthPlan:remove']">查看物流单</el-button>
          </template>
        </el-table-column>
        <el-table-column label="采购经办人" align="center" prop="purchasingAgent" width="90" />
        <el-table-column label="日期" align="center" prop="preparedDate" width="120">
          <template #default="scope">
            <span>{{ parseTime(scope.row.preparedDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="供应商返回" align="center" prop="feedbackUrl" width="130">
          <template #default="scope">
            <el-link :href="scope.row.feedbackUrl" target="_blank" type="primary" v-if="scope.row.feedbackUrl">回单</el-link>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="wf_business_status" :value="scope.row.status"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="160">
          <template #default="scope">
            <el-button
              link
              type="primary"
              v-if="scope.row.status == 'draft' || scope.row.status == 'back'"
              icon="Finished"
              @click="handleAudit(scope.row)"
              v-hasPermi="['cailiaoshebei:purchaseDoc:edit']"
            >
              审核</el-button
            >
            <el-button
              link
              type="primary"
              v-if="scope.row.status != 'draft'"
              icon="view"
              @click="handleViewDetail(scope.row)"
              v-hasPermi="['cailiaoshebei:purchaseDoc:edit']"
            >
              查看</el-button
            >
            <el-button
              link
              type="primary"
              v-if="!scope.row.feedbackUrl && scope.row.status == 'finish'"
              icon="Upload"
              @click="handleUpload(scope.row)"
              v-hasPermi="['cailiaoshebei:purchaseDoc:edit']"
              >上传</el-button
            >
            <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-if="scope.row.status == 'draft'"
              v-hasPermi="['cailiaoshebei:purchaseDoc:edit']"
              >修改</el-button
            >
            <el-button
              link
              type="primary"
              v-if="scope.row.status == 'finish' && scope.row.feedbackUrl"
              icon="Share"
              @click="handleShare(scope.row)"
              v-hasPermi="['cailiaoshebei:purchaseDoc:remove']"
              >物流单分享</el-button
            >
          </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 :title="dialog.title" v-model="dialog.visible" width="800px" append-to-body>
      <el-form ref="purchaseDocFormRef" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="20">
          <el-col :span="12" :offset="0"
            ><el-form-item label="采购单编号" prop="docCode"> <el-input v-model="form.docCode" placeholder="请输入采购单编号" /> </el-form-item
          ></el-col>
          <el-col :span="12" :offset="0"
            ><el-form-item label="供应商" prop="supplier">
              <el-select v-model="form.supplier" value-key="id" placeholder="请选择供应商" clearable filterable @change="">
                <el-option v-for="item in supplierOptions" :key="item.id" :label="item.name" :value="item.name"> </el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12" :offset="0"
            ><el-form-item label="需求批次号" prop="mrpBaseId">
              <el-select v-model="form.mrpBaseId" value-key="id" placeholder="请选择需求批次号" filterable @change="getPlanList">
                <el-option v-for="item in batchOptions" :key="item.id" :label="item.planCode" :value="item.id"> </el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="需求计划" prop="planId">
              <el-select v-model="form.planId" value-key="id" placeholder="请选择需求计划" multiple filterable :disabled="!form.mrpBaseId">
                <el-option v-for="item in planList" :key="item.id" :label="item.name" :value="item.id"> </el-option>
              </el-select> </el-form-item
          ></el-col>
          <el-col :span="12" :offset="0"
            ><el-form-item label="事由" prop="reason"> <el-input v-model="form.reason" placeholder="请输入事由" /> </el-form-item
          ></el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="设备统称" prop="name"> <el-input v-model="form.name" placeholder="请输入设备统称" /> </el-form-item
          ></el-col>
          <el-col :span="12" :offset="0"
            ><el-form-item label="到货日期" prop="arrivalDate">
              <el-date-picker clearable v-model="form.arrivalDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择到货日期">
              </el-date-picker> </el-form-item
          ></el-col>
          <el-col :span="12" :offset="0"
            ><el-form-item label="负责人联系方式" prop="designDirectorTel">
              <el-input v-model="form.designDirectorTel" placeholder="请输入设计负责人联系方式" /> </el-form-item
          ></el-col>
          <el-col :span="12" :offset="0"
            ><el-form-item label="现场联系方式" prop="technicalDirectorTel">
              <el-input v-model="form.technicalDirectorTel" placeholder="请输入现场技术负责人联系方式" /> </el-form-item
          ></el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="收货地址" prop="receivingAddress">
              <el-input v-model="form.receivingAddress" placeholder="请输入收货地址" /> </el-form-item
          ></el-col>
          <el-col :span="12" :offset="0">
            <el-form-item label="联系人" prop="contacts"> <el-input v-model="form.contacts" placeholder="请输入联系人" /> </el-form-item
          ></el-col>
          <el-col :span="12" :offset="0"
            ><el-form-item label="项目负责人" prop="projectDirector">
              <el-input v-model="form.projectDirector" placeholder="请输入项目负责人" /> </el-form-item
          ></el-col>
          <el-col :span="12" :offset="0"
            ><el-form-item label="采购经办人" prop="purchasingAgent">
              <el-input v-model="form.purchasingAgent" placeholder="请输入采购经办人" /> </el-form-item
          ></el-col>
          <!-- <el-col :span="12" :offset="0"
            ><el-form-item label="日期" prop="preparedDate">
              <el-date-picker clearable v-model="form.preparedDate" type="date" value-format="YYYY-MM-DD" placeholder="请选择日期">
              </el-date-picker> </el-form-item
          ></el-col> -->
        </el-row>
      </el-form>
      <el-table v-loading="loading" :data="selectPlanList" v-if="form.id">
        <el-table-column label="物资名称" align="center" prop="name" />

        <el-table-column label="质量标准" align="center" prop="qs" />
        <el-table-column label="规格型号" align="center" prop="specification" />
        <el-table-column label="计量单位" align="center" prop="unit" width="80" />
        <el-table-column label="需求数量" align="center" prop="demandQuantity" v-if="form.docType == 2">
          <template #default="scope">
            <el-input v-model="scope.row.demandQuantity" placeholder="请输入" type="number" />
          </template>
        </el-table-column>
        <el-table-column label="需求数量" align="center" prop="demandQuantity" v-else />
        <!-- <el-table-column label="需求到货时间" align="center" prop="arrivalTime" width="250" /> -->
        <el-table-column label="备注" align="center" prop="remark" />
      </el-table>
      <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>
    <el-dialog title="上传文件" v-model="uploadDialogVisible" width="30%">
      <file-upload v-model="feedbackUrl" :file-type="['pdf']" :onUploadSuccess="handleSuccess" />
      <template #footer>
        <span>
          <el-button @click="uploadDialogVisible = false">取消</el-button>
          <el-button type="primary" @click="uploadFile">确定</el-button>
        </span>
      </template>
    </el-dialog>
    <!-- 查看文件列表 -->
    <el-dialog title="文件列表" v-model="viewVisible" width="45%">
      <el-table v-if="fileList.length > 0" :data="fileList" style="width: 100%" border>
        <el-table-column label="单号" align="center" prop="ltn" />
        <el-table-column label="数量" align="center" prop="num" />
        <el-table-column label="物资名称" align="center" prop="name" />
      </el-table>
      <div v-else class="empty-list text-center">暂无文件</div>
      <template #footer>
        <span>
          <el-button type="primary" @click="viewVisible = false">关闭</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="PurchaseDoc" lang="ts">
import { getBatch, listBatch } from '@/api/materials/batchPlan';
import { listPurchaseDoc, getPurchaseDoc, listLink, addPurchaseDoc, updatePurchaseDoc } from '@/api/materials/purchaseDoc';
import { PurchaseDocVO, PurchaseDocQuery, PurchaseDocForm } from '@/api/materials/purchaseDoc/types';
import { listContractor } from '@/api/project/contractor';
import { useUserStoreHook } from '@/store/modules/user';
import { getToken } from '@/utils/auth';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const route = useRoute();
const router = useRouter();
const { supply, wf_business_status } = toRefs<any>(proxy?.useDict('supply', 'wf_business_status'));
// 获取用户 store
const userStore = useUserStoreHook();
// 从 store 中获取项目列表和当前选中的项目
const currentProject = computed(() => userStore.selectedProject);
const uploadDialogVisible = ref(false);
const purchaseDocList = ref<PurchaseDocVO[]>([]);
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 feedbackUrl = ref('');
const queryFormRef = ref<ElFormInstance>();
const purchaseDocFormRef = ref<ElFormInstance>();
const IP = 'http://192.168.110.151:7788';

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
const batchOptions = ref([]);
const supplierOptions = ref([]);

const planList = ref([]);
const initFormData: any = {
  id: undefined,
  projectId: currentProject.value?.id,

  docCode: undefined,
  supplier: undefined,
  reason: undefined,
  name: undefined,
  arrivalDate: undefined,
  designDirectorTel: undefined,
  technicalDirectorTel: undefined,
  receivingAddress: undefined,
  contacts: undefined,
  associationList: [],
  projectDirector: undefined,
  purchasingAgent: undefined,
  preparedDate: undefined,
  feedbackUrl: undefined,
  signingUnit: undefined,
  signingPerson: undefined,
  signingDate: undefined,
  status: undefined
};
const data = reactive({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    projectId: currentProject.value?.id,

    docCode: undefined,
    supplier: undefined,
    reason: undefined,
    name: undefined,
    arrivalDate: undefined,
    designDirectorTel: undefined,
    technicalDirectorTel: undefined,
    receivingAddress: undefined,
    contacts: undefined,
    projectDirector: undefined,
    purchasingAgent: undefined,
    preparedDate: undefined,
    feedbackUrl: undefined,
    signingUnit: undefined,
    signingPerson: undefined,
    signingDate: undefined,
    status: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '主键ID不能为空', trigger: 'blur' }],
    // 电话号码验证
    technicalDirectorTel: [
      { required: true, message: '请输入电话', trigger: 'blur' },
      { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
    ],
    designDirectorTel: [
      { required: true, message: '请输入电话', trigger: 'blur' },
      { pattern: /^1[3-9]\d{9}$/, message: '请输入正确的手机号码', trigger: 'blur' }
    ]
  }
});

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

/** 查询物资-采购联系单列表 */
const getList = async () => {
  loading.value = true;
  const res = await listPurchaseDoc(queryParams.value);
  purchaseDocList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  purchaseDocFormRef.value?.resetFields();
  form.value.projectId = currentProject.value?.id;
};

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

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

const fileList = ref([]);
const viewVisible = ref(false);
const handleView = async (row?: any) => {
  const res = await listLink({
    docId: row.id
  });
  fileList.value = res.rows;

  viewVisible.value = true;
};

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

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = '添加物资-采购联系单';
};

/** 修改按钮操作 */
const handleUpdate = async (row?: PurchaseDocVO) => {
  reset();
  const _id = row?.id || ids.value[0];
  const res = await getPurchaseDoc(_id);
  Object.assign(form.value, res.data);
  getPlanList();
  form.value.planId = form.value.associationList?.map((item: any) => item.planId);

  dialog.visible = true;
  dialog.title = '修改物资-采购联系单';
};

const selectPlanList = computed(() => {
  if (!form.value.planId) return [];
  const result = planList.value.filter((item) => form.value.planId.includes(item.id));
  return result;
});

/** 提交按钮 */
const submitForm = () => {
  purchaseDocFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      form.value.associationList = form.value.planId?.map((item: any) => ({
        planId: item
      }));

      if (form.value.id) {
        await updatePurchaseDoc(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addPurchaseDoc(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

const getPlanList = async () => {
  form.value.planId = '';
  const res = await getBatch({
    pageNum: 1,
    pageSize: 10,
    projectId: currentProject.value?.id,
    mrpBaseId: form.value.mrpBaseId
  });
  planList.value = res.rows;
};

const getBatchList = async () => {
  const res = await listBatch({
    pageNum: 1,
    pageSize: 10,
    planCode: undefined,
    projectId: currentProject.value?.id
  });
  batchOptions.value = res.rows.filter((item) => item.status == 'finish');
};

const getSupplierList = async () => {
  const res = await listContractor({
    projectId: currentProject.value?.id,
    pageNum: 1,
    contractorType: 4,
    pageSize: 10000
  });
  supplierOptions.value = res.rows;
};

/** 分享按钮操作 */
const handleShare = async (row?: PurchaseDocVO) => {
  const textarea = document.createElement('textarea');
  const data = JSON.stringify({
    docId: row.id,
    mrpBaseId: row.mrpBaseId,
    projectId: currentProject.value?.id,
    token: 'Bearer ' + getToken()
  });
  // 获取当前域名地址
  console.log(location);
  textarea.value = IP + '/materials/purchaseDoc/uploadCode?data=' + data;
  // textarea.value = location.host + '/materials/purchaseDoc/uploadCode?data=' + data;
  textarea.style.position = 'fixed';
  textarea.style.opacity = '0';
  document.body.appendChild(textarea);
  textarea.select();
  const success = document.execCommand('copy');
  document.body.removeChild(textarea);
  ElMessage[success ? 'success' : 'error'](success ? '复制成功！' : '复制失败');
};

/** 导出按钮操作 */
const handleUpload = (row?: PurchaseDocVO) => {
  form.value.feedbackUrl = '';
  form.value.id = row.id;
  uploadDialogVisible.value = true;
};

const uploadFile = async () => {
  if (!feedbackUrl.value) {
    proxy?.$modal.msgError('请上传文件');
    return;
  }
  await updatePurchaseDoc(form.value).finally(() => (buttonLoading.value = false));
  proxy?.$modal.msgSuccess('操作成功');
  uploadDialogVisible.value = false;

  await getList();
};

const handleSuccess = (list, res: any) => {
  form.value.feedbackUrl = res.data.url;
};

/** 审核按钮操作 */
const handleAudit = async (row?: PurchaseDocVO) => {
  proxy?.$tab.closePage(route);
  proxy?.$tab.openPage('/materials/purchaseDoc/indexEdit', '审核采购联系单', {
    id: row.id,
    type: 'update'
  });
};
/** 审核按钮操作 */
const handleViewDetail = async (row?: PurchaseDocVO) => {
  proxy?.$tab.closePage(route);
  proxy?.$tab.openPage('/materials/purchaseDoc/indexEdit', '审核采购联系单', {
    id: row.id,
    type: 'view'
  });
};

onMounted(() => {
  getList();
  getSupplierList();
  getBatchList();
});

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

onUnmounted(() => {
  listeningProject();
});
</script>
