<template>
  <div class="add-accounts-content">
    <el-form ref="ruleForm" :key="key" :model="ruleForm" :rules="rules" label-width="150px" label-position="top" class="rule-form-orange">
      <cy-info-title>基础信息</cy-info-title>
      <el-form-item label="出入账类型" prop="billType">
        <el-radio-group v-model="ruleForm.billType" :disabled="disabled">
          <el-radio :label="0">入账</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="企业名称" prop="companyId">
        <el-select v-model="ruleForm.companyId" filterable placeholder="请选择企业名称" :disabled="disabled">
          <el-option
            v-for="item in corporateNameList"
            :key="item.id"
            :label="item.corporateName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <cy-info-title style="margin-top: 10px">出入账信息</cy-info-title>
      <el-form-item label="账户归属" prop="attributionOfAccountId">
        <el-radio-group v-model="ruleForm.attributionOfAccountId" :disabled="disabled" @change="attributionOfAccountChange(ruleForm.companyOwnershipId)">
          <el-radio v-for="item in attributionOfAccountList" :key="item.id" :label="item.id">{{ item.name }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="公司归属" prop="companyOwnershipId" class="form-required">
        <el-select
          v-if="!isCapital(ruleForm.attributionOfAccountId)"
          v-model="ruleForm.companyOwnershipId"
          filterable
          placeholder="请选择公司归属"
          :disabled="disabled"
          @change="companyOwnershipChange(ruleForm.companyOwnershipId)"
        >
          <el-option
            v-for="item in getCompanyOwnershipList(ruleForm.attributionOfAccountId)"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <el-select
          v-if="isCapital(ruleForm.attributionOfAccountId)"
          v-model="ruleForm.refinancingManagementId"
          filterable
          placeholder="请选择公司归属"
          :disabled="disabled"
          @change="refinancingManagementChange(ruleForm.refinancingManagementId)"
        >
          <el-option
            v-for="item in companyOwnershipCapitalList"
            :key="item.id"
            :label="item.institutionName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item v-if="isCapital(ruleForm.attributionOfAccountId)" label="业务类型" class="form-required">
        <el-select
          v-model="ruleForm.businessTypeId"
          filterable
          placeholder="请选择业务类型"
          :disabled="disabled"
          @change="businessTypeChange"
        >
          <el-option
            v-for="item in businessTypeList"
            :key="item.businessTypeDic.id"
            :label="item.businessTypeDic.name"
            :value="item.businessTypeDic.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开户银行名称" prop="bankName">
        <el-select
          v-if="!isCapital(ruleForm.attributionOfAccountId)"
          v-model="ruleForm.bankName"
          filterable
          placeholder="请选择开户银行名称"
          :disabled="disabled"
          @change="bankChange"
        >
          <el-option
            v-for="item in bankList"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
        <el-select
          v-if="isCapital(ruleForm.attributionOfAccountId)"
          v-model="ruleForm.bankName"
          filterable
          placeholder="请选择开户银行名称"
          :disabled="disabled"
          @change="bankChange"
        >
          <el-option
            v-for="item in bankList"
            :key="item.bankName"
            :label="item.bankName"
            :value="item.bankName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="账号" prop="bankCardNo">
        <el-input
          v-model="ruleForm.bankCardNo"
          placeholder="请输入账号"
          :disabled="disabled"
        >
        </el-input>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-radio-group v-model="ruleForm.type" :disabled="disabled">
          <el-radio
            v-for="item in constant.cashFlowEntryType"
            :key="item.code"
            :label="item.code"
          >
            {{ item.name }}
          </el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        v-if="ruleForm.type === 'entryType_cash'"
        label="入账金额"
        prop="incomeAmount"
        class="form-required"
      >
        <cy-amount-input v-model="ruleForm.incomeAmount" class="one-form-item" :disabled="disabled" />
      </el-form-item>
      <el-form-item
        v-if="ruleForm.type === 'entryType_bills_discounted'"
        label="票据金额"
        prop="invoiceAmount"
        class="one-form-item form-required"
      >
        <cy-amount-input v-model="ruleForm.invoiceAmount" class="one-form-item" :disabled="disabled" />
      </el-form-item>
      <el-form-item
        v-if="ruleForm.type === 'entryType_bills_discounted'"
        label="实际金额"
        prop="realAmount"
        class="one-form-item form-required"
      >
        <cy-amount-input v-model="ruleForm.realAmount" class="one-form-item" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="入账日期" prop="incomeDate" :class="{'one-form-item':ruleForm.type === 0}" style="width: 100%">
        <el-date-picker
          v-model="ruleForm.incomeDate"
          type="date"
          clearable
          placeholder="请选择日期"
          style="width: 100%"
          :picker-options="pickerOptions"
          value-format="yyyy-MM-dd"
          :disabled="disabled"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="附件："
        style="width:100%"
      >
        <unified-editor :content.sync="ruleForm.content" :disabled="disabled" />

      </el-form-item>
      <el-form-item label="备注" style="width: 100%; margin-right: 20px;">
        <el-input
          v-model="ruleForm.remark"
          placeholder="请输入备注"
          :disabled="disabled"
        />
      </el-form-item>
    </el-form>
  </div>
</template>
<script>
import { mapGetters } from 'vuex'
import { getEnterprises, getAttributionOfAccount, getCompanyOwnership, getInstitutionName, getNocapitalBankName, getBankCardNo, getBusinessType, getCpitalBankName, cashFlowGetEntryDetail } from '@/api/financialManagement/financialManagement'
// cashFlowGetExitDetail
import { codeChangeName, filterListData } from '@/utils/index'
export default {
  components: {
    UnifiedEditor: () => import('@/components/UnifiedEditor/index.vue')
  },
  props: {
    disabled: {
      type: Boolean,
      default: false
    }
    // deatailsInfo: {
    //   type: Object,
    //   default: () => {}
    // }
  },
  data() {
    return {
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now()
        }
      },
      key: 0,
      ruleForm: {
        id: '',
        billType: 0,
        companyId: '',
        attributionOfAccountId: '',
        companyOwnershipId: '',
        refinancingManagementId: '',
        bankName: '',
        bankCardNo: '',
        bankCardNoId: '',
        businessTypeId: '',
        businessNumberId: '',
        type: '',
        incomeDate: '',
        incomeAmount: '',
        invoiceAmount: '',
        realAmount: '',
        content: '',
        remark: ''
      },
      rules: {
        billType: [
          { required: true, message: '请选择出入账类型', trigger: 'change' }
        ],
        companyId: [
          { required: true, message: '请选择企业名称', trigger: 'change' }
        ],
        attributionOfAccountId: [
          { required: true, message: '请选择账户归属', trigger: 'change' }
        ],
        bankName: [
          { required: true, message: '请选择开户银行名称', trigger: 'blur' }
        ],
        bankCardNo: [
          { required: true, message: '请选择开户行账号', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择类型', trigger: 'change' }
        ],
        incomeDate: [
          { required: true, message: '请选择入账日期', trigger: 'blur' }
        ]
      },
      corporateNameList: [],
      attributionOfAccountList: [],
      companyOwnershipList: [],
      companyOwnershipCapitalList: [],
      bankList: [],
      nanCapitalDataList: [],
      businessTypeList: [],
      lifeFile: []
    }
  },
  computed: {
    ...mapGetters([
      'name',
      'constant'
    ]),
    getCompanyOwnershipList() {
      return (attributionOfAccountId) => {
        const attributionOfAccountCode = codeChangeName(this.attributionOfAccountList, attributionOfAccountId, 'id', 'code')
        let list = []
        if (attributionOfAccountCode === 'longSilver') {
          list = filterListData(this.companyOwnershipList, ['shang_hai_ao_hong', 'xi_an_ao_hong', 'shan_xi_ka_fubao', 'shen_zhen_chang_yin_bao', 'shang_hai_ke_fubao'])
        } else if (attributionOfAccountCode === 'noLongSilver') {
          list = filterListData(this.companyOwnershipList, ['zi_guan', 'factoring', 'investment', 'shen_zhen_chang_yin', 'shang_hai_yin_guo'])
        }
        return list
      }
    },
    isCapital() {
      return (attributionOfAccountId) => {
        const attributionOfAccountCode = codeChangeName(this.attributionOfAccountList, attributionOfAccountId, 'id', 'code')
        if (attributionOfAccountCode === 'longSilver') {
          return false
        } else if (attributionOfAccountCode === 'noLongSilver') {
          return false
        } else if (attributionOfAccountCode === 'capital') {
          return true
        } else if (attributionOfAccountCode === 'undefined') {
          return false
        }
      }
    }
  },
  created() {
    this.$store.dispatch('getConstant', ['cashFlowEntryType'])
    this.getEnterprises()
    this.getAttributionOfAccount()
    this.getCompanyOwnership()
    this.getInstitutionName()

    // 这里增加一个判断，根据出入帐类型，来查看不同的详情
    if (this.$route.query && this.$route.query.entryOutType === '1') {
      this.cashFlowGetEntryDetail()
    }
    if (this.$route.query && this.$route.query.entryOutType === '2') {
      this.getCashFlowGetExitDetail()
    }
    // const { id } = this.$route.query
    // if (id) {
    // }
  },
  methods: {
    cashFlowGetEntryDetail() {
      const { id } = this.$route.query
      if (id) {
        cashFlowGetEntryDetail(id).then(data => {
          const { cashFlowManageEntryInfo } = data.data
          this.ruleForm = {
            id: cashFlowManageEntryInfo.id,
            billType: 0,
            companyId: cashFlowManageEntryInfo.companyId,
            attributionOfAccountId: cashFlowManageEntryInfo.accountBelong,
            companyOwnershipId: cashFlowManageEntryInfo.companyBelong,
            refinancingManagementId: cashFlowManageEntryInfo.companyBelong,
            bankCardNo: cashFlowManageEntryInfo.companyBelong,
            bankCardNoId: cashFlowManageEntryInfo.companyBelong,
            type: cashFlowManageEntryInfo.type,
            incomeDate: cashFlowManageEntryInfo.incomeDate,
            incomeAmount: cashFlowManageEntryInfo.incomeAmount,
            invoiceAmount: cashFlowManageEntryInfo.invoiceAmount,
            realAmount: cashFlowManageEntryInfo.realAmount,
            remark: cashFlowManageEntryInfo.remark,
            content: cashFlowManageEntryInfo.content
          }
          const attributionOfAccountCode = codeChangeName(this.attributionOfAccountList, this.ruleForm.attributionOfAccountId, 'id', 'code')
          if (attributionOfAccountCode === 'capital') {
            this.refinancingManagementChange(this.ruleForm.companyOwnershipId)
            this.ruleForm.businessTypeId = cashFlowManageEntryInfo.businessType
            this.ruleForm.businessNumberId = cashFlowManageEntryInfo.businessTypeNumber
            setTimeout(() => {
              this.businessTypeChange()
              this.ruleForm.bankName = cashFlowManageEntryInfo.bankName
              this.bankChange()
            }, 200)
          } else {
            this.companyOwnershipChange(this.ruleForm.companyOwnershipId)
            this.ruleForm.bankName = cashFlowManageEntryInfo.bankName
            this.bankChange()
          }
        })
      }
    },
    // 获取出账详情
    getCashFlowGetExitDetail() {
      // const { id } = this.$route.query
      // if (id) {
      //   cashFlowGetExitDetail(id).then(data => {
      //     const { cashFlowManageBaseVo } = data.data
      //     this.ruleForm = {
      //       id: cashFlowManageEntryInfo.id,
      //       billType: 1,
      //       companyId: cashFlowManageEntryInfo.companyId,
      //       attributionOfAccountId: cashFlowManageEntryInfo.accountBelong,
      //       companyOwnershipId: cashFlowManageEntryInfo.companyBelong,
      //       refinancingManagementId: cashFlowManageEntryInfo.companyBelong,
      //       bankCardNo: cashFlowManageEntryInfo.companyBelong,
      //       bankCardNoId: cashFlowManageEntryInfo.companyBelong,
      //       type: cashFlowManageEntryInfo.type,
      //       incomeDate: cashFlowManageEntryInfo.incomeDate,
      //       incomeAmount: cashFlowManageEntryInfo.incomeAmount,
      //       invoiceAmount: cashFlowManageEntryInfo.invoiceAmount,
      //       realAmount: cashFlowManageEntryInfo.realAmount,
      //       remark: cashFlowManageEntryInfo.remark
      //     }
      //     const attributionOfAccountCode = codeChangeName(this.attributionOfAccountList, this.ruleForm.attributionOfAccountId, 'id', 'code')
      //     if (attributionOfAccountCode === 'capital') {
      //       this.refinancingManagementChange(this.ruleForm.companyOwnershipId)
      //       this.ruleForm.businessTypeId = cashFlowManageEntryInfo.businessType
      //       this.ruleForm.businessNumberId = cashFlowManageEntryInfo.businessTypeNumber
      //       setTimeout(() => {
      //         this.businessTypeChange()
      //         this.ruleForm.bankName = cashFlowManageEntryInfo.bankName
      //         this.bankChange()
      //       }, 200)
      //     } else {
      //       this.companyOwnershipChange(this.ruleForm.companyOwnershipId)
      //       this.ruleForm.bankName = cashFlowManageEntryInfo.bankName
      //       this.bankChange()
      //     }
      //   })
      // }
    },
    getEnterprises() {
      getEnterprises().then(response => {
        this.corporateNameList = response.data
      })
    },
    // 获取账户归属 longSilver--长银系 noLongSilver--非长银系  capital--资方
    getAttributionOfAccount() {
      getAttributionOfAccount({ parentCode: 'attributionOfAccount' }).then(response => {
        this.attributionOfAccountList = response.data
      })
    },
    getCompanyOwnership() {
      getCompanyOwnership({ parentCode: 'companyOwnership' }).then(response => {
        this.companyOwnershipList = response.data
      })
    },
    getInstitutionName() {
      getInstitutionName().then(response => {
        this.companyOwnershipCapitalList = response.data.data
      })
    },
    attributionOfAccountChange() {
      this.ruleForm.companyOwnershipId = ''
      this.ruleForm.refinancingManagementId = ''
      this.ruleForm.bankName = ''
      this.ruleForm.bankCardNo = ''
      this.ruleForm.bankCardNoId = ''
      this.ruleForm.businessTypeId = ''
      this.ruleForm.businessNumberId = ''
    },
    companyOwnershipChange(companyOwnership) {
      this.ruleForm.bankName = ''
      this.ruleForm.bankCardNo = ''
      this.ruleForm.bankCardNoId = ''
      this.ruleForm.businessTypeId = ''
      this.ruleForm.businessNumberId = ''
      const params = {
        companyOwnershipId: companyOwnership
      }
      getNocapitalBankName(params).then(({ data }) => {
        this.bankList = data
        this.nanCapitalDataList = data
      })
    },
    refinancingManagementChange(companyOwnership) {
      this.ruleForm.bankName = ''
      this.ruleForm.bankCardNo = ''
      this.ruleForm.bankCardNoId = ''
      this.ruleForm.businessTypeId = ''
      this.ruleForm.businessNumberId = ''
      const params = {
        refinancingManagementId: companyOwnership
      }
      getBusinessType(params).then(({ data }) => {
        this.businessTypeList = data.data
      })
    },
    businessTypeChange() {
      this.ruleForm.bankName = ''
      this.ruleForm.bankCardNo = ''
      this.ruleForm.bankCardNoId = ''
      this.ruleForm.businessNumberId = ''
      const institutionNameSelectData = this.businessTypeList.find(item => item.businessTypeDic.id === this.ruleForm.businessTypeId)
      this.ruleForm.businessNumberId = institutionNameSelectData.businessNumberDic.id
      const { refinancingManagementId, businessTypeId, businessNumberId } = this.ruleForm
      const params = {
        refinancingManagementId,
        businessType: businessTypeId,
        businessNumber: businessNumberId
      }
      getCpitalBankName(params).then(({ data }) => {
        this.bankList = data
      })
    },
    bankChange() {
      this.ruleForm.bankCardNo = ''
      this.ruleForm.bankCardNoId = ''
      const { bankName, companyOwnershipId, attributionOfAccountId, refinancingManagementId, businessTypeId } = this.ruleForm
      const attributionOfAccountCode = codeChangeName(this.attributionOfAccountList, attributionOfAccountId, 'id', 'code')
      let params = {}
      if (attributionOfAccountCode === 'capital') {
        params = {
          refinancingManagementId,
          bankName,
          businessType: businessTypeId
        }
      } else {
        params = {
          companyOwnershipId,
          bankName
        }
      }
      getBankCardNo(params).then(response => {
        this.ruleForm.bankCardNo = response.data[0].bankCardNo
        this.ruleForm.bankCardNoId = response.data[0].id
      })
    },
    getParams() {
      return new Promise((resolve, reject) => {
        const { companyId, attributionOfAccountId, companyOwnershipId, refinancingManagementId, bankCardNoId, businessTypeId, businessNumberId, type, incomeDate, incomeAmount, invoiceAmount, realAmount, remark, bankName, bankCardNo, id, content } = this.ruleForm

        // 账户归属选择为资方的时候校验公司归属和业务类型
        if (this.isCapital(attributionOfAccountId)) {
          if (!refinancingManagementId) {
            this.$message.warning('请选择公司归属')
            return
          }
          if (!businessTypeId) {
            this.$message.warning('请选择业务类型')
            return
          }
        }
        if (!this.isCapital(attributionOfAccountId)) {
          if (!companyOwnershipId) {
            this.$message.warning('请选择公司归属')
            return
          }
        }
        // 当类型为现金时校验入账金额
        if (type === 'entryType_cash') {
          if (!incomeAmount) {
            this.$message.warning('请输入账金额')
            return
          }
        }
        // 当类型为票据贴现时校验票据金额和实际金额
        if (type === 'entryType_bills_discounted') {
          if (!invoiceAmount) {
            this.$message.warning('请输入票据金额')
            return
          }
          if (!realAmount) {
            this.$message.warning('请输入实际金额')
            return
          }
        }

        const param = {
          Loading: true,
          id,
          companyId,
          accountBelong: attributionOfAccountId,
          companyBelong: companyOwnershipId || refinancingManagementId,
          bankAccountId: bankCardNoId,
          businessType: businessTypeId,
          businessTypeNumber: businessNumberId,
          type,
          incomeDate,
          remark,
          bankName,
          content,
          bankAccount: bankCardNo
        }
        if (type === 'entryType_cash') {
          param.incomeAmount = incomeAmount
        } else if (type === 'entryType_bills_discounted') {
          param.invoiceAmount = invoiceAmount
          param.realAmount = realAmount
        }
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            resolve(param)
          }
        })
      })
    }
  }
}
</script>

<style  scoped>

</style>
