<template>
  <div>
    <!-- 左侧树形结构 -->
    <el-container>
      <el-aside width="180px">
        <div class="JKWTree-tree">
          <h3>计件工计时单维护</h3>
          <el-tree :data="treeData" :props="defaultProps" highlight-current="true"
            @node-click="handleNodeClick"></el-tree>
        </div>
      </el-aside>

      <el-container>
        <el-main>
          <!-- 按钮区域 -->
          <div class="gva-table-box">
            <div class="gva-btn-list">
              <el-row :span="6">
                <el-input v-model="searchInfo" placeholder="输入工单编号或产品名称" />
              </el-row>
              <el-button type="primary" :icon="Search" @click="onSearch">搜索</el-button>
              <el-button type="primary" :icon="Refresh">重置</el-button>
              <div style="margin-left: auto;">
                <el-button type="primary" :icon="Download" @click="exportExcel">导出到Excel</el-button>
              </div>
            </div>
            <!-- 数据展示 -->
            <el-table ref="multipleTable" style="width: 100%" tooltip-effect="dark" :data="tableData" row-key="ID" border
              highlight-current-row="true" @selection-change="handleSelectionChange" @row-dblclick="doubleClick">
              <el-table-column type="selection" width="55" />
              <el-table-column align="left" prop="wgjs_rq" label="日期" width="100" />
              <el-table-column align="left" prop="wgjs_bh1" label="员工编号1" width="100" />
              <el-table-column align="left" prop="name1" label="姓名1" width="100" />
              <el-table-column align="left" prop="department" label="所在部门" width="100" />
              <el-table-column align="left" prop="wgjs_js1" label="计时1" width="100" />
              <el-table-column align="left" prop="wgjs_yy1" label="原因1" width="100" />

              <el-table-column align="left" prop="wgjs_bh2" label="员工编号2" width="100" />
              <el-table-column align="left" prop="name2" label="姓名2" width="100" />
              <el-table-column align="left" prop="wgjs_js2" label="计时2" width="100" />
              <el-table-column align="left" prop="wgjs_yy2" label="原因2" width="100" />

              <el-table-column align="left" prop="wgjs_bh3" label="员工编号3" width="100" />
              <el-table-column align="left" prop="name3" label="姓名3" width="100" />
              <el-table-column align="left" prop="wgjs_js3" label="计时3" width="100" />
              <el-table-column align="left" prop="wgjs_yy3" label="原因3" width="100" />

              <el-table-column align="left" prop="wgjs_bh4" label="员工编号4" width="100" />
              <el-table-column align="left" prop="name4" label="姓名4" width="100" />
              <el-table-column align="left" prop="wgjs_js4" label="计时4" width="100" />
              <el-table-column align="left" prop="wgjs_yy4" label="原因4" width="100" />

              <el-table-column align="left" prop="wgjs_bh5" label="员工编号5" width="100" />
              <el-table-column align="left" prop="name5" label="姓名5" width="100" />
              <el-table-column align="left" prop="wgjs_js5" label="计时5" width="100" />
              <el-table-column align="left" prop="wgjs_yy5" label="原因5" width="100" />

              <el-table-column align="left" prop="wgjs_bh6" label="员工编号6" width="100" />
              <el-table-column align="left" prop="name6" label="姓名6" width="100" />
              <el-table-column align="left" prop="wgjs_js6" label="计时6" width="100" />
              <el-table-column align="left" prop="wgjs_yy6" label="原因6" width="100" />

            </el-table>
            <!-- 分页 -->
            <div class="gva-pagination">
              <el-pagination layout="total, sizes, prev, pager, next, jumper" :current-page="page" :page-size="limit"
                :page-sizes="[10, 30, 50, 100]" :total="total" @current-change="handleCurrentChange"
                @size-change="handleSizeChange" />
            </div>
          </div>
          <!-- 弹出框 -->
          <el-dialog v-model="dialogFormVisible" :before-close="closeDialog" title="计件工计时单维护" destroy-on-close
            width="800px">
            <!-- <el-scrollbar height="500px"> -->
            <el-form :model="formData" label-position="left" ref="elFormRef" :rules="rule">

              <!-- 日期 -->
              <el-row>
                <el-form-item label="日期" prop="name">
                  <el-input v-model="formData.image" :clearable="true" placeholder="2024.01.03" style="width: 100px;" />
                </el-form-item>
              </el-row>

              <!-- 标题栏 -->
              <el-row :gutter="10">
                <el-col :span="3">
                  <span>员工编号</span>
                </el-col>
                <el-col :span="3">
                  <span>员工姓名</span>
                </el-col>
                <el-col :span="3">
                  <span>计时</span>
                </el-col>
                <el-col :span="12">
                  <span>原因备注</span>
                </el-col>
                <el-col :span="3">
                  <span>冲月定额</span>
                </el-col>
              </el-row>

              <!-- 员工1 -->
              <el-row :gutter="5" style="margin-top: 10px;">
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="ZI00401" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="瞿春生" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="11.0" />
                </el-col>
                <el-col :span="12">
                  <el-input v-model="formData.image" :clearable="true" placeholder="打包废纸" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="是" />
                </el-col>
              </el-row>

              <!-- 员工2 -->
              <el-row :gutter="5" style="margin-top: 10px;">
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="ZI00401" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="瞿春生" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="11.0" />
                </el-col>
                <el-col :span="12">
                  <el-input v-model="formData.image" :clearable="true" placeholder="打包废纸" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="是" />
                </el-col>
              </el-row>

              <!-- 员工3 -->
              <el-row :gutter="5" style="margin-top: 10px;">
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="ZI00401" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="瞿春生" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="11.0" />
                </el-col>
                <el-col :span="12">
                  <el-input v-model="formData.image" :clearable="true" placeholder="打包废纸" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="是" />
                </el-col>
              </el-row>

              <!-- 员工4 -->
              <el-row :gutter="5" style="margin-top: 10px;">
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="ZI00401" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="瞿春生" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="11.0" />
                </el-col>
                <el-col :span="12">
                  <el-input v-model="formData.image" :clearable="true" placeholder="打包废纸" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="是" />
                </el-col>
              </el-row>

              <!-- 员工5 -->
              <el-row :gutter="5" style="margin-top: 10px;">
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="ZI00401" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="瞿春生" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="11.0" />
                </el-col>
                <el-col :span="12">
                  <el-input v-model="formData.image" :clearable="true" placeholder="打包废纸" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="是" />
                </el-col>
              </el-row>

              <!-- 员工6 -->
              <el-row :gutter="5" style="margin-top: 10px;">
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="ZI00401" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="瞿春生" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="11.0" />
                </el-col>
                <el-col :span="12">
                  <el-input v-model="formData.image" :clearable="true" placeholder="打包废纸" />
                </el-col>
                <el-col :span="3">
                  <el-input v-model="formData.image" :clearable="true" placeholder="是" />
                </el-col>
              </el-row>

            </el-form>

            <template #footer>
              <div class="dialog-footer">
                <el-button @click="closeDialog">取 消</el-button>
                <el-button type="primary" @click="enterDialog">确 定</el-button>
              </div>
            </template>
          </el-dialog>

        </el-main>
      </el-container>
    </el-container>

  </div>
