<template>
  <div class="financial-situation">
    <el-form
      ref="ruleForm"
      :model="ruleForm"
      :rules="rules"
      label-position="top"
      label-width="150px"
      class="rule-form-orange"
    >
      <el-form-item
        label="指标名称"
        prop="resource"
        style="width: 100%"
      >
        <el-input v-model="title" disabled placeholder="请输入指标名称" />
      </el-form-item>
      <el-form-item
        label="纳税申报表"
        prop="resource"
        style="width: 100%"
      >
        <cy-upload
          :upload-params="{}"
          :file-data="defaultDatailsInfo.enterpriseTaxReturnFiles"
          disabled
          :upload-from-orange-status="true"
          placeholder="请上传附件"
        />
      </el-form-item>
      <el-form-item
        label="近两年财务报表"
        prop="resource"
        style="width: 100%"
      >
        <cy-comm-table
          ref="commTable"
          :columns="columns"
          :data="defaultDatailsInfo.corporateFinanceList"
        />
      </el-form-item>
      <el-form-item
        label="企业内账报表"
        prop="resource"
        style="width: 100%"
      >
        <cy-upload
          :upload-params="{}"
          :file-data="defaultDatailsInfo.enterpriseAccountingReportFiles"
          disabled
          :upload-from-orange-status="true"
          placeholder="请上传附件"
        />
      </el-form-item>
      <el-form-item
        style="width: 100%;"
      >
        <div class="auditing-box">
          <div>
            <cy-info-title type="center">审核内容</cy-info-title>
          </div>
          <div>
            <cy-info-title type="center">调整内容</cy-info-title>
          </div>
        </div>
      </el-form-item>
      <el-form-item label="主营业务收入" class="one-form-item">
        <el-input v-model="defaultDatailsInfo.mainBusinessIncome" disabled placeholder="请输入主营业务收入">
          <template slot="append">元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="主营业务收入" prop="mainBusinessIncomeFix" class="one-form-item">
        <cy-amount-input v-model="ruleForm.mainBusinessIncomeFix" :is-unit="true" unit-name="元" placeholder="请输入主营业务收入" :disabled="disabled" @blur="calcMainBusinessProfit">
          <template slot="append">元</template>
        </cy-amount-input>
      </el-form-item>
      <el-form-item label="主营业务成本" class="one-form-item">
        <el-input v-model="defaultDatailsInfo.mainBusinessCost" disabled placeholder="请输入主营业务成本">
          <template slot="append">元</template>
        </el-input>
      </el-form-item>
      <el-form-item label="主营业务成本" prop="mainBusinessCostFix" class="one-form-item">
        <cy-amount-input
          v-model="ruleForm.mainBusinessCostFix"
          :is-unit="true"
          unit-name="元"
          placeholder="请输入主营业务成本"
          :disabled="disabled"
          @blur="calcMainBusinessProfit"
        >
          <template slot="append">元</template>
        </cy-amount-input>
      </el-form-item>
      <el-form-item label="上年度毛利率" class="one-form-item">
        <el-input v-model="defaultDatailsInfo.mainBusinessProfit" disabled placeholder="请输入上年度毛利率">
          <template slot="append">%</template>
        </el-input>
      </el-form-item>
      <el-form-item label="上年度毛利率" prop="mainBusinessProfitFix" class="one-form-item">
        <el-input v-model="ruleForm.mainBusinessProfitFix" placeholder="请输入上年度毛利率" disabled>
          <template slot="append">%</template>
        </el-input>
      </el-form-item>
      <el-form-item
        label="问题"
        prop="resource"
        style="width: 100%;"
      >
        <el-input v-model="ruleForm.question" type="textarea" placeholder="请输入问题" :disabled="disabled" />
      </el-form-item>
      <el-form-item
        label=" 尽调反馈"
        prop="resource"
        style="width: 100%;"
      >
        <unified-editor :content.sync="ruleForm.feedback" :disabled="disabled" />
        <cy-upload
          ref="fileStorage"
          :upload-params="{}"
          :file-data="fileStorageList"
          :upload-from-orange-status="true"
          placeholder="请上传附件"
          :disabled="disabled"
        />
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import bus from '@/utils/eventBus'
export default {
  components: {
    UnifiedEditor: () => import('@/components/UnifiedEditor/index.vue')
  },
  inject: ['datailsInfo', 'disabled', 'submitType'],
  data() {
    return {
      title: '企业财务情况',
      ruleForm: {
        mainBusinessIncomeFix: '',
        mainBusinessCostFix: '',
        mainBusinessProfitFix: '',
        question: '',
        feedback: ''
      },
      rules: {
        mainBusinessIncomeFix: [
          { required: true, message: '请输入主营业务收入', trigger: 'blur' }
        ],
        mainBusinessCostFix: [
          { required: true, message: '请输入主营业务成本', trigger: 'blur' }
        ],
        mainBusinessProfitFix: [
          { required: true, message: '请输入上年度毛利率', trigger: 'blur' }
        ]
      },
      fileStorageList: [],
      tableData: [],
      columns: [
        {
          label: '序号',
          index: true,
          width: '100'
        },
        {
          label: '报表类型',
          prop: 'type',
          showTooltip: true
        },
        {
          label: '报表年月',
          showTooltip: true,
          prop: 'date'
        },
        {
          label: '金额单位',
          showTooltip: true,
          prop: 'amountUnit'
        },
        // {
        //   label: '创建时间',
        //   showTooltip: true,
        //   prop: 'userNumber'
        // },
        {
          label: '附件',
          showTooltip: true,
          render: (h, row) => {
            return <cy-upload
              uploadParams={{}}
              fileData={[row.fileStorage]}
              disabled
              defaultDesc='请上传附件'
            />
          }
        }
      ]
    }
  },
  computed: {
    defaultDatailsInfo() {
      return this.datailsInfo.value
    }
  },
  watch: {
    datailsInfo: {
      handler(newV) {
        const { mainBusinessIncomeFix, mainBusinessCostFix, mainBusinessProfitFix, videoDueDiligenceFinance } = newV.value
        this.ruleForm = {
          mainBusinessIncomeFix,
          mainBusinessCostFix,
          mainBusinessProfitFix,
          question: videoDueDiligenceFinance.question,
          feedback: videoDueDiligenceFinance?.feedback
        }
        this.fileStorageList = videoDueDiligenceFinance?.files || []
      },
      deep: true,
      immediate: true
    }
  },
  mounted() {
    const _this = this
    bus.on('financialSituationParams', (cb) => {
      cb(_this.getParams())
    })
  },
  methods: {
    calcMainBusinessProfit() {
      const { mainBusinessIncomeFix, mainBusinessCostFix } = this.ruleForm
      if (mainBusinessIncomeFix && mainBusinessCostFix) {
        this.ruleForm.mainBusinessProfitFix = ((Number(mainBusinessIncomeFix) - Number(mainBusinessCostFix)) / Number(mainBusinessIncomeFix) * 100).toFixed(2)
      } else {
        this.ruleForm.mainBusinessProfitFix = ''
      }
    },
    async getParams() {
      const _this = this
      return new Promise((resolve, reject) => {
        const fileList = _this.$refs.fileStorage.getFileLists
        const params = {
          ..._this.ruleForm,
          fileIds: fileList.map(item => item.id)
        }
        if (_this.submitType() === 'temp') {
          resolve(params)
        } else {
          _this.$refs.ruleForm.validate(valid => {
            if (valid) {
              resolve(params)
            } else {
              reject()
              return false
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.financial-situation {
  .auditing-box {
    display: flex;
    justify-content: space-around;
  }
}
</style>
