<template>
  <div class="create-bidding">
    <!-- <create-bidding-Content /> -->
    <cy-info-title>基础信息</cy-info-title>
    <basic-information ref="basicInformation" :details-info="detailsInfo" :disabled="disabled" />
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-position="top"
      label-width="150px"
      class="rule-form-orange"
      style="margin-top: 20px"
    >
      <cy-info-title>招投标信息</cy-info-title>
      <el-form-item label="是否资格预审：" prop="prequalification">
        <el-radio-group v-model="ruleForm.prequalification" :disabled="disabled">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="招标文件："
        class="form-required"
      >
        <cy-upload
          ref="tendersFile"
          :upload-params="{
            fileType: 'tendersFile'
          }"
          :file-data="ruleForm.tendersFile"
          :upload-from-orange-status="true"
          placeholder="请上传招标文件"
          :disabled="disabled"
        />
      </el-form-item>
      <el-form-item
        label="投标文件："
        class="form-required"
      >
        <cy-upload
          ref="biddingFile"
          :upload-params="{
            fileType: 'biddingFile'
          }"
          :file-data="ruleForm.biddingFile"
          :upload-from-orange-status="true"
          placeholder="请上传投标文件"
          :disabled="disabled"
        />
      </el-form-item>
    </el-form>
    <div class="submit-btn">
      <el-button v-if="!disabled" @click="() => { $router.push({ name: 'MyTrade', query: {type: 'BiddingManagement'} })}">关闭</el-button>
      <el-button v-if="!disabled" type="primary" @click="onSubmit">提交</el-button>
    </div>
  </div>
</template>

<script>
import { doAddBiddingManagement, getDeatail } from '@/api/myTrade/biddingManagement/biddingManagement'
export default {
  name: 'MyTradeBiddingManagement',
  components: {
    BasicInformation: () => import('../components/BasicInformation.vue')
  },
  props: {
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      ruleForm: {
        prequalification: '',
        tendersFile: [],
        biddingFile: []
      },
      uploadParams: {},
      rules: {
        prequalification: [{ required: true, message: '请选择是否资格预审', trigger: 'blur' }]
      },
      detailsInfo: {}
      // disabled: false
    }
  },
  created() {
    this.getDetails()
  },
  methods: {
    getDetails() {
      const { id, type } = this.$route.query
      // if (type === 'see') {
      //   this.disabled = true
      // }
      if (id || type) {
        getDeatail({ id, Loading: true }).then(({ data }) => {
          this.detailsInfo = data
          this.ruleForm.prequalification = data.prequalification
          this.ruleForm.tendersFile = data.tendersFileStorageDOList || []
          this.ruleForm.biddingFile = data.biddingFileStorageDOList || []
        })
      }
    },
    validate() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleFormRef.validate((valid) => {
          if (valid) {
            resolve()
          } else {
            reject()
          }
        })
      })
    },
    async onSubmit() {
      const tendersFileList = this.$refs.tendersFile.getFileLists
      const biddingFileList = this.$refs.biddingFile.getFileLists
      const { id } = this.$route.query
      const basicInfor = await this.$refs.basicInformation.validate()
      await this.validate()
      if (!tendersFileList.length) {
        this.$message.warning('请上传招标文件')
        return
      }
      if (!biddingFileList.length) {
        this.$message.warning('请上传投标文件')
        return
      }
      const params = {
        Loading: true,
        id,
        projectInformationId: basicInfor.projectInformationId,
        businessNumber: basicInfor.businessNumber,
        tradingPlatformId: basicInfor.tradingPlatformId,
        prequalification: this.ruleForm.prequalification,
        tendersFileIds: tendersFileList.map(item => item.id),
        biddingFileIds: biddingFileList.map(item => item.id)
      }
      doAddBiddingManagement(params).then(() => {
        this.$message.success('提交成功')
        this.$router.push({
          name: 'MyTrade',
          query: {
            type: 'BiddingManagement'
          }
        })
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.create-bidding {

}
</style>