</template>

<script setup>
import {
  createCompany,
  deleteCompany,
  deleteCompanyByIds,
  updateCompany,
  findCompany,
  getCompanyList
} from '@/api/company'

// 全量引入格式化工具 请按需保留
import { getDictFunc, formatDate, formatBoolean, filterDict, ReturnArrImg, onDownloadFile } from '@/utils/format'
import { ElMessage, ElMessageBox } from 'element-plus'
import { Search, Refresh, Download } from '@element-plus/icons-vue'
import { ref, reactive, onMounted, onBeforeMount } from 'vue'
import { getTimeSheetSideTable, getTimeSheetTable } from '@/api/mes_api_gty/myapi'

defineOptions({
  name: '08-timesheet'
})

// 侧边栏数据请求
const treeData = reactive([]);
const getSideData = async () => {
  const response = await getTimeSheetSideTable();
  if (response.code === 0) {
    const transformedData = response.data.map(item => ({
      label: item.date,
      params: {
        date: item.date.substring(0, 4) + '-' + item.date.substring(4),
      },
    }));
    treeData.splice(0, treeData.length, ...transformedData);
  }
};
getSideData();


// 自动化生成的字典（可能为空）以及字段
const formData = ref({
  rq: '',
  ygbh1: '',
  xm1: '',
  szbm: '',
  js1: '',
  yy1: '',
  ygbh2: '',
  xm2: '',
  js2: '',
  cl2: '',
  ygbh3: '',
  xm3: '',
  js3: '',
  cl3: '',
  ygbh4: '',
  xm4: '',
  js4: '',
  cl4: '',
  ygbh5: '',
  xm5: '',
  js5: '',
  cl5: '',
  ygbh6: '',
  xm6: '',
  js6: '',
})

// 验证规则
const rule = reactive({
})

const elFormRef = ref()
const elSearchFormRef = ref()

