<template>
  <div class="pay-condition">
    <el-form
      :model="ruleForm"
      label-position="top"
      label-width="150px"
      class="rule-form-orange"
      ref="resultForm"
    >
      <el-table
        :data="ruleForm.creditConclusionList"
        style="width: 100%">
        <el-table-column label=" 序号" type="index"></el-table-column>
        <el-table-column prop="position" label="条件">
          <template slot-scope="scope">
            <el-form-item label="" :prop="`creditConclusionList.${scope.$index}.content`" style="width: 100%; margin-bottom: 15px;"
              :rules="[{ required: true, message: '请输入条件', trigger: 'blur' }]" v-if="scope.row.type != 1"
            >
              <el-input placeholder="请输入条件" :disabled="scope.$index < 7 || isView" v-model="ruleForm.creditConclusionList[scope.$index].content" style="width: 100%;" />
            </el-form-item>
            <div v-else>
              <el-form-item l-form-item v-for="item in scope.row.content" :key="item.content" label="" style="width: 100%;">
                <el-input placeholder="请输入条件" disabled v-model="item.content" style="width: 100%;" />
              </el-form-item>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="操作" width="200" v-if="!isView">
          <template slot-scope="scope">
            <el-button type="text" v-if="scope.$index >= 6" @click="ruleForm.creditConclusionList.push({content: ''})">增加</el-button>
            <el-button type="text" v-if="scope.$index > 6" @click="deleteCondition(scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        creditConclusionList: []
      },
      isFirst: true
    }
  },
  props: ["info", "isView"],
  methods: {
    deleteCondition(index) {
      this.ruleForm.creditConclusionList.splice(index, 1)
    },
    confirm(type) {
      let arr = [...this.ruleForm.creditConclusionList]
      arr[6] = {
        content: this.ruleForm.creditConclusionList[6].content.map(item => {
          return item.content
        }).join('。')
      }
      return new Promise((res, rej) => {
        if(type === 'submit') {
          this.$refs.resultForm.validate(flag => {
            if(flag) {
              res({ creditConclusionList: arr })
            } else {
              rej()
            }
          })
        }else {
          res({ creditConclusionList: arr })
        }

      })
    }
  },
  watch: {
    'info.randomId': {
      handler: function(val) {
        if(val) {
          let arr = []
          if(this.isView) {
            arr = [ ...this.info.creditConclusionHistoryList ]
            let list = arr[6].content.split('。').map(item => {
              return { content: item }
            })
            arr.splice(6, 1, ...list)
            this.ruleForm.creditConclusionList = arr
          } else {
            if(this.isFirst && this.info.creditConclusionList.length > 6) {
              arr = this.info.creditConclusionList.length > 6 ? this.info.creditConclusionList.splice(7, this.info.creditConclusionList.length - 1) : []
              this.isFirst = false
            } else {
              arr = this.ruleForm.creditConclusionList.length > 6 ? this.ruleForm.creditConclusionList.splice(7, this.ruleForm.creditConclusionList.length - 1) : []
            }
            this.ruleForm.creditConclusionList.splice(0)
            this.ruleForm.creditConclusionList.push({ content: `给予卖方${this.info.supplierEnterprise.corporateName}授信金额${this.info.proposedCreditLine}元人民币。` })
            this.ruleForm.creditConclusionList.push({ content: `授信期限自${this.info.creditStartDate}---${this.info.creditEndDate}。` })
            this.ruleForm.creditConclusionList.push({ content: `本次合作的买方为${this.info.coreEnterprise.corporateName}，项目为${this.info.projectInformation.projectName}。` })
            this.ruleForm.creditConclusionList.push({ content: `利息和管理费由卖方支付，支付方式为${this.info.paymentMethodForInterestManagementFee ? this.info.paymentMethodForInterestManagementFee == 'paymentMethodForInterestManagementFee_A' ? '前收' : this.info.paymentMethodForInterestManagementFee == 'paymentMethodForInterestManagementFee_B' ? '后收' : this.info.paymentMethodForInterestManagementFee == 'paymentMethodForInterestManagementFee_C' ? '管理费前收利息后收' : this.info.paymentMethodForInterestManagementFeeDetail : '________'}。` })
            this.ruleForm.creditConclusionList.push({ content: `上游账期为${this.info.upstreamAccountingPeriod || '________'}天，保理融资期限为${this.info.factoringFinancingPeriod || '________'}天，账期自下游验收货物并结算后起算` })
            this.ruleForm.creditConclusionList.push({ content: `上报卖方${this.info.reportMethod === 1 ? 'A类' : this.info.reportMethod === 0 ? 'B类' : '________'}征信。` })
            this.info.creditEnhancementMeasuresIllustrate[0].content = this.info.creditEnhancementMeasuresIllustrate[0].msg
            if(this.info.creditEnhancementMeasuresIllustrate.length > 1) {
              this.info.creditEnhancementMeasuresIllustrate[1].content = this.info.creditEnhancementMeasuresIllustrate[1].msg + (this.info.creditEnhancementMeasuresIllustrate[1].code || '________征信')
            }
            if(this.info.creditEnhancementMeasuresIllustrate.length > 2) {
              this.info.creditEnhancementMeasuresIllustrate[2].content = this.info.creditEnhancementMeasuresIllustrate[2].msg + (this.info.creditEnhancementMeasuresIllustrate[2].code || '________征信')
            }
            this.ruleForm.creditConclusionList.push({ content: this.info.creditEnhancementMeasuresIllustrate, type: 1 })
            this.ruleForm.creditConclusionList = [...this.ruleForm.creditConclusionList, ...arr]
          }
        }
      },
      immediate: true
    }
  }
}
</script>

<style lang="scss" scoped>
.basic-info {

}

</style>
