<template>
  <div>
    <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-right="{}">
        <el-button
          class="ml-3"
          @click="exportData('/api/v1/plan/order/export')"
        >
          <template #icon> <i-ep-download /> </template>导出
        </el-button>
      </template> -->
      <template #menu-left="{}">
        <el-button
          type="primary"
          @click="editDialog.visible = true"
          style="margin-bottom: 15px"
          >新增 / 编辑</el-button
        >
      </template>
      <template #menu="{ row, index, type }">
        <el-button @click="deleteTep(row.id)" text type="primary"
          >删除</el-button
        >
      </template>
    </avue-crud>
    <el-dialog
      v-model="editDialog.visible"
      :title="editDialog.title"
      width="1200px"
      @close="handleClose"
    >
      <div class="dialog">
        <div>
          请选择Excel模板:
          <el-select v-model="value1" multiple placeholder="Select">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </div>

        <div class="btns">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="onSelected"> 确定 </el-button>
        </div>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import { ref } from "vue";
import { useCrud } from "@/hooks/userCrud";
import { useDictionaryStore } from "@/store";
import { useRoute } from "vue-router";
import { getForm, add, deleteTemp } from "@/api/excel";
const value1 = ref([]);
const options = ref([]);
const route = useRoute();
//删除
const deleteTep = async (id) => {
  const { data, code } = await deleteTemp({
    id,
  });
  if (code == "200") {
    ElMessage.success("删除成功！");
    dataList();
    setValue1();
  }
};
//新增
const onSelected = async () => {
  const { data, code } = await add({
    baseFormIds: value1.value,
    operationId: route.params.id,
  });
  if (code == "200") {
    ElMessage.success("添加成功！");
    dataList();
    handleClose();
  }
};
//关闭弹窗
const handleClose = () => {
  editDialog.value.visible = false;
  value1.value = [];
};
// 数据字典相关
const { dicts } = useDictionaryStore();

// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/opExcelForm",
  });
// const setValue1 = () => {
//   value1.value = [];
//   data.value.forEach((el) => {
//     value1.value.push(el.id);
//   });
// };
search.value = { ...search.value, operationId: route.params.id };
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具
const editDialog = ref({ visible: false, title: "绑定模版" });
const crudRef = ref(null); //crudRef.value 获取avue-crud对象
//获取Excel模版列表
const getTemplate = async () => {
  const { data } = await getForm({
    pageNo: 1,
    pageSize: 9999999,
    formType: 0
  });
  options.value = [];
  data.records.forEach((element) => {
    options.value.push({
      value: element.id,
      label: element.formName,
    });
  });
};
onMounted(() => {
  getTemplate();
  dataList();
});
// watch(
//   () => data.value,
//   (val) => {
//     setValue1();
//   },
//   {
//     immediately: true,
//   }
// );

option.value = Object.assign(option.value, {
  selection: false,
  addBtn: false,
  menu: true,
  viewBtn: false,
  editBtn: false,
  delBtn: false,
  addBtn: false,
  searchMenuSpan: 8,
  column: [
    {
      label: "工序id",
      prop: "operationId",
      display: false,
      hide: true,
    },
    {
      label: "模版名称",
      prop: "formName",
      addDisabled: true,
      editDisabled: true,
    },
    {
      label: "模版类型",
      prop: "formType",
      addDisabled: true,
      editDisabled: true,
      dicData: dicts.excel_type,
      props: { label: "dictLabel", value: "dictValue" },
    },
    {
      label: "修改时间",
      prop: "updated",
      addDisabled: true,
      editDisabled: true,
    },
    {
      label: "操作人",
      prop: "updator",
      addDisabled: true,
      editDisabled: true,
    },
  ],
});
</script>
<style lang="scss" scoped>
.dialog {
  width: 100%;
  height: 120px;
  position: relative;
  .btns {
    position: absolute;
    right: 0;
    bottom: 0;
  }
}
</style>
