<template>
  <div class="pledge-way-content">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="150px" label-position="left" class="rule-form">
      <div class="info-title">保证方式</div>
      <el-form-item label="投标文件：" prop="tenderDocument">
        <el-radio-group v-model="ruleForm.tenderDocument" :disabled="disabled"  class="form-radio-border">
          <el-radio :label="0">线下装订</el-radio>
          <el-radio :label="1">线上上传</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="投标保证方式：" prop="bidGuaranteeMethod">
        <el-radio-group v-model="ruleForm.bidGuaranteeMethod" @change="bidGuaranteeMethodChange" :disabled="disabled"  class="form-radio-border">
          <el-radio :label="0">保证金</el-radio>
          <el-radio :label="1">保函</el-radio>
        </el-radio-group>
      </el-form-item>
      <div class="form-upload-box" v-if="ruleForm.bidGuaranteeMethod">
        <div class="form-upload-name" style="min-width: 150px;">
          保函文件：
        </div>
        <cy-upload
          :disabled="disabled"
          ref="guaranteeFiles"
          :upload-params="uploadParams"
          :file-data="ruleForm.guaranteeFiles"
          :upload-from-status="true"
        >
        </cy-upload>
      </div>
      <div class="form-upload-box" v-if="ruleForm.bidGuaranteeMethod">
        <div class="form-upload-name form-required" style="min-width: 150px;">
          中标通知书 ：
        </div>
        <cy-upload
          :disabled="disabled"
          ref="bidWinningNotice"
          :upload-params="uploadParams"
          :file-data="ruleForm.bidWinningNotice"
          :upload-from-status="true"
        >
        </cy-upload>
      </div>
      <el-form-item
        label="投标说明："
        style="width: 100%;"
        class="textarea-content"
        prop="tenderDescription"
        v-if="ruleForm.bidGuaranteeMethod"
      >
        <el-input type="textarea" :disabled="disabled" v-model="ruleForm.tenderDescription" style="width: 100%;" rows="3" clearable></el-input>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
module.exports = {
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    deatailsInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      ruleForm: {
        tenderDocument: '',
        bidGuaranteeMethod: '',
        tenderDescription: '',
        guaranteeFiles: [],
        bidWinningNotice: []
      },
      rules: {
        tenderDocument: [
          { required: true, message: '请选择投标文件', trigger: 'change' },
        ],
        bidGuaranteeMethod: [
        { required: true, message: '请选择投标文件', trigger: 'change' },
        ]
      },
      uploadParams: {
        mouldName: 'BusinessManagement',
      },
    }
  },
  watch: {
    deatailsInfo: {
      handler(newV) {
        if(newV) {
          const { tenderDocument, bidGuaranteeMethod, tenderDescription, guaranteeFiles, bidWinningNotice} = JSON.parse(JSON.stringify(newV))
          this.ruleForm = {
            tenderDocument,
            bidGuaranteeMethod,
            tenderDescription,
            guaranteeFiles,
            bidWinningNotice
          }
        }
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    getSubmit() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleForm.validate(valid => {
          if(valid) {
            let params = {
              ...this.ruleForm,
            }
            if(this.ruleForm.bidGuaranteeMethod) {
              const guaranteeFiles =  this.$refs.guaranteeFiles.getFileLists
              const bidWinningNotice = this.$refs.bidWinningNotice.getFileLists
              if(!guaranteeFiles.length && !this.ruleForm.tenderDescription) {
                this.$message.warning('请上传保函文件或输入投标说明')
                return
              }
              if(!bidWinningNotice.length) {
                this.$message.warning('请上传中标通知书')
                return
              }
              params.guaranteeFiles = guaranteeFiles.map(item => { return { id: item.id}})
              params.bidWinningNotice = bidWinningNotice.map(item => { return { id: item.id}})
            }
            resolve(params)
          }else {
            reject()
          }
        })
      })
    },
    bidGuaranteeMethodChange(e) {
      this.$nextTick(() => {
        if(this.$refs.guaranteeFiles) {
          this.$refs.guaranteeFiles.fileLists = []
          this.$refs.bidWinningNotice.fileLists = []
          this.ruleForm.tenderDescription = ''
        }
      })
    }
  },
}
</script>

<style scoped>

</style>
