<template>
  <div>
    <!-- 左侧树形结构 -->
    <el-container>
      <el-aside width="250px">
        <div class="JKWTree-tree">
          <h3>包装计件单据维护</h3>
          <el-tree :data="treeData" :props="defaultProps" @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="输入工单编号或产品名称" />
                <div>{{ searchInfo }}</div>
              </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"
              @selection-change="handleSelectionChange" @row-dblclick="doubleClick">
              <el-table-column type="selection" width="55" />
              <el-table-column align="left" label="员工编号" prop="id" width="100" />
              <el-table-column align="left" label="员工姓名" prop="name" width="100" />
              <el-table-column align="left" label="生产日期" prop="date" width="100" />
              <el-table-column align="left" label="班组" prop="group" width="100" />
              <el-table-column align="left" label="包装产量" prop="packingOutput" width="100" />
              <el-table-column align="left" label="返工产量" prop="reworkOutput" width="100" />
              <el-table-column align="left" label="计件产量" prop="pieceOutput" width="100" />
              <el-table-column align="left" label="相关工单" prop="order" width="100" />
              <el-table-column align="left" label="创建用户" prop="user" width="100" />
              <el-table-column align="left" label="创建时间" prop="createTime" width="100" />
              <el-table-column align="left" label="修改时间" prop="updateTime" width="100" />
              <el-table-column align="left" label="UNIQID" prop="uniqid" width="100" />
            </el-table>
            <!-- 分页 -->
            <div class="gva-pagination">
              <el-pagination layout="total, sizes, prev, pager, next, jumper" :current-page="page" :page-size="pageSize"
                :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="type === 'create' ? '添加' : '修改'"
            destroy-on-close width="1200px">
            <!-- <el-scrollbar height="500px"> -->
            <el-form :model="formData" label-position="left" ref="elFormRef" :rules="rule">

              <el-row :gutter="20">
                <el-col :span="4">
                  <el-form-item label="日期" prop="id">
                    <el-input v-model="formData.address" :clearable="true" placeholder="2023.12.14" />
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="员工编号" prop="name">
                    <el-row :gutter="20">
                      <el-col :span="12">
                        <el-input v-model="formData.code" :clearable="true" placeholder="ZT01733" />
                      </el-col>
                      <el-col :span="12">
                        <el-input v-model="formData.name" :clearable="true" placeholder="张玉田" />
                      </el-col>
                    </el-row>
                  </el-form-item>
                </el-col>
                <el-col :span="3">
                  <el-form-item label="组别" prop="name">
                    <el-input v-model="formData.image" :clearable="true" placeholder="A班" />
                  </el-form-item>
                </el-col>
              </el-row>

              <el-row :gutter="20">
                <el-col :span="4">
                </el-col>
                <el-col :span="8">
                  <el-row :gutter="20">
                    <el-col :span="14">
                      <el-form-item label="计时时数" prop="id">
                        <el-input v-model="formData.address" :clearable="true" placeholder="0.00" />
                      </el-form-item>
                    </el-col>
                    <el-col :span="10">
                      <el-form-item label="冲月定额" prop="id">
                        <el-input v-model="formData.address" :clearable="true" placeholder="是" />
                      </el-form-item>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>
              <el-scrollbar height="300px">
                <el-table style="width: 100%" :data="detailData">
                  <el-table-column prop="gdbh" label="工单编号" width="80"></el-table-column>
                  <el-table-column prop="yjgx" label="印件工序" width="80"></el-table-column>
                  <el-table-column prop="gxmc" label="工序名称" width="100"></el-table-column>
                  <el-table-column prop="yjmc" label="印件名称"></el-table-column>
                  <el-table-column prop="bzcl" label="包装产量" width="80"></el-table-column>
                  <el-table-column prop="fgcl" label="返工产量" width="80"></el-table-column>
                  <el-table-column prop="mxsl" label="每箱数量" width="80"></el-table-column>
                  <el-table-column prop="jcxs" label="计产系数" width="80"></el-table-column>
                  <el-table-column prop="ly" label="来源" width="80"></el-table-column>
                  <el-table-column prop="dedh" label="定额代号" width="100"></el-table-column>
                </el-table>
              </el-scrollbar>

              <el-form-item label="其他备注" prop="id" style="margin-top: 10px;">
                <el-input v-model="formData.address" :clearable="true" placeholder="" />
              </el-form-item>

            </el-form>
            <!-- </el-scrollbar> -->
            <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'

defineOptions({
  name: '06-packingDocuments'
})

