<template>
  <div class="contract-approval-content">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-position="top" class="rule-form-orange">
      <cy-info-title>电子签合同</cy-info-title>
      <el-form-item label="合同文件" style="width: 100%">
        <cy-upload
          ref="fileStorage"
          :upload-params="{}"
          :file-data="detailsInfo.fileStorageList"
          :upload-from-orange-status="true"
          disabled
          placeholder="请上传附件"
          style="width: 100%"
        />
        <!-- <div class="download-box" @click="downloadFile">
          <i class="iconfont icon-xiazai"></i>
        </div> -->
      </el-form-item>
      <template v-if="detailsInfo.smbContractInfos.length">
        <!-- 苏商银行 -->
        <cy-info-title style="margin-bottom: 20px; margin-top: 10px;">
          资方电子签合同
          <div slot="right">
            <el-button v-if="!disabled && !isResetCapital" type="text" @click="resetCapital">重新发送给资方</el-button>
          </div>
        </cy-info-title>
        <cy-comm-table
          ref="commTable"
          :columns="columns"
          :data="detailsInfo.smbContractInfos"
          :count="0"
        />
      </template>
      <cy-info-title>审批结论</cy-info-title>
      <el-form-item label="审批结论" prop="conclusion">
        <el-radio-group v-model="ruleForm.conclusion" :disabled="disabled">
          <el-radio label="pass">通过</el-radio>
          <el-radio label="back">退回</el-radio>
          <el-radio label="veto">否决</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="审批意见"
        :rules="[
          { required: ruleForm.conclusion === 'back' || ruleForm.conclusion === 'veto', message: '请输入审批意见', trigger: 'blur' }
        ]"
        prop="suggestion"
        style="width: 100%"
      >
        <el-input v-model="ruleForm.suggestion" :disabled="disabled" placeholder="请输入审批意见" clearable />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { contractApprovalDownload, resetSmbCapital } from '@/api/financingManage'
import { getFilePerview } from '@/api/dictionary'
import { exportFile } from '@/utils'
export default {
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    detailsInfo: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      ruleForm: {
        conclusion: '',
        suggestion: ''
      },
      rules: {
        conclusion: [{ required: true, message: '请选择签署结论', trigger: 'change' }]
      },
      columns: [
        {
          label: '合同类型',
          prop: 'contractTypeName',
          showTooltip: true
        },
        {
          label: '合同文件',
          prop: 'contractType',
          showTooltip: true,
          render: (h, row, index) => {
            return <cy-upload
              uploadParams={{}}
              fileData={row.fileStorage ? [row.fileStorage] : []}
              disabled={true}
              limit={1}
              defaultDesc='请上传文件'
            />
          }
        },
        {
          label: '失败原因',
          prop: 'failReason',
          showTooltip: true
        },
        {
          label: '资方签署状态',
          prop: 'signStatus',
          showTooltip: true,
          render: (h, row, index) => {
            return row.signStatus ? '签署成功' : '签署失败'
          }
        },
        {
          label: '操作',
          prop: 'amount',
          render: (h, row, index) => {
            return <div>
              { row.signStatus && <el-button type='text' onClick={() => this.downloadCapitalFile(row) }>下载</el-button> }

            </div>
          }
        }

      ]
    }
  },
  computed: {
    isResetCapital() {
      return this.detailsInfo.smbContractInfos.every(item => item.signStatus)
    }
  },
  watch: {
    detailsInfo: {
      handler(newV) {
        if (newV === undefined) return
        const { conclusion, suggestion } = newV
        this.ruleForm = {
          conclusion,
          suggestion
        }
        this.$nextTick(() => {
          this.$refs.ruleForm.resetFields()
        })
      },
      immediate: true,
      deep: true
    }
  },
  methods: {
    downloadFile() {
      const { contractInfoId } = this.detailsInfo
      contractApprovalDownload({ contractInfoId }).then((data) => {
        exportFile(data)
      })
    },
    downloadCapitalFile(row) {
      getFilePerview({ absolutePath: row.fileStorage.absolutePath }).then((data) => {
        exportFile(data)
      })
    },
    resetCapital() {
      const params = {
        Loading: true,
        smbMasterContractInfoId: this.detailsInfo.smbContractInfos.find(item => item.contractType === 1).id,
        smbguaContractContractInfoId: this.detailsInfo.smbContractInfos.find(item => item.contractType === 2).id
      }
      resetSmbCapital(params).then(() => {
        this.$message.success('重新发送给资方')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.download-box {
  position: absolute;
  top: 20%;
  right: 30px;
  z-index: 10;
  width: 30px;
  text-align: center;
}
</style>
