const fs = require('fs')
const path = require('path')
const assert = require('assert')

function read(relativePath) {
  return fs.readFileSync(path.join(__dirname, '..', relativePath), 'utf8')
}

const personalCreateDialog = read('src/views/loanPricing/workflow/components/PersonalCreateDialog.vue')
const personalDetail = read('src/views/loanPricing/workflow/components/PersonalWorkflowDetail.vue')

assert(
  personalCreateDialog.includes('updatePersonalWorkflow') &&
    personalCreateDialog.includes("this.isEdit ? '编辑个人利率定价流程' : '新增个人利率定价流程'") &&
    personalCreateDialog.includes('editData.serialNum') &&
    personalCreateDialog.includes('updatePersonalWorkflow(this.editData.serialNum, data)') &&
    personalCreateDialog.includes(': createPersonalWorkflow(data)'),
  '个人弹窗应支持编辑模式回显并调用个人更新接口'
)

assert(
  !personalCreateDialog.includes('label="贷款用途"') &&
    !personalCreateDialog.includes('prop="loanPurpose"') &&
    !personalCreateDialog.includes('loanPurpose:'),
  '个人新增弹窗不应继续保留贷款用途字段'
)

assert(
  personalCreateDialog.includes('label="借款期限(年)"') && personalCreateDialog.includes('prop="loanTerm"'),
  '个人新增弹窗缺少借款期限字段'
)

assert(
  !personalCreateDialog.includes("value=\"consumer\"") && !personalCreateDialog.includes("value=\"business\""),
  '个人新增弹窗不应继续保留贷款用途选项'
)

assert(
  personalCreateDialog.includes('loanTermOptions') &&
    personalCreateDialog.includes("'1'") &&
    personalCreateDialog.includes("'6'") &&
    !personalCreateDialog.includes("'7'"),
  '个人新增弹窗借款期限选项应限制为 1-6 年'
)

assert(
  personalCreateDialog.includes('v-if="isCollateralGuarantee"') &&
    personalCreateDialog.includes("this.form.guarType === '抵押' || this.form.guarType === '质押'") &&
    personalCreateDialog.includes('resetCollateralFields()'),
  '个人新增弹窗抵质押信息未按担保方式显示并清空'
)

assert(
  personalCreateDialog.includes('collateralTypeOptions') &&
    personalCreateDialog.includes("return ['存单质押', '其他质押']"),
  '个人新增弹窗应保留质押类型选项'
)

assert(
  personalCreateDialog.includes('label="抵押物性质"') &&
    personalCreateDialog.includes('prop="collateralNature"') &&
    personalCreateDialog.includes("return ['普通住宅、土地、厂房', '商业性质不动产', '其他抵押财产']") &&
    personalCreateDialog.includes('label="是否属于九地"') &&
    personalCreateDialog.includes('九地是指绍兴、杭州、宁波、上海、北京、广州、深圳、南京、苏州') &&
    personalCreateDialog.includes(':disabled="!requiresNineArea"') &&
    personalCreateDialog.includes("'无需选择'"),
  '个人新增弹窗缺少抵押物性质、九地选择或九地说明'
)

assert(
  personalCreateDialog.includes("return this.form.isNineArea === '是' ? '一类' : '二类'") &&
    personalCreateDialog.includes("return this.form.isNineArea === '是' ? '二类' : '三类'") &&
    personalCreateDialog.includes("return '四类'"),
  '个人新增弹窗抵押物类型映射不完整'
)

assert(
  personalCreateDialog.includes("if (this.isPledge && !value)") &&
    personalCreateDialog.includes("if (this.isMortgage && !value)") &&
    personalCreateDialog.includes("if (this.requiresNineArea && !value)"),
  '个人新增弹窗缺少抵押和质押的条件校验'
)

assert(
  !personalCreateDialog.includes('label="是否有经营佐证"') &&
    !personalCreateDialog.includes('prop="bizProof"') &&
    !personalCreateDialog.includes('bizProof:') &&
    personalCreateDialog.includes("loanLoop: this.form.loanLoop ? '1' : '0'") &&
    personalCreateDialog.includes("data.collThirdParty = this.form.collThirdParty ? '1' : '0'") &&
    personalCreateDialog.includes('data.collType = this.resolveMortgageCollateralType()') &&
    personalCreateDialog.includes('delete data.collateralNature') &&
    personalCreateDialog.includes('delete data.isNineArea') &&
    personalCreateDialog.includes('delete data.collType') &&
    personalCreateDialog.includes('delete data.collThirdParty'),
  '个人新增弹窗开关字段或非抵质押提交字段处理不正确'
)

assert(
  personalDetail.includes('label="贷款用途"') && personalDetail.includes('detailData.loanPurpose'),
  '个人详情页缺少贷款用途展示'
)

assert(
  personalDetail.includes("value === '1'") && personalDetail.includes("value === '0'"),
  '个人详情页布尔格式化未兼容 1/0'
)

console.log('personal create input params assertions passed')