const treeData = reactive([
  {
    label: '2024.01.03【单据数: 102张】',
    children: [
      {
        label: '[1008/姜剑娟] 【记录数: 2张】',
      },
      {
        label: '[1013/洪丽] 【记录数: 100张】',
      }]
  },
  {
    label: '2024.01.02【单据数: 32张】',
    children: [
      {
        label: '[1008/姜剑娟] 【记录数: 3张】',
      },
      {
        label: '[1013/洪丽] 【记录数: 29张】',
      }]
  },
  {
    label: '2023.12.31【单据数: 24张】',
    children: [
      {
        label: '[1008/姜剑娟] 【记录数: 24张】',
      },]
  },
  {
    label: '2023.12.30【单据数: 44张】',
    children: [
      {
        label: '[1008/姜剑娟] 【记录数: 6张】',
      },
      {
        label: '[1013/洪丽] 【记录数: 38张】',
      }]
  },
  {
    label: '2023.12.29【单据数: 77张】',
    children: [
      {
        label: '[1013/洪丽] 【记录数: 77张】',
      }]
  },
  {
    label: '2023.12.27【单据数: 78张】',
    children: [
      {
        label: '[1008/姜剑娟] 【记录数: 2张】',
      },
      {
        label: '[1013/洪丽] 【记录数: 76张】',
      }]
  },
])
const handleNodeClick = (node, check) => {
  //存放当前节点的nodeId
  console.log(node, check);

}

// 自动化生成的字典（可能为空）以及字段
const formData = ref({
  id: '',
  name: '',
  date: '',
  group: '',
  packingOutput: '',
  reworkOutput: '',
  pieceOutput: '',
  order: '',
  user: '',
  createTime: '',
  updateTime: '',
  uniqid: '',
})

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

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