// 表格数据部分
const tableData = reactive([
  {
    rq: '2023/12/31 ',
    ygbh1: 'ZM00369',
    xm1: '曹会灵',
    szbm: '模切车间',
    js1: '9',
    yy1: '打包废纸',
    ygbh2: 'M00141',
    xm2: '张超',
    js2: '17',
    cl2: '',
    ygbh3: '',
    xm3: '',
    js3: '',
    cl3: '',
    ygbh4: '',
    xm4: '',
    js4: '',
    cl4: '',
    ygbh5: '',
    xm5: '',
    js5: '',
    cl5: '',
    ygbh6: '',
    xm6: '',
    js6: '',
  },
])
const page = ref(1)
const total = ref(0)
const limit = ref(10)
const searchInfo = ref('')
const params = {
  date: '',
  page: page.value.toString(),
  limit: limit.value.toString(),
}

// 分页设置
const handleSizeChange = (val) => {
  limit.value = val;
  params.limit = val.toString();
  getTableData();
}

// 页面跳转
const handleCurrentChange = (val) => {
  page.value = val;
  params.page = val.toString();
  getTableData();
}

const getTableData = async () => {
  const response = await getTimeSheetTable(params)
  if (response.code === 0) {
    total.value = response.data.total;
    tableData.splice(0, tableData.length, ...response.data.rows);
  }
}

const handleNodeClick = (node, check) => {
  if (node.params) {
    params.date = node.params.date;
    handleCurrentChange(1);
  }
}

// 搜索
function onSearch() {
}

// 获取需要的字典 可能为空 按需保留
const setOptions = async () => {
}

// 获取需要的字典 可能为空 按需保留
setOptions()

// 多选数据
const multipleSelection = ref([])
// 多选
const handleSelectionChange = (val) => {
  multipleSelection.value = val
}

// 批量删除控制标记
const deleteVisible = ref(false)

// 多选删除
const onDelete = async () => {
  const ids = []
  if (multipleSelection.value.length === 0) {
    ElMessage({
      type: 'warning',
      message: '请选择要删除的数据'
    })
    return
  }
  multipleSelection.value &&
    multipleSelection.value.map(item => {
      ids.push(item.ID)
    })
  const res = await deleteCompanyByIds({ ids })
  if (res.code === 0) {
    ElMessage({
      type: 'success',
      message: '删除成功'
    })
    if (tableData.value.length === ids.length && page.value > 1) {
      page.value--
    }
    deleteVisible.value = false
    getTableData()
  }
}

// 行为控制标记（弹窗内部需要增还是改）
const type = ref('')

// 更新行
const updateCompanyFunc = async (row) => {
  const res = await findCompany({ ID: row.ID })
  type.value = 'update'
  if (res.code === 0) {
    formData.value = res.data.recompany
    dialogFormVisible.value = true
  }
}

// 弹窗控制标记
const dialogFormVisible = ref(false)

// 打开弹窗
const openDialog = () => {
  type.value = 'create'
  dialogFormVisible.value = true
}

// 关闭弹窗
const closeDialog = () => {
  dialogFormVisible.value = false
  formData.value = {
    address: '',
    image: '',
    name: '',
  }
}
// 弹窗确定
const enterDialog = async () => {
  elFormRef.value?.validate(async (valid) => {
    if (!valid) return
    let res
    switch (type.value) {
      case 'create':
        res = await createCompany(formData.value)
        break
      case 'update':
        res = await updateCompany(formData.value)
        break
      default:
        res = await createCompany(formData.value)
        break
    }
    if (res.code === 0) {
      ElMessage({
        type: 'success',
        message: '创建/更改成功'
      })
      closeDialog()
      getTableData()
    }
  })
}

// 双击表格操作
function doubleClick(row, column, event) {
  console.log(row, column, event);
  // updateCompanyFunc(row);
  type.value = 'update';
  dialogFormVisible.value = true
}

// 导出excel
function exportExcel() {
  console.log('导出到excel');
}

// 生命周期钩子
onMounted(() => {
});
</script>

<style scoped>
.JKWTree-container {
  display: flex;
}

.JKWTree-tree {
  width: 300px;
  background-color: #fff;
  padding: 10px;
  margin-right: 20px;
}

.JKWTree-tree h3 {
  font-size: 15px;
  font-weight: 700;
  margin: 10px 0;
}

.JKWTree-content {
  flex: 1;
}

/* 选中某行时的背景色*/
::v-deep .el-table__body tr.current-row>td {
  background: #ff80ff !important;
}
</style>
