<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="{row,index,type}">
        <el-button @click="binding(row)"
                   icon="el-icon-link"
                   text
                   type="primary"
        >技能</el-button>
      </template>
      <template #menu-left="{ size }">
        <el-button
          :disabled="toDeleteIds.length < 1"
          type="danger"
          icon="el-icon-delete"
          :size="size"
          @click="multipleDelete"
          >删除</el-button
        >
      </template>
    </avue-crud>
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="800px"
      @close="dialog.visible = false"
      :destroy-on-close="true"
    >
      <post-skill  :postId="post.id" :dialog="dialog"/>
    </el-dialog>
    <ExcelUpload ref="uploadRef" @finished="uploadFinished" />
  </div>
</template>
<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import ButtonPermKeys from "@/common/configs/buttonPermission";

import { useCommonStoreHook } from "@/store";
import dictDataUtil from "@/common/configs/dictDataUtil";
const { isShowTable, tableType } = toRefs(useCommonStoreHook());
const test = () => {
  isShowTable.value = true;
  tableType.value = tableType.value == 1 ? 2 : 1;
};
const post=ref(null);
const binding=(row)=>{
  post.value=row;
  dialog.visible=true;
}
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/sys/post",
  });
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } = Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具

const crudRef = ref(null); //crudRef.value 获取avue-crud对象
const dialog = reactive({
  title: "技能",
  visible: false,
});
// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  delBtn: false,
  selection: true,
  column: [
    {
      label: "岗位组",
      prop: "postGroup",
      type: "select",
      search: true,
      dicUrl:
          dictDataUtil.request_url +
          dictDataUtil.TYPE_CODE.post_group,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      rules: [
        {
          required: true,
          message: "岗位组不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "岗位类别",
      prop: "postType",
      type: "select",
      search: true,
      dicUrl:
          dictDataUtil.request_url +
          dictDataUtil.TYPE_CODE.post_type,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      rules: [
        {
          required: true,
          message: "岗位类别不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "岗位编码",
      prop: "postCode",
      search: true,
      rules: [
        {
          required: true,
          message: "岗位编码不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "岗位名称",
      prop: "postName",
      rules: [
        {
          required: true,
          message: "岗位名称不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "状态",
      prop: "state",
      type: "select",
      dicData: [{label: '正常' , value: 0},{label: '禁用' , value: 1}],
      html: true,
      formatter: (val) => {
        if (val.state === 0) {
          return '<b class="el-tag el-tag--success el-tag--light">正常</b>';
        }
        return '<b class="el-tag el-tag--danger el-tag--light">禁用</b>';
      },
      addDisplay: false,
      rules: [
        {
          required: true,
          message: "状态不能为空",
          trigger: "trigger",
        },
      ],
    },
    {
      label: "备注",
      prop: "remark",
      width: "100",
      minRows: 2, //最小行/最小值
      type: "textarea", //类型为多行文本域框
      maxlength: 512, //最大输入长度
    },
    {
      label: "创建时间",
      prop: "created",
      display: false
    },
  ],
});

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/device/import");
  }
};
</script>
