// 批量生成所有页面组件 - 第1部分
const fs = require('fs');
const path = require('path');
const b = 'D:/opencode/haoliang/frontend/src/views';

function w(d, f, c) {
  const p = path.join(b, d, f);
  fs.writeFileSync(p, c, 'utf8');
  console.log('OK: ' + d + '/' + f);
}

// ==================== 品牌列表 ====================
w('brand', 'BrandListPage.vue', `<template>
  <div>
    <div style="margin-bottom:16px"><el-button type="primary" @click="goAdd">+ 新增品牌</el-button></div>
    <el-table :data="tableData" border stripe v-loading="loading">
      <el-table-column prop="brandName" label="品牌名称" width="150"/>
      <el-table-column prop="deviceField" label="device字段" width="120"/>
      <el-table-column prop="tagsPath" label="tags路径" width="120"/>
      <el-table-column label="状态" width="80" align="center"><template #default="{row}"><el-tag :type="row.isEnabled?'success':'danger'" size="small">{{row.isEnabled?'启用':'停用'}}</el-tag></template></el-table-column>
      <el-table-column prop="fieldCount" label="字段数" width="80" align="center"/>
      <el-table-column label="操作" width="200" align="center"><template #default="{row}">
        <el-button link type="primary" @click="goEdit(row.id)">编辑</el-button>
        <el-button link type="primary" @click="handleToggle(row)">{{row.isEnabled?'禁用':'启用'}}</el-button>
        <el-button link type="danger" @click="handleDelete(row)">删除</el-button>
      </template></el-table-column>
    </el-table>
  </div>
</template>
<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/utils/request'
import { useMockMode } from '@/composables/useMockMode'
const router = useRouter()
const { isMock } = useMockMode()
const loading = ref(false)
const tableData = ref<any[]>([])
function goAdd() { router.push(isMock.value ? '/mock/brand/create' : '/brand/create') }
function goEdit(id: number) { router.push((isMock.value ? '/mock/brand/' : '/brand/') + id + '/edit') }
async function loadData() { loading.value = true; try { const r: any = await request.get('/admin/brand'); tableData.value = r.data?.items || [] } finally { loading.value = false } }
async function handleToggle(row: any) { await ElMessageBox.confirm('确定' + (row.isEnabled ? '禁用' : '启用') + '？', '提示', { type: 'warning' }); await request.post('/admin/brand/toggle', { id: row.id }); ElMessage.success('操作成功'); loadData() }
async function handleDelete(row: any) { await ElMessageBox.confirm('确定删除【' + row.brandName + '】？此操作不可恢复。', '提示', { type: 'warning' }); await request.post('/admin/brand/delete', { id: row.id }); ElMessage.success('已删除'); loadData() }
onMounted(loadData)
</script>`);

// ==================== 品牌编辑 ====================
w('brand', 'BrandEditPage.vue', `<template>
  <div>
    <div style="display:flex;align-items:center;gap:12px;margin-bottom:20px">
      <el-button @click="$router.back()"><el-icon><ArrowLeft /></el-icon> 返回</el-button>
      <span style="font-size:16px;font-weight:bold">{{ isEdit ? '编辑品牌' : '新增品牌' }}</span>
    </div>
    <el-card shadow="hover" style="margin-bottom:20px">
      <el-form :model="form" label-width="100px">
        <el-form-item label="品牌名称" required><el-input v-model="form.brandName" maxlength="50" /></el-form-item>
        <el-form-item label="device字段" required><el-input v-model="form.deviceField" /></el-form-item>
        <el-form-item label="tags路径" required><el-input v-model="form.tagsPath" /></el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="hover">
      <template #header><div style="display:flex;justify-content:space-between"><span>字段映射列表</span><el-button size="small" @click="addMapping">+ 新增映射</el-button></div></template>
      <el-table :data="form.mappings" border stripe size="small">
        <el-table-column label="标准字段" width="160"><template #default="{row}"><el-select v-model="row.standardField"><el-option v-for="f in standardFields" :key="f" :label="f" :value="f" /></el-select></template></el-table-column>
        <el-table-column label="字段名" width="120"><template #default="{row}"><el-input v-model="row.fieldName" /></template></el-table-column>
        <el-table-column label="匹配方式" width="100"><template #default="{row}"><el-select v-model="row.matchBy"><el-option label="id" value="id" /><el-option label="desc" value="desc" /></el-select></template></el-table-column>
        <el-table-column label="数据类型" width="100"><template #default="{row}"><el-select v-model="row.dataType"><el-option label="string" value="string" /><el-option label="number" value="number" /></el-select></template></el-table-column>
        <el-table-column label="必填" width="60" align="center"><template #default="{row}"><el-checkbox v-model="row.isRequired" :true-value="1" :false-value="0" /></template></el-table-column>
        <el-table-column label="操作" width="80" align="center"><template #default="{ $index }"><el-button link type="danger" @click="form.mappings.splice($index, 1)">删除</el-button></template></el-table-column>
      </el-table>
    </el-card>
    <div style="margin-top:20px;text-align:right">
      <el-button @click="$router.back()">取消</el-button>
      <el-button type="primary" :loading="submitting" @click="handleSave">保存</el-button>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/utils/request'
const route = useRoute()
const router = useRouter()
const isEdit = !!route.params.id
const submitting = ref(false)
const standardFields = ['program_name','part_count','device_status','run_status','operate_mode','spindle_speed_set','feed_speed_set','spindle_speed_actual','feed_speed_actual','spindle_load','spindle_override','power_on_time','run_time','cutting_time','cycle_time','machining_status']
const form = reactive({ brandName: '', deviceField: 'device', tagsPath: 'tags', mappings: [] as any[] })
function addMapping() { form.mappings.push({ standardField: '', fieldName: '', matchBy: 'id', dataType: 'string', isRequired: 0 }) }
async function loadData() {
  if (!isEdit) return
  const r: any = await request.get('/admin/brand/detail', { params: { id: route.params.id } })
  if (r.data) { form.brandName = r.data.brandName; form.deviceField = r.data.deviceField; form.tagsPath = r.data.tagsPath; form.mappings = r.data.mappings || [] }
}
async function handleSave() {
  await ElMessageBox.confirm('品牌模板修改不影响历史数据，确定保存？', '提示', { type: 'warning' })
  submitting.value = true
  try { await request.post(isEdit ? '/admin/brand/update' : '/admin/brand', { ...form, id: isEdit ? route.params.id : undefined }); ElMessage.success('保存成功'); router.back() } finally { submitting.value = false }
}
onMounted(loadData)
</script>`);

console.log('Part 1 done: brand pages');
