const fs = require('fs')

const file = 'd:/huimvDemo/用工/huimv-employment/huimv-employment/app/packageA/components/home/EnterpriseHome.vue'
let s = fs.readFileSync(file, 'utf8')

// Verify Chinese before patch
if (!s.includes('\u5c97\u4f4d\u7c7b\u578b')) {
	console.error('File missing Chinese before patch, abort')
	process.exit(1)
}

const oldBlock = [
	'\t\t\t\t\t\t\t\t<view class="fe-biz-card__header">',
	'\t\t\t\t\t\t\t\t\t<text class="fe-biz-card__header-title">\ud83d\udcc4 {{ (msg.draft && msg.draft.title) || \'\u7528\u5de5\u8349\u7a3f\' }}</text>',
	'\t\t\t\t\t\t\t\t\t<text class="fe-draft-card-status" :class="msg.draftStatusClass">{{ msg.draftStatusLabel }}</text>',
	'\t\t\t\t\t\t\t\t</view>',
	'\t\t\t\t\t\t\t\t<view class="fe-biz-card__row">',
	'\t\t\t\t\t\t\t\t\t<text>\u5c97\u4f4d\u7c7b\u578b</text>',
	'\t\t\t\t\t\t\t\t\t<text class="fe-biz-card__val">{{ (msg.draft && msg.draft.workType) || \'\u5f85\u8865\u5145\' }}</text>',
	'\t\t\t\t\t\t\t\t</view>',
	'\t\t\t\t\t\t\t\t<view class="fe-biz-card__row">',
	'\t\t\t\t\t\t\t\t\t<text>\u5de5\u4f5c\u5730\u70b9</text>',
	'\t\t\t\t\t\t\t\t\t<text class="fe-biz-card__val">{{ (msg.draft && msg.draft.workLocation) || \'\u5f85\u8865\u5145\' }}</text>',
	'\t\t\t\t\t\t\t\t</view>',
	'\t\t\t\t\t\t\t\t<view class="fe-biz-card__row">',
	'\t\t\t\t\t\t\t\t\t<text>\u7528\u5de5\u4eba\u6570</text>',
	'\t\t\t\t\t\t\t\t\t<text class="fe-biz-card__val">{{ (msg.draft && msg.draft.headcountText) || \'\u5f85\u8865\u5145\' }}</text>',
	'\t\t\t\t\t\t\t\t</view>',
	'\t\t\t\t\t\t\t\t<view class="fe-draft-price">',
	'\t\t\t\t\t\t\t\t\t{{ (msg.draft && msg.draft.estimatedTotalText) || \'\u2014\' }}',
	'\t\t\t\t\t\t\t\t\t<text style="font-size:26rpx;font-weight:500;color:#64748B;">\u9884\u4f30\u603b\u4ef7</text>',
	'\t\t\t\t\t\t\t\t</view>'
].join('\n')

