<template>
  <el-dialog v-model="dialogVisible" title="招标文件" width="500" draggable @close="closeDialog">
    <el-form ref="ruleFormRef" style="max-width: 600px" :model="form" :rules="rules" label-width="auto">
      <el-form-item label="中标单位" prop="winningBidder">
        <el-select v-model="form.winningBidder" filterable placeholder="请选择单位" style="width: 240px">
          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="招标文件" prop="name">
        <file-upload
          v-model="form.costEstimationFile"
          :fileSize="100"
          :auto-upload="false"
          uploadUrl="/tender/biddingPlan/uploadBiddingDocuments"
          method="put"
          ref="fileUploadRef"
          :limit="1"
          :data="{
            projectId: currentProject?.id,
            type: planType,
            fileType: '0',
            bidStatus: '0',
            id: row.id,
            winningBidderId: form.winningBidder
          }"
          showFileList
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="closeDialog()"> 取消 </el-button>
        <el-button type="primary" @click="submitForm()">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { getUnitList } from '@/api/tender/index';
import { useUserStoreHook } from '@/store/modules/user';
const userStore = useUserStoreHook();
const currentProject = computed(() => userStore.selectedProject);
const dialogVisible = ref(false);
const row = ref<any>();
const planType = ref<any>('');
const fileUploadRef = ref<any>();
const ruleForm = ref<any>();
const options = ref<any>([]);

const rules = ref({
  costEstimationFile: [{ required: true, message: '请上传招标文件', trigger: ['blur'] }]
});
const emit = defineEmits(['success']);
const form = ref({
  costEstimationFile: '',
  winningBidder: ''
});

const open = (rows: any, type: string) => {
  dialogVisible.value = true;
  console.log(rows, type);
  row.value = rows;
  planType.value = type;
  getUnitListData();
};
const closeDialog = () => {
  dialogVisible.value = false;
  form.value.winningBidder = '';
  emit('success');
};
const getUnitListData = async () => {
  let res = await getUnitList({
    projectId: currentProject.value?.id
  });
  if (res.code == 200) {
    options.value = res.data.map((item: any) => {
      return {
        label: item.supplierName,
        value: item.id
      };
    });
  }
  console.log(res);
};
const resetForm = () => {};
const submitForm = () => {
  if (!form.value.winningBidder) {
    ElMessage({
      message: '请选择中标单位',
      type: 'warning'
    });
    return;
  }
  fileUploadRef.value.submitUpload().then((res) => {
    if (res == 'noFile') {
      ElMessage({
        message: '请上传招标文件',
        type: 'warning'
      });
      return;
    }
    dialogVisible.value = false;
    emit('success');
  });
};
defineExpose({
  open
});
</script>

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