<template>
  <div class="data-entry-container">
    <h1>COGS数据录入工具</h1>

    <!-- 当前成本结构 -->
    <div class="form-section">
      <h2>当前成本结构</h2>
      <table class="data-table">
        <thead>
        <tr>
          <th>成本项</th>
          <th>金额(元/米)</th>
          <th>占比(%)</th>
        </tr>
        </thead>
        <tbody>
        <tr>
          <td>{{ formData.curr.n1 }}</td>
          <td>
            <input type="number" v-model="formData.curr.v1" placeholder="请输入金额">
          </td>
          <td>
            <input type="number" v-model="formData.curr.p1" placeholder="请输入占比">
          </td>
        </tr>
        <tr>
          <td>{{ formData.curr.n2 }}</td>
          <td>
            <input type="number" v-model="formData.curr.v2" placeholder="请输入金额">
          </td>
          <td>
            <input type="number" v-model="formData.curr.p2" placeholder="请输入占比">
          </td>
        </tr>
        <tr>
          <td>{{ formData.curr.n3 }}</td>
          <td>
            <input type="number" v-model="formData.curr.v3" placeholder="请输入金额">
          </td>
          <td>
            <input type="number" v-model="formData.curr.p3" placeholder="请输入占比">
          </td>
        </tr>
        <tr>
          <td>{{ formData.curr.n4 }}</td>
          <td>
            <input type="number" v-model="formData.curr.v4" placeholder="请输入金额">
          </td>
          <td>
            <input type="number" v-model="formData.curr.p4" placeholder="请输入占比">
          </td>
        </tr>
        <tr>
          <td>{{ formData.curr.n5 }}</td>
          <td>
            <input type="number" v-model="formData.curr.v5" placeholder="请输入金额">
          </td>
          <td>
            <input type="number" v-model="formData.curr.p5" placeholder="请输入占比">
          </td>
        </tr>
        <tr>
          <td>{{ formData.curr.n6 }}</td>
          <td>
            <input type="number" v-model="formData.curr.v6" placeholder="请输入金额">
          </td>
          <td>
            <input type="number" v-model="formData.curr.p6" placeholder="请输入占比">
          </td>
        </tr>
        </tbody>
      </table>
    </div>

    <!-- 历史成本数据 -->
    <div class="form-section">
      <h2>单位成本历史数据</h2>
      <button @click="regenerateHistoricalData" class="add-btn">重新生成过去12个月</button>
      <table class="data-table">
        <thead>
          <tr>
            <th>月份</th>
            <th v-for="(key, index) in ['n1', 'n2', 'n3', 'n4', 'n5', 'n6']" :key="index">{{ formData.curr[key] }}</th>
          </tr>
        </thead>
        <tbody>
          <tr v-for="(item, index) in formData.his.n1" :key="index">
            <td>
              <input type="text" v-model="formData.his.n1[index].month" placeholder="请输入月份">
            </td>
            <td v-for="(key, idx) in ['n1', 'n2', 'n3', 'n4', 'n5', 'n6']" :key="idx">
              <input type="number" v-model="formData.his[key][index].value" placeholder="请输入值">
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <!-- 成本总趋势 -->
    <div class="form-section">
      <h2>成本总趋势</h2>
      <button @click="regenerateHistoricalData" class="add-btn">重新生成过去12个月</button>
      <table class="data-table">
        <thead>
        <tr>
          <th>月份</th>
          <th>总成本(元/米)</th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="(item, index) in formData.trend" :key="index">
          <td>
            <input type="text" v-model="item.month" placeholder="请输入月份">
          </td>
          <td>
            <input type="number" v-model="item.value" placeholder="请输入值">
          </td>
        </tr>
        </tbody>
      </table>
    </div>

    <!-- 生产效率数据 -->
    <div class="form-section">
      <h2>人均单位成本</h2>
      <table class="data-table">
        <thead>
        <tr>
          <th v-for="(item, index) in formData.prod" :key="index">{{ item.name }}</th>
        </tr>
        </thead>
        <tbody>
        <tr>
          <td v-for="(item, index) in formData.prod" :key="index">
            <input type="number" v-model="item.value" placeholder="请输入值">
          </td>
        </tr>
        </tbody>
      </table>
    </div>

    <!-- 成本明细数据 -->
    <div class="form-section">
      <h2>成本明细数据</h2>
      <button @click="addDetailItem" class="add-btn">添加成本项</button>
      <div class="table-container">
        <table class="data-table detail-table">
          <thead>
          <tr>
            <th>成本项</th>
            <th v-for="(process, index) in processes" :key="index">{{ process }}</th>
            <th>操作</th>
          </tr>
          </thead>
          <tbody>
          <tr v-for="(item, index) in formData.detail" :key="index">
            <td>
              <input v-model="item.item" placeholder="请输入成本项">
            </td>
            <td v-for="(value, valueIndex) in item.values" :key="valueIndex">
              <input type="number" v-model="item.values[valueIndex]" placeholder="请输入值">
            </td>
            <td>
              <button @click="removeDetailItem(index)" class="remove-btn">删除</button>
            </td>
          </tr>
          </tbody>
        </table>
      </div>
    </div>

    <div class="actions">
      <button @click="submitForm" class="primary">保存</button>
    </div>

    <div v-if="jsonOutput" class="json-output">
      <h3>生成的JSON数据</h3>
      <pre>{{ jsonOutput }}</pre>
      <button @click="copyToClipboard">复制到剪贴板</button>
    </div>
  </div>
