<template>
  <div class="mainContentBox">
    <avue-crud
      ref="crudRef"
      v-model:search="search"
      v-model="form"
      :data="data"
      :option="option"
      v-model:page="page"
      @row-save="createRow"
      @row-update="updateRow"
      @row-del="deleteRow"
      @search-change="searchChange"
      @search-reset="resetChange"
      @size-change="dataList"
      @current-change="dataList"
      @selection-change="selectionChange"
    >
      <template #menu-left="{ size }">
        <el-button
          :disabled="toDeleteIds.length < 1"
          type="danger"
          icon="el-icon-delete"
          :size="size"
          @click="multipleDelete"
          >删除</el-button
        >
      </template>
      <template #menu="{ row, index, type }">
        <el-button @click="binding(row)" text type="primary"
          >产能
          <template #icon>
            <svg-icon icon-class="channeng"></svg-icon>
          </template>
        </el-button>
      </template>
    </avue-crud>
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="900px"
      @close="dialog.visible = false"
      :destroy-on-close="true"
    >
      <capacity-page :productionLineId="productionLine.id" />
    </el-dialog>
  </div>
  <CommonTable
    ref="ctableRef"
    tableTitle="员工选择"
    tableType="USERS"
    @selected-sure="onSelectedFinish"
  />
</template>
<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import ButtonPermKeys from "@/common/configs/buttonPermission";
import { useCommonStoreHook } from "@/store";
const { isShowTable, tableType } = toRefs(useCommonStoreHook());

const test = () => {
  isShowTable.value = true;
  tableType.value = tableType.value == 1 ? 2 : 1;
};
const dialog = reactive({
  title: "产能",
  visible: false,
});
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/base/productionLine/list",
  });
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具
// checkBtnPerm(ButtonPermKeys.PLAN.BTNS.order_add) :permission="permission"
// const permission = reactive({
//   delBtn: checkPerm(buttonPermission.PLAN.BTNS.order_del),
//   addBtn: checkPerm(buttonPermission.PLAN.BTNS.order_add),
//   editBtn: checkPerm(buttonPermission.PLAN.BTNS.order_edit),
//   menu: true,
// });
const ctableRef = ref(null);
const crudRef = ref(null); //crudRef.value 获取avue-crud对象

onMounted(() => {
  // console.log("crudRef", crudRef)
  dataList();
});

/**
 * 上传excel相关
 */
const uploadRef = ref(null);
const uploadFinished = () => {
  // 上传完成后的刷新操作
  page.currentPage = 1;
  dataList();
};
const importExcelData = () => {
  if (uploadRef.value) {
    uploadRef.value.show("/api/v1/plan/order/import");
  }
};
const productionLine = ref(null);
const binding = (row) => {
  productionLine.value = row;
  dialog.visible = true;
};

const onSelectedFinish = (selectedValue) => {
  form.value.manager = selectedValue.userName;
};
// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  selection: true,
  column: [
    {
      label: "产线名称",
      prop: "name",
      search: true,
      overHidden: true,
      span: 24,
      rules: [
        {
          required: true,
          message: "请填写产线名称",
          trigger: "blur",
        },
      ],
    },
    {
      label: "负责人",
      prop: "manager",
      span: 24,
      rules: [
        {
          required: true,
          message: "请选择产线负责人",
          trigger: "blur",
        },
      ],
      click: ({ value, column }) => {
        ctableRef.value.startSelect();
      },
    },
    {
      label: "产线位置",
      prop: "position",
      span: 24,
      overHidden: true,
    },
    {
      label: "所属车间",
      prop: "workShopName",
      overHidden: true,
      display: false,
    },
    {
      label: "产线描述",
      overHidden: true,
      prop: "remark",
      type: "textarea",
      span: 24,
    },
    {
      label: "创建人",
      prop: "creator",
      display: false,
    },
    {
      label: "创建时间",
      prop: "created",
      overHidden: true,
      display: false,
    },
  ],
});
</script>
