<!--
@description:添加财务审计报告
@Modifier:qk
@routerPath:supplierDeclaration/supplier/financialSituation
@date:2020/2/17-17:27
-->
<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="dialogData.dialog"
    :close-on-click-modal="false"
    :title="dialogTitle"
    width="700px"
    custom-class="el-dialog__body-p0"
    top="calc((100vh - 711px) / 2)"
  >
    <div class="main mb10">
      <el-form
        ref="form"
        :model="financialConditionData"
        :rules="rules"
        :inline-message="true"
      >
        <table class="from_table">
          <tr>
            <td><el-form-item ref="year" prop="year" label="年度" /></td>
            <td>
              <el-select v-model="financialConditionData.year" placeholder="请选择">
                <el-option
                  v-for="item in financialConditionYearList"
                  :key="item.typecode"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </td>
          </tr>
          <tr>
            <td><el-form-item ref="currencies" prop="currencies" label="币种" /></td>
            <td>
              <el-select v-model="financialConditionData.currencies" placeholder="请选择">
                <el-option
                  v-for="item in currencies"
                  :key="item.typecode"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </td>
          </tr>
          <tr>
            <td><el-form-item ref="totalAssets" prop="totalAssets" label="资产总计(万元)" /></td>
            <td><money :thousands="true" :unit-fixed="6" :money-value="financialConditionData" width="100%" name="totalAssets" /></td>
          </tr>
          <tr>
            <td><el-form-item ref="totalLiabilities" prop="totalLiabilities" label="负债总计(万元)" /></td>
            <td><money :thousands="true" :unit-fixed="6" :money-value="financialConditionData" width="100%" name="totalLiabilities" /></td>
          </tr>
          <tr>
            <td><el-form-item ref="totalProfit" prop="totalProfit" label="利润总额(万元)" /></td>
            <td><money :negative="true" :thousands="true" :unit-fixed="6" :money-value="financialConditionData" width="100%" name="totalProfit" /></td>
          </tr>
          <tr>
            <td><el-form-item ref="businessIncome" prop="businessIncome" label="营业收入(万元)" /></td>
            <td><money :thousands="true" :unit-fixed="6" :money-value="financialConditionData" width="100%" name="businessIncome" /></td>
          </tr>
          <tr>
            <td><el-form-item ref="netProfit" prop="netProfit" label="净利润(万元)" /></td>
            <td><money :negative="true" :thousands="true" :unit-fixed="6" :money-value="financialConditionData" width="100%" name="netProfit" /></td>
          </tr>
          <tr>
            <td><el-form-item ref="assetLiabilityRatio" prop="assetLiabilityRatio" label="负债率(%)" /></td>
            <td><money :unit-fixed="2" :disabled="true" :money-value="financialConditionData" width="100%" name="assetLiabilityRatio" /></td>
          </tr>
          <tr>
            <td><el-form-item ref="totalCurrentAssets" prop="totalCurrentAssets" label="流动负债总计(万元)" /></td>
            <td> <money :thousands="true" :unit-fixed="6" :money-value="financialConditionData" width="100%" name="totalCurrentAssets" /></td>
          </tr>
          <tr>
            <td> <el-form-item ref="totalCurrentLiabilities" prop="totalCurrentLiabilities" label="流动资产总计(万元)" /></td>
            <td><money :thousands="true" :unit-fixed="6" :money-value="financialConditionData" width="100%" name="totalCurrentLiabilities" /></td>
          </tr>
          <tr>
            <td><el-form-item ref="flowRate" prop="flowRate" label="流动比率(%)" /></td>
            <td><money :unit-fixed="2" :disabled="true" :money-value="financialConditionData" width="100%" name="flowRate" /></td>
          </tr>
          <tr>
            <td>
              <el-form-item
                ref="financialAuditingReportType"
                label="财务审计报告扫描件"
              />
            </td>
            <td>
              <upload
                ref="upload"
                :on-change="financialAuditingReportChange"
                :data="financialAuditingReportData"
                :auto-delete="true"
                :auto-upload="true"
                :limit="1"
                file-ref="fileRef"
                but-text="文件上传"
                title=""
                accept="application/msword,.doc,.docx,application/pdf"
                tip-text="上传格式 (.doc .docx .pdf)"
              />
            </td>
          </tr>
        </table>
      </el-form>
    </div>
    <!--底部操作按钮-->
    <span slot="footer" class="dialog-footer">
      <el-button :loading="confirmLoading" type="primary" @click="ensure('form')">确 认</el-button>
      <el-button :loading="confirmLoading" @click="cancel()">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* 接口*/