const newBlock = [
	'\t\t\t\t\t\t\t\t<view class="fe-biz-card__header">',
	'\t\t\t\t\t\t\t\t\t<text class="fe-biz-card__header-title">\ud83d\udcc4 {{ (msg.draft && msg.draft.title) || \'\u7528\u5de5\u8349\u7a3f\' }}</text>',
	'\t\t\t\t\t\t\t\t\t<text class="fe-draft-card-status" :class="msg.draftStatusClass">{{ msg.draftStatusLabel }}</text>',
	'\t\t\t\t\t\t\t\t</view>',
	'\t\t\t\t\t\t\t\t<view',
	'\t\t\t\t\t\t\t\t\tv-if="msg.draft && msg.draft.workerGroupRows && msg.draft.workerGroupRows.length"',
	'\t\t\t\t\t\t\t\t\tclass="fe-draft-groups"',
	'\t\t\t\t\t\t\t\t>',
	'\t\t\t\t\t\t\t\t\t<view',
	'\t\t\t\t\t\t\t\t\t\tv-for="(g, gIdx) in msg.draft.workerGroupRows"',
	'\t\t\t\t\t\t\t\t\t\t:key="gIdx"',
	'\t\t\t\t\t\t\t\t\t\tclass="fe-draft-group"',
	'\t\t\t\t\t\t\t\t\t>',
	'\t\t\t\t\t\t\t\t\t\t<view v-if="msg.draft.workerGroupRows.length > 1" class="fe-draft-group__title">',
	'\t\t\t\t\t\t\t\t\t\t\t\u5de5\u79cd {{ gIdx + 1 }}',
	'\t\t\t\t\t\t\t\t\t\t</view>',
	'\t\t\t\t\t\t\t\t\t\t<view class="fe-biz-card__row">',
	'\t\t\t\t\t\t\t\t\t\t\t<text>\u5c97\u4f4d\u7c7b\u578b</text>',
	'\t\t\t\t\t\t\t\t\t\t\t<text class="fe-biz-card__val">{{ g.workType }}</text>',
	'\t\t\t\t\t\t\t\t\t\t</view>',
	'\t\t\t\t\t\t\t\t\t\t<view class="fe-biz-card__row">',
	'\t\t\t\t\t\t\t\t\t\t\t<text>\u5de5\u4f5c\u5730\u70b9</text>',
	'\t\t\t\t\t\t\t\t\t\t\t<text class="fe-biz-card__val">{{ g.workLocation }}</text>',
	'\t\t\t\t\t\t\t\t\t\t</view>',
	'\t\t\t\t\t\t\t\t\t\t<view class="fe-biz-card__row">',
	'\t\t\t\t\t\t\t\t\t\t\t<text>\u7528\u5de5\u4eba\u6570</text>',
	'\t\t\t\t\t\t\t\t\t\t\t<text class="fe-biz-card__val">{{ g.headcountText }}</text>',
	'\t\t\t\t\t\t\t\t\t\t</view>',
	'\t\t\t\t\t\t\t\t\t</view>',
	'\t\t\t\t\t\t\t\t</view>',
	'\t\t\t\t\t\t\t\t<view v-else>',
	'\t\t\t\t\t\t\t\t\t<view class="fe-biz-card__row">',
	'\t\t\t\t\t\t\t\t\t\t<text>\u5c97\u4f4d\u7c7b\u578b</text>',
	'\t\t\t\t\t\t\t\t\t\t<text class="fe-biz-card__val">{{ (msg.draft && msg.draft.workType) || \'\u5f85\u8865\u5145\' }}</text>',
	'\t\t\t\t\t\t\t\t\t</view>',
	'\t\t\t\t\t\t\t\t\t<view class="fe-biz-card__row">',
	'\t\t\t\t\t\t\t\t\t\t<text>\u5de5\u4f5c\u5730\u70b9</text>',
	'\t\t\t\t\t\t\t\t\t\t<text class="fe-biz-card__val">{{ (msg.draft && msg.draft.workLocation) || \'\u5f85\u8865\u5145\' }}</text>',
	'\t\t\t\t\t\t\t\t\t</view>',
	'\t\t\t\t\t\t\t\t\t<view class="fe-biz-card__row">',
	'\t\t\t\t\t\t\t\t\t\t<text>\u7528\u5de5\u4eba\u6570</text>',
	'\t\t\t\t\t\t\t\t\t\t<text class="fe-biz-card__val">{{ (msg.draft && msg.draft.headcountText) || \'\u5f85\u8865\u5145\' }}</text>',
	'\t\t\t\t\t\t\t\t\t</view>',
	'\t\t\t\t\t\t\t\t</view>',
	'\t\t\t\t\t\t\t\t<view class="fe-draft-price">',
	'\t\t\t\t\t\t\t\t\t{{ (msg.draft && msg.draft.estimatedTotalText) || \'\u2014\' }}',
	'\t\t\t\t\t\t\t\t\t<text style="font-size:26rpx;font-weight:500;color:#64748B;">\u9884\u4f30\u603b\u4ef7</text>',
	'\t\t\t\t\t\t\t\t</view>'
].join('\n')

// Normalize line endings for match: file may be CRLF
const oldCrlf = oldBlock.replace(/\n/g, '\r\n')
const newCrlf = newBlock.replace(/\n/g, '\r\n')

if (s.includes(oldCrlf)) {
	s = s.split(oldCrlf).join(newCrlf)
	console.log('patched with CRLF')
} else if (s.includes(oldBlock)) {
	s = s.split(oldBlock).join(newBlock)
	console.log('patched with LF')
} else {
	console.error('old block not found')
	// debug nearby
	const i = s.indexOf('fe-biz-card__header-title')
	console.log(JSON.stringify(s.slice(i, i + 200)))
	process.exit(1)
}

fs.writeFileSync(file, s, { encoding: 'utf8' })
const out = fs.readFileSync(file, 'utf8')
console.log('??? count', (out.match(/\?\?\?/g) || []).length)
console.log('has workerGroupRows', out.includes('workerGroupRows'))
console.log('has gangwei', out.includes('\u5c97\u4f4d\u7c7b\u578b'))
