<template>
  <div class="financial-situation">
    <cy-info-title type="center">企业财务情况</cy-info-title>
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-position="top"
      label-width="150px"
      class="rule-form-orange"
    >
      <el-form-item label="纳税申报表" style="width: 100%" class="form-required" prop="taxReturn">
        <cy-upload
          ref="taxReturn"
          :upload-params="{
            fileType: 'enterprise_tax_return'
          }"
          :file-data="taxReturn"
          :upload-from-orange-status="true"
          placeholder="请上传纳税申报表"
          disabled
        />
      </el-form-item>
      <cy-info-title>
        近两年财务报表
      </cy-info-title>
      <cy-comm-table
        ref="commTable"
        style="margin-top: 10px; width: 100%"
        :columns="columns"
        :data="tableData"
        :count="0"
      />
      <el-form-item v-if="!hasGuaranteeEnterprise" label="企业内账报表" style="width: 100%" prop="accountingreport">
        <cy-upload
          ref="accountingreport"
          :upload-params="{
            fileType: 'enterprise_accounting_report'
          }"
          :file-data="accountingreport"
          :upload-from-orange-status="true"
          placeholder="请上传企业内账报表"
          disabled
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  props: {
    financialInfo: {
      type: Object,
      default: () => {}
    },
    hasGuaranteeEnterprise: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      taxReturn: [],
      accountingreport: [],
      ruleForm: {
      },
      rules: {
      },
      tableData: [],
      columns: [
        {
          label: '序号',
          index: true
        },
        {
          label: '报表类型',
          prop: 'type',
          ruleRequired: true
        },
        {
          label: '报表年月',
          prop: 'date',
          ruleRequired: true
        },
        {
          label: '金额单位',
          prop: 'amountUnit',
          ruleRequired: true
        },
        // {
        //   label: '创建时间',
        //   prop: 'socialCreditCode'
        // },
        {
          label: '附件',
          ruleRequired: true,
          render: (h, row, index) => {
            return <cy-upload
              ref={`fileStorage${index}`}
              limit={1}
              uploadParams={{
                fileType: 'enterprise_financial_statements'
              }}
              fileData={row.fileStorage}
              defaultDesc='请上传附件'
              disabled
            />
          }
        }
      ]
    }
  },
  watch: {
    financialInfo: {
      handler(newV) {
        if (newV && Object.keys(newV).length) {
          const { taxReturn, corporateFinances, accountingreport } = JSON.parse(JSON.stringify(newV))
          this.taxReturn = taxReturn
          this.accountingreport = accountingreport
          this.tableData = corporateFinances.map(item => {
            item.fileStorage = [item.fileStorage]
            return item
          })
        }
      },
      immediate: true
    }
  },
  methods: {
    // optionsClick(type, index) {
    //   if (type === 'add') {
    //     this.tableData.push(
    //       {
    //         type: '',
    //         date: '',
    //         amountUnit: '',
    //         fileStorage: []
    //       }
    //     )
    //   } else if (type === 'delete') {
    //     this.tableData.splice(index, 1)
    //   }
    // },
    getParams(type) {
      const taxReturn = this.$refs.taxReturn.getFileLists
      const corporateFinances = this.tableData.map((item, index) => {
        const { type, date, amountUnit } = item
        const fileStorage = this.$refs.commTable.$refs[`fileStorage${index}`].getFileLists
        return {
          type, date, amountUnit,
          fileId: fileStorage.map(item => item.id)[0]
        }
      })

      var params = {
        taxReturnIds: taxReturn.map(item => item.id),
        corporateFinances: corporateFinances,
        accountingreportIds: []
      }
      if (!this.hasGuaranteeEnterprise) {
        const accountingreport = this.$refs.accountingreport.getFileLists
        params = {
          ...params,
          accountingreportIds: accountingreport.map(item => item.id)
        }
      }
      return new Promise((resolve, reject) => {
        if (type === 'submit') {
          this.$refs.ruleForm.validate(valid => {
            if (!taxReturn.length) {
              this.$message.warning('请上传纳税申报表')
              return
            }
            if (!this.tableData.length) {
              this.$message.warning('至少有一条近两年财务报表')
              return
            }
            for (let i = 0; i < this.tableData.length; i++) {
              const { type, date, amountUnit } = this.tableData[i]
              if (!type) {
                this.$message.warning('请输入报表类型')
                return
              }
              if (!date) {
                this.$message.warning('请选择报表年月')
                return
              }
              if (!amountUnit) {
                this.$message.warning('请输入金额单位')
                return
              }
              const fileStorage = this.$refs.commTable.$refs[`fileStorage${i}`].getFileLists
              if (!fileStorage.length) {
                if (!amountUnit) {
                  this.$message.warning('请上传附件')
                  return
                }
              }
            }

            if (valid) {
              resolve(params)
            } else {
              reject()
              return false
            }
          })
        } else {
          resolve(params)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.financial-situation {

}
</style>