</template>

<script>
import {onMounted, reactive, ref} from 'vue';
import {getCogs, saveCogs} from "@/api/house/input";

export default {
  name: "CogsInput",
  setup() {
    const formData = reactive({
      curr: {
        total: "4.32",
        n1: "人工工资+社保+福利",
        v1: 0.76,
        p1: 14.7,
        n2: "电+汽",
        v2: 1.3,
        p2: 26.7,
        n3: "运营+管理",
        v3: 0.33,
        p3: 11.3,
        n4: "机物料+修理+制版材料+染料+助剂+辅料+水",
        v4: 1.63,
        p4: 33.3,
        n5: "折旧",
        v5: 0.48,
        p5: 13.3,
        n6: "其他",
        v6: 0.08,
        p6: 0.67
      },
      his: {
        n1: [],
        n2: [],
        n3: [],
        n4: [],
        n5: [],
        n6: []
      },
      trend: [],
      prod: [],
      detail: []
    });

    const jsonOutput = ref('');
    const processes = ref(["加弹", "经编", "前整", "印染", "后整", "成品"]);

    // 页面加载时获取数据
    const fetchData = async () => {
      try {
        const response = await getCogs();
        if (response && response.data) {
          // 用获取到的数据填充表单
          Object.assign(formData, response.data);
        } else {
          console.warn('接口返回数据为空或格式不正确');
        }
      } catch (error) {
        console.error('获取数据失败:', error);
        // 保留默认数据，确保页面不为空白
      }
    };

    // 组件挂载时调用获取数据的方法
    onMounted(() => {
      fetchData();
    });

    const submitForm = async () => {
      try {
        await saveCogs(formData);
        alert('表单提交成功');
      } catch (error) {
        console.error('提交失败:', error);
        alert('提交失败');
      }
    };
    const generatePastMonths = () => {
      const months = [];
      const now = new Date();
      for (let i = 11; i >= 0; i--) {
        const date = new Date(now.getFullYear(), now.getMonth() - i, 1);
        const month = (date.getMonth() + 1).toString().padStart(2, '0');
        const year = date.getFullYear();
        months.push(`${year}-${month}`);
      }
      return months;
    };

    // 添加重新生成历史数据的方法
    const regenerateHistoricalData = () => {
      const newMonths = generatePastMonths();
      
      // 更新每个成本项的月份数据
      const costKeys = ['n1', 'n2', 'n3', 'n4', 'n5', 'n6'];
      costKeys.forEach(key => {
        if (formData.his[key] && Array.isArray(formData.his[key])) {
          formData.his[key] = formData.his[key].map((item, index) => ({
            ...item,
            month: newMonths[index] || item.month
          }));
        }
      });
      
      // 更新总趋势数据的月份
      if (formData.trend && Array.isArray(formData.trend)) {
        formData.trend = formData.trend.map((item, index) => ({
          ...item,
          month: newMonths[index] || item.month
        }));
      }
    };


    const addDetailItem = () => {
      formData.detail.push({
        item: "",
        values: Array(processes.value.length).fill(0)
      });
    };

    const removeDetailItem = (index) => {
      formData.detail.splice(index, 1);
    };

    const generateJson = () => {
      jsonOutput.value = JSON.stringify(formData, null, 2);
    };

    const copyToClipboard = () => {
      navigator.clipboard.writeText(jsonOutput.value)
          .then(() => alert('已复制到剪贴板'))
          .catch(err => console.error('复制失败:', err));
    };

    return {
      formData,
      jsonOutput,
      processes,
      submitForm,
      addDetailItem,
      removeDetailItem,
      generateJson,
      copyToClipboard,
      regenerateHistoricalData
    };
  }
};
</script>

<style scoped>
/* 基础布局样式 */
.data-entry-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  background-color: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

/* 标题样式 */
h1 {
  text-align: center;
  margin-bottom: 24px;
  color: #333;
  font-size: 24px;
}

h2 {
  margin: 20px 0 10px 0;
  color: #333;
}