// =========== 表格控制部分 ===========
const tableData = reactive([
  // {
  //   id: 'ZM00326',
  //   name: '袁鲜红',
  //   date: '2023/12/1',
  //   group: 'A班',
  //   packingOutput: '165',
  //   reworkOutput: '0',
  //   pieceOutput: '165',
  //   order: '1801001',
  //   user: '[1013/洪丽]',
  //   createTime: '',
  //   updateTime: '',
  //   uniqid: '12345',
  // },
  {
    "id": "ZM00326",
    "name": "袁鲜红",
    "date": "2023-12-01",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12345
  },
  {
    "id": "ZM01834",
    "name": "崔玉坤",
    "date": "2023-12-01",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12346
  },
  {
    "id": "ZM00326",
    "name": "袁鲜红",
    "date": "2023-12-02",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12347
  },
  {
    "id": "ZM01834",
    "name": "崔玉坤",
    "date": "2023-12-02",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12348
  },
  {
    "id": "ZM00326",
    "name": "袁鲜红",
    "date": "2023-12-03",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12349
  },
  {
    "id": "ZM01834",
    "name": "崔玉坤",
    "date": "2023-12-03",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12350
  },
  {
    "id": "ZM00270",
    "name": "吴爱军",
    "date": "2023-12-03",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12351
  },
  {
    "id": "ZM00326",
    "name": "袁鲜红",
    "date": "2023-12-04",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12352
  },
  {
    "id": "ZM01834",
    "name": "崔玉坤",
    "date": "2023-12-04",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12353
  },
  {
    "id": "ZM00270",
    "name": "吴爱军",
    "date": "2023-12-04",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12354
  },
  {
    "id": "ZM00326",
    "name": "袁鲜红",
    "date": "2023-12-05",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12355
  },
  {
    "id": "ZM01834",
    "name": "崔玉坤",
    "date": "2023-12-05",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12356
  },
  {
    "id": "ZM00270",
    "name": "吴爱军",
    "date": "2023-12-05",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12357
  },
  {
    "id": "ZM00326",
    "name": "袁鲜红",
    "date": "2023-12-06",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12358
  },
  {
    "id": "ZM01834",
    "name": "崔玉坤",
    "date": "2023-12-06",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12359
  },
  {
    "id": "ZM00270",
    "name": "吴爱军",
    "date": "2023-12-06",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12360
  },
  {
    "id": "ZM00326",
    "name": "袁鲜红",
    "date": "2023-12-07",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12361
  },
  {
    "id": "ZM01834",
    "name": "崔玉坤",
    "date": "2023-12-07",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12362
  },
  {
    "id": "ZM00270",
    "name": "吴爱军",
    "date": "2023-12-07",
    "group": "A班",
    "packingOutput": 165,
    "reworkOutput": 0,
    "pieceOutput": 165.0,
    "order": 1801001.0,
    "user": "[1013/洪丽]",
    "createTime": "nan",
    "updateTime": "nan",
    "uniqid": 12363
  },
])
const detailData = reactive([
  {
    gdbh: '2311736',
    yjgx: '01-15',
    gxmc: '包装(圆切)',
    yjmc: '利群(新)小《降焦改版)',
    bzcl: '13.00',
    fgcl: '0.00',
    mxsl: '2500',
    jcxs: '1.000',
    ly: '',
    dedh: '030002002',
  },
  {
    gdbh: '2311736',
    yjgx: '01-15',
    gxmc: '包装(圆切)',
    yjmc: '利群(新)小《降焦改版)',
    bzcl: '13.00',
    fgcl: '0.00',
    mxsl: '2500',
    jcxs: '1.000',
    ly: '',
    dedh: '030002002',
  },
  {
    gdbh: '2311736',
    yjgx: '01-15',
    gxmc: '包装(圆切)',
    yjmc: '利群(新)小《降焦改版)',
    bzcl: '13.00',
    fgcl: '0.00',
    mxsl: '2500',
    jcxs: '1.000',
    ly: '',
    dedh: '030002002',
  },
  {
    gdbh: '2311736',
    yjgx: '01-15',
    gxmc: '包装(圆切)',
    yjmc: '利群(新)小《降焦改版)',
    bzcl: '13.00',
    fgcl: '0.00',
    mxsl: '2500',
    jcxs: '1.000',
    ly: '',
    dedh: '030002002',
  },
  {
    gdbh: '2311736',
    yjgx: '01-15',
    gxmc: '包装(圆切)',
    yjmc: '利群(新)小《降焦改版)',
    bzcl: '13.00',
    fgcl: '0.00',
    mxsl: '2500',
    jcxs: '1.000',
    ly: '',
    dedh: '030002002',
  },
  {
    gdbh: '2311736',
    yjgx: '01-15',
    gxmc: '包装(圆切)',
    yjmc: '利群(新)小《降焦改版)',
    bzcl: '13.00',
    fgcl: '0.00',
    mxsl: '2500',
    jcxs: '1.000',
    ly: '',
    dedh: '030002002',
  },
  {
    gdbh: '2311736',
    yjgx: '01-15',
    gxmc: '包装(圆切)',
    yjmc: '利群(新)小《降焦改版)',
    bzcl: '13.00',
    fgcl: '0.00',
    mxsl: '2500',
    jcxs: '1.000',
    ly: '',
    dedh: '030002002',
  },
  {
    gdbh: '2311736',
    yjgx: '01-15',
    gxmc: '包装(圆切)',
    yjmc: '利群(新)小《降焦改版)',
    bzcl: '13.00',
    fgcl: '0.00',
    mxsl: '2500',
    jcxs: '1.000',
    ly: '',
    dedh: '030002002',
  },
  {
    gdbh: '2311736',
    yjgx: '01-15',
    gxmc: '包装(圆切)',
    yjmc: '利群(新)小《降焦改版)',
    bzcl: '13.00',
    fgcl: '0.00',
    mxsl: '2500',
    jcxs: '1.000',
    ly: '',
    dedh: '030002002',
  },
  {
    gdbh: '2311736',
    yjgx: '01-15',
    gxmc: '包装(圆切)',
    yjmc: '利群(新)小《降焦改版)',
    bzcl: '13.00',
    fgcl: '0.00',
    mxsl: '2500',
    jcxs: '1.000',
    ly: '',
    dedh: '030002002',
  },
])
const page = ref(1)
const total = ref(0)
const pageSize = ref(10)
const searchInfo = ref('34234123')


// 分页
const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

// 修改页面容量
const handleCurrentChange = (val) => {
  page.value = val
  getTableData()
}

// 查询
const getTableData = async () => {
  const table = await getCompanyList({ page: page.value, pageSize: pageSize.value, ...searchInfo.value })
  if (table.code === 0) {
    tableData.value = table.data.list
    total.value = table.data.total
    page.value = table.data.page
    pageSize.value = table.data.pageSize
  }
}

getTableData()

// 搜索
function onSearch() {
  console.log('搜索信息: ' + searchInfo.value);
}

// ============== 表格控制部分结束 ===============

// 获取需要的字典 可能为空 按需保留
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(() => {
  console.log(tableData.length);
});
</script>

<style>
.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;
}
</style>
