<template>
  <div class="mainContentBox">
    <avue-crud
      ref="crudRef"
      v-model:search="search"
      v-model="form"
      :data="data"
      :option="option"
      v-model:page="page"
      @size-change="dataList"
      @current-change="dataList"
      @row-save="createRow"
      @row-del="deleteRow2"
      @row-update="updateRow"
      @search-change="searchChange"
      @search-reset="resetChange"
      @selection-change="selectionChange"
      :before-open="onBeforeOpen"
    >
      <template #menu="{ row }">
        <el-button
          type="danger"
          link
          icon="el-icon-delete"
          v-if="row.workOrderCode !== '-'"
          @click="delRow(row)"
        >删除
        </el-button>
      </template>
      <template #checkBatch-form="scope">
        <div
          style="
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 20px;
          "
        >
          <el-input
            v-model="form.checkBatch"
            placeholder="请输入批号"
          ></el-input>
          <el-button type="primary" @click="addNumbers">新增单号</el-button>
        </div>
        <el-table
          v-show="tableData.length > 0"
          :data="tableData"
          style="width: 100%; margin-top: 20px"
        >
          <el-table-column prop="workOrderCode" label="工单编号" width="120" />
          <el-table-column prop="workOrderCode" label="工单编码" width="120" />
          <el-table-column prop="orderCode" label="订单编码" width="120" />
          <el-table-column prop="materialCode" label="物料编号" width="120" />
          <el-table-column prop="materialName" label="物料名称" width="120" />
          <el-table-column prop="materialModel" label="物料型号" width="120" />
          <el-table-column fixed="right" label="操作" min-width="120">
            <template #default="scope">
              <el-button
                link
                type="danger"
                size="small"
                @click.prevent="deleteTableRow(scope.$index)"
              >
                删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </avue-crud>
    <!--    <div class="tree-container">-->
    <!--      <el-table-->
    <!--        :data="data"-->
    <!--        row-key="id"-->
    <!--        border-->
    <!--        default-expand-all-->
    <!--        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"-->
    <!--      >-->
    <!--        <el-table-column prop="checkBatch" label="批次号" width="180" />-->
    <!--        <el-table-column prop="materialName" label="物料名称" />-->
    <!--        <el-table-column prop="workOrderCode" label="工单号" />-->
    <!--        <el-table-column prop="orderCode" label="订单号" />-->
    <!--        <el-table-column label="操作" width="150">-->
    <!--          <template #default="scope">-->
    <!--            <el-button-->
    <!--              size="small"-->
    <!--              type="danger"-->
    <!--              @click="handleDelete(scope.row)"-->
    <!--              :disabled="!scope.row.id"-->
    <!--            >-->
    <!--              删除-->
    <!--            </el-button>-->
    <!--          </template>-->
    <!--        </el-table-column>-->
    <!--      </el-table>-->
    <!--    </div>-->
    <CommonTable
      ref="productionLineRef"
      :multipleRow="multipleRow"
      tableTitle="选择工单"
      tableType="WORK_ORDERS"
      @selected-sure="onSelectedFinish"
    />
  </div>
</template>
<script setup lang="ts">
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";

// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/plan/batchNumber",
  });
const {dataList, createRow, updateRow, deleteRow, deleteRow2, searchChange, resetChange} =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具

const crudRef = ref(null); //crudRef.value 获取avue-crud对象

onMounted(() => {
  // console.log("crudRef", crudRef)
  dataList();
});
const handleDelete = (row: any) => {
  console.log("handleDelete");

}
const delRow = (row: any) => {
  Methords.deleteRow(row, () => {
    dataList();
  });

}
const multipleRow = ref(true);
const productionLineRef = ref(null);
const startCreat = () => {
  productionLineRef.value?.mergeOption({
    selection: true,
    reserveSelection: true,
  });
};
const addNumbers = () => {
  nextTick(() => {
    if (productionLineRef.value) {
      productionLineRef.value?.mergeOption({
        selection: true,
        reserveSelection: true,
      });
      productionLineRef.value.startSelect({
        queryType: "99"
      });
    }
  });
};

// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  searchEnter: false,
  selection: false,
  menuWidth: 200,
  border: true,
  index: false,
  delBtn: false,
  viewBtn: false,
  editBtn: false,
  rowKey: "id",
  column: [
    {
      label: "检验批号",
      prop: "checkBatch",
      search: true,
      overHidden: true,
      span: 24,
      rules: [
        {
          required: true,
          message: "请输入工厂批号",
          trigger: "blur",
        },
      ],
    },
    {
      label: "工单编码",
      prop: "workOrderCode",
      search: false,
      overHidden: true,
      span: 24,
      display: false,
    },
    {
      label: "订单编号",
      prop: "orderCode",
      search: false,
      overHidden: true,
      span: 24,
      display: false,
    },
    {
      label: "物料编码",
      prop: "materialCode",
      search: false,
      overHidden: true,
      span: 24,
      display: false,
    },
    {
      label: "物料型号",
      prop: "materialModel",
      search: false,
      overHidden: true,
      span: 24,
      display: false,
    },
    {
      label: "物料名称",
      prop: "materialName",
      search: false,
      overHidden: true,
      span: 24,
      display: false,
    },
    {
      label: "创建时间",
      prop: "created",
      display: false,
    },
  ],
});

const tableData = ref<any[]>([]); //选中的行
const onBeforeOpen = (done) => {
  console.log("before open");
  tableData.value = [];
  form.value.dtos = [];
  done();
};
const onSelectedFinish = (selectedValue) => {
  let toAddRow = JSON.parse(JSON.stringify(selectedValue));
  let tableData_ids = tableData.value.map((item) => item.id);
  toAddRow = toAddRow.filter((item) => {
    return tableData_ids.indexOf(item.id) === -1;
  });
  tableData.value.push(...toAddRow);
  form.value.dtos = tableData.value;
};
const deleteTableRow = (index: number) => {
  tableData.value.splice(index, 1);
  form.value.dtos = tableData.value;
};
</script>
