<template>
  <div class="company-add">
    <el-form
      ref="ruleFormRef"
      :model="ruleForm"
      :rules="rules"
      label-position="left"
      label-width="150px"
      class="rule-form-orange company-form"
    >
      <el-form-item label="企业关键字" prop="keyword" style="width: 100%">
        <el-input
          v-model="ruleForm.keyword"
          placeholder="请输入关键字查询"
        >
          <el-button slot="append" icon="el-icon-search" @click="searchClick"></el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="企业名称" prop="corporateName" style="width: 100%">
        <el-select
          v-model="ruleForm.corporateName"
          style="width: 100%"
          clearable
          placeholder="请选择企业名称"
          @change="searchChange"
        >
          <el-option
            v-for="item in listData"
            :key="item.corporateName"
            :label="item.corporateName"
            :value="item.corporateName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="统一社会信用代码" style="width: 100%" prop="socialCreditCode">
        <el-input
          v-model="ruleForm.socialCreditCode"
          placeholder="请输入统一社会信用代码"
          disabled
        />
      </el-form-item>
      <el-form-item label="征信查询授权书" prop="creditAuth" style="width: 100%">
        <cy-upload
          ref="creditAuth"
          :upload-params="{
            fileType: 'creditAuth'
          }"
          :file-data="ruleForm.creditAuth"
          :upload-from-orange-status="true"
          placeholder="请上传征信查询授权书"
        />
      </el-form-item>
      <el-form-item label="征信查询日期" prop="searchDate" style="width: 100%;" :rules="[{require: ruleForm.creditReport,message: '请选择征信查询日期', trigger: 'change'}]">
        <el-date-picker v-model="ruleForm.searchDate" value-format="yyyy-MM-dd" type="date" placeholder="选择征信查询日期" style="width: 100%;" />
      </el-form-item>
      <el-form-item label="征信报告" prop="creditReport" style="width: 100%">
        <cy-upload
          ref="creditReport"
          :upload-params="{
            fileType: 'creditReport'
          }"
          :file-data="ruleForm.creditReport"
          :upload-from-orange-status="true"
          placeholder="请上传征信报告"
        />
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
import { getEnterpriseInspectionInfo, getAuthAddress } from '@/api/creditInfoManage/index'
export default {
  data() {
    return {
      listData: [],
      ruleForm: {
        id: '',
        keyword: '',
        corporateName: '',
        socialCreditCode: '',
        searchDate: '',
        creditAuth: [],
        creditReport: []
      },
      rules: {
        corporateName: [
          { required: true, message: '请输入企业名称', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    validate() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleFormRef.validate(valid => {
          if (valid) {
            const authFile = this.$refs.creditAuth.getFileLists
            const creditFile = this.$refs.creditReport.getFileLists
            if (this.ruleForm.searchDate && !creditFile.length) {
              this.$message.warning('请上传征信报告')
              return
            }

            const postData = {
              Loading: true,
              type: 1,
              name: this.ruleForm.corporateName,
              cardNo: this.ruleForm.socialCreditCode,
              searchDate: this.ruleForm.searchDate !== '' ? this.ruleForm.searchDate + ' 00:00:00' : '',
              authAddress: authFile.length ? authFile.map(item => item.id)[0] : '',
              creditAddress: creditFile ? creditFile.map(item => item.id)[0] : ''
            }
            return resolve(postData)
          } else {
            reject()
            return false
          }
        })
      })
    },

    searchClick() {
      if (this.ruleForm.keyword) {
        getEnterpriseInspectionInfo({ keyword: this.ruleForm.keyword }).then(({ data }) => {
          this.listData = data
        })
      }
    },
    searchChange(val) {
      const { socialCreditCode } = this.listData.find(item => item.corporateName === val)
      this.ruleForm = {
        ...this.ruleForm,
        socialCreditCode
      }

      this.getAuthInfo(socialCreditCode)
    },
    // 根据统一社会信用代码查征信授权书
    getAuthInfo(code) {
      const postData = new FormData()
      postData.append('cardNo', code)
      getAuthAddress(postData).then(({ data }) => {
        console.log(data)
        this.ruleForm.authFile = [data]
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.company-add {
    .company-form {
    max-height: 55vh;
    overflow: auto;
    .el-form-item {
      margin-bottom: 20px;
    }
  }
}
</style>