import { queryFinancialSituationInfo, saveFinancialSituationInfo } from '@/api/supplier/supplierFinancialSituation'
/* 公用方法 */
import { characters1_auto, checkNum, checkMyriadMoney } from '@/assets/js/validate'
import { randomData, restoreInitialValue, GetPercent } from '@assets/js/utils'
/* 混合开发 */
import { declareHegSupperGroupMixin } from '@/views/supplier/editorDeclaration/mixins/declareHegSupperGroupMixin'// 供应商-字典
import { declareHegSupperMixins } from '@/views/supplier/editorDeclaration/mixins/declareHegSupperMixins'// 供应商-公用方法参数
/* 组件*/
import money from '@comp/common/money/money'// 金额
import upload from '@comp/common/updownload/upload'

export default {
  name: 'EnterpriseQualificationDialog',
  components: {
    money,
    upload
  },
  mixins: [declareHegSupperMixins, declareHegSupperGroupMixin],
  props: {
    // 弹框展示状态
    dialogData: {
      type: Object,
      default: function() {
        return {
          dialog: false, // 展示状态
          id: '', // 编辑时才会有财务状况id
          oldId: '' // 修改时用的Id
        }
      }
    }
  },
  data() {
    return {
      dialogTitle: '', // 弹框标题
      confirmLoading: false, // Loading
      // 年度选择-只能选择从今年起到向前三年的年份
      financialConditionYearList: [],
      rules: {
        currencies: [{ required: true, message: '请选择', trigger: 'change' }], // 币种
        year: [{ required: true, message: '请选择', trigger: 'change' }], // 年度
        totalAssets: [
          { required: true, validator: characters1_auto, trigger: 'blur' },
          { required: true, validator: checkMyriadMoney, trigger: 'blur' }
        ], // 资产总计
        totalLiabilities: [
          { required: true, validator: characters1_auto, trigger: 'blur' },
          { required: true, validator: checkMyriadMoney, trigger: 'blur' }
        ], // 负债总计
        totalProfit: [
          { required: true, validator: characters1_auto, trigger: 'blur' }
        ], // 利润总额
        businessIncome: [
          { required: true, validator: characters1_auto, trigger: 'blur' },
          { required: true, validator: checkMyriadMoney, trigger: 'blur' }
        ], // 营业收入
        netProfit: [
          { required: true, validator: characters1_auto, trigger: 'blur' }
        ], // 净利润
        assetLiabilityRatio: [{ required: true, validator: checkNum, trigger: 'change' }], // 资产负债率%
        totalCurrentAssets: [
          { required: true, validator: characters1_auto, trigger: 'blur' },
          { required: true, validator: checkMyriadMoney, trigger: 'blur' }
        ], // 流动资产总计
        totalCurrentLiabilities: [
          { required: true, validator: characters1_auto, trigger: 'blur' },
          { required: true, validator: checkMyriadMoney, trigger: 'blur' }
        ], // 流动负债总计
        flowRate: [{ required: true, validator: checkNum, trigger: 'change' }], // 流动比率%
        financialAuditingReportType: [{ required: true, message: '请上传', trigger: 'change' }] // 财务审计报告扫描件
      }
    }
  },
  watch: {
    // 弹框展示状态
    dialogData: {
      handler(newVal) {
        // 判断是否为编辑
        if (newVal.dialog) { // 编辑时
          if (newVal.id) { // 编辑时
            this.dialogTitle = '编辑财务审计报告'
            this.initData()// 查询编辑数据
          } else {
            this.dialogTitle = '添加财务审计报告'
            if (this.financialConditionData.id) { // 非编辑时&&有上次的遗留信息时
              this.restoreInitial()// 恢复初始值
            }
            this.financialConditionData.id = randomData()// 主键id
            this.financialConditionData.changeType = '00'
            this.financialAuditingReportData.bizId = this.financialConditionData.id// 财务审计报告扫描件
          }
        }
      },
      deep: true
    },
    // 资产总计
    'financialConditionData.totalAssets'(val) {
      if (Number(val) === 0) {
        this.$message({
          message: '资产总计不能为0',
          type: 'warning'
        })
        this.financialConditionData.totalAssets = 0
      } else {
        this.assetLiabilityRatio()// 负债率计算%
      }
    },
    // 负债总计
    'financialConditionData.totalLiabilities'(val) {
      if (Number(val) === 0) {
        this.$message({
          message: '负债总计不能为0',
          type: 'warning'
        })
        this.financialConditionData.totalLiabilities = 0
      } else {
        this.assetLiabilityRatio()// 负债率计算%
      }
    },
    // 流动资产总计
    'financialConditionData.totalCurrentAssets'(val) {
      if (Number(val) === 0) {
        this.$message({
          message: '流动资产总计不能为0',
          type: 'warning'
        })
        this.financialConditionData.totalCurrentAssets = 0
      } else {
        this.flowRate()// 流动负债率计算%
      }
    },
    // 流动负债总计
    'financialConditionData.totalCurrentLiabilities'(val) {
      if (Number(val) === 0) {
        this.$message({
          message: '流动负债总计不能为0',
          type: 'warning'
        })
        this.financialConditionData.totalCurrentLiabilities = 0
      } else {
        this.flowRate()// 流动负债率计算%
      }
    }
  },
  created() {
    // 生成附件bizId
    if (this.dialogData.id === '') {
      this.financialConditionData.id = randomData()// 主键id
      this.financialConditionData.changeType = '00'
      this.financialAuditingReportData.bizId = this.financialConditionData.id// 财务审计报告扫描件
    } else {
      this.dialogTitle = '编辑财务审计报告'
      this.initData()
    }
    this.getSupplyRecordDictionaries()// 申报材料供货记录-字典信息
    this.financialConditionYearInt()// 初始化年度
  },
  methods: {
    // 查询编辑数据
    initData() {
      const data = {
        id: this.dialogData.id,
        oldId: this.dialogData.oldId
      }
      queryFinancialSituationInfo(data).then(res => {
        this.financialConditionData = res.data
        this.financialConditionData.changeType = '01'
        this.financialAuditingReportData.bizId = this.financialConditionData.id// 财务审计报告扫描件
      })
    },
    // 年度选择-只能选择从今年起到向前三年的年份
    financialConditionYearInt() {
      for (let i = 3; i >= 0; i--) {
        this.financialConditionYearList.push(
          { typename: this.$Moment().subtract(i, 'years').format('YYYY'),
            typecode: this.$Moment().subtract(i, 'years').format('YYYY') }
        )
      }
    },
    // 财务审计报告扫描件
    financialAuditingReportChange(file, fileList) {
      if (fileList.length > 0) { // 上传了
        this.$set(this.financialConditionData, 'financialAuditingReportType', 'true')
      } else { // 未上传
        this.$set(this.financialConditionData, 'financialAuditingReportType', '')
      }
    },
    // 负债率计算%
    assetLiabilityRatio() {
      // 负债率=负债总计/资产总计*100%
      this.financialConditionData.assetLiabilityRatio = GetPercent(this.financialConditionData.totalLiabilities, this.financialConditionData.totalAssets)
    },
    // 流动负债率计算%
    flowRate() {
      // 流动负债率=流动资产总计/流动负债总计*100%
      this.financialConditionData.flowRate = GetPercent(this.financialConditionData.totalCurrentAssets, this.financialConditionData.totalCurrentLiabilities)
    },
    // 选择信息-确定
    ensure(formName) {
      // 校验输入内容
      this.$refs[formName].validate((valid, field) => {
        if (valid) { // 校验通过
          this.confirmLoading = true // Loading
          this.financialConditionData.status = '00'// 00 保存 01 提交
          saveFinancialSituationInfo(this.financialConditionData).then(response => {
            // this.restoreInitial()// 恢复初始值
            this.$emit('consignContractInfo', this.financialConditionData)
            this.dialogData.dialog = false
            this.confirmLoading = false // Loading
          }).catch(() => {
            this.confirmLoading = false // Loading
          })
        }
      })
    },
    // 取消
    cancel() {
      this.dialogData.dialog = false// 关闭弹框
      this.confirmLoading = false // Loading
    },
    // 恢复初始值
    restoreInitial() {
      // 恢复初始值
      restoreInitialValue(this.financialConditionData)
    }
  }
}
</script>

<style scoped lang="less">

</style>