h3 {
  margin: 15px 0 10px 0;
  color: #555;
  font-size: 16px;
}

/* 表单区域样式 */
.form-section {
  margin-bottom: 24px;
  background: #f8f9fa;
  padding: 20px;
  border-radius: 6px;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.form-section h2 {
  margin-bottom: 16px;
  color: #333;
  font-size: 18px;
  border-bottom: 1px solid #e0e0e0;
  padding-bottom: 8px;
}

.form-subsection {
  margin: 20px 0;
  padding: 15px;
  background: #ffffff;
  border-radius: 4px;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

/* 响应式布局 */
@media (max-width: 768px) {
  .form-section {
    padding: 15px;
  }

  .form-subsection {
    padding: 10px;
  }

  .data-table td input {
    font-size: 14px;
    padding: 4px 8px;
  }
}

/* 悬停效果 */
.form-subsection:hover {
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: box-shadow 0.3s ease-in-out;
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 16px;
}

.form-group {
  display: flex;
  flex-direction: column;
}

.form-group label {
  margin-bottom: 6px;
  font-weight: 500;
  color: #555;
}

.form-group input {
  padding: 10px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  transition: border-color 0.3s;
}

.form-group input:focus {
  outline: none;
  border-color: #409eff;
}

.add-btn {
  background-color: #409eff;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  margin-bottom: 12px;
  transition: background-color 0.3s;
}

.add-btn:hover {
  background-color: #337ecc;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 12px;
  background-color: white;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.data-table th,
.data-table td {
  padding: 12px;
  border: 1px solid #ebeef5;
  text-align: left;
}

.data-table th {
  background-color: #f5f7fa;
  font-weight: 500;
  color: #606266;
}

.data-table td input {
  width: 100%;
  padding: 6px 10px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  box-sizing: border-box;
}

.remove-btn {
  background-color: #f56c6c;
  color: white;
  border: none;
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.remove-btn:hover {
  background-color: #e45656;
}

.actions {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin-top: 24px;
}

.actions button {
  padding: 10px 24px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.actions button:not(.primary) {
  background-color: #f4f4f5;
  color: #606266;
}

.actions button:not(.primary):hover {
  background-color: #e9e9eb;
}

.actions button.primary {
  background-color: #67c23a;
  color: white;
}

.actions button.primary:hover {
  background-color: #5daf34;
}

.json-output {
  margin-top: 24px;
  background-color: #f5f7fa;
  padding: 16px;
  border-radius: 4px;
}

.json-output h3 {
  margin-top: 0;
  color: #333;
}

.json-output pre {
  white-space: pre-wrap;
  word-wrap: break-word;
  background-color: #fff;
  padding: 12px;
  border-radius: 4px;
  max-height: 300px;
  overflow-y: auto;
}

.table-container {
  overflow-x: auto;
}

.detail-table th,
.detail-table td {
  white-space: nowrap;
}

/* 按钮组样式 */
.actions {
  display: flex;
  justify-content: center;
  gap: 16px;
  margin-top: 24px;
}

.actions button {
  padding: 10px 24px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.actions button:not(.primary) {
  background-color: #f4f4f5;
  color: #606266;
}

.actions button:not(.primary):hover {
  background-color: #e9e9eb;
}

.actions button.primary {
  background-color: #67c23a;
  color: white;
}

.actions button.primary:hover {
  background-color: #5daf34;
}

/* JSON输出区域样式 */
.json-output {
  margin-top: 24px;
  background-color: #f5f7fa;
  padding: 16px;
  border-radius: 4px;
}

.json-output h3 {
  margin-top: 0;
  color: #333;
}

.json-output pre {
  white-space: pre-wrap;
  word-wrap: break-word;
  background-color: #fff;
  padding: 12px;
  border-radius: 4px;
  max-height: 300px;
  overflow-y: auto;
}

/* 表格容器 */
.table-container {
  overflow-x: auto;
}

/* 表格样式 */
.data-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 12px;
  background-color: white;
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.05);
}

.data-table th,
.data-table td {
  padding: 12px;
  border: 1px solid #ebeef5;
  text-align: left;
}

.data-table th {
  background-color: #f5f7fa;
  font-weight: 500;
  color: #606266;
}

.data-table td input {
  width: 100%;
  padding: 6px 10px;
  border: 1px solid #dcdfe6;
  border-radius: 4px;
  box-sizing: border-box;
}

/* 按钮样式 */
.add-btn {
  background-color: #409eff;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 4px;
  cursor: pointer;
  margin-bottom: 12px;
  transition: background-color 0.3s;
}

.add-btn:hover {
  background-color: #337ecc;
}

.remove-btn {
  background-color: #f56c6c;
  color: white;
  border: none;
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.remove-btn:hover {
  background-color: #e45656;
}
</style>
