<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-right="{}">
        <el-dropdown split-button
          >导入
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item
                @click="downloadTemplate('/api/v1/op/baseOperation/template')"
              >
                <i-ep-download />下载模板
              </el-dropdown-item>
              <el-dropdown-item @click="importExcelData">
                <i-ep-top />导入数据
              </el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
        <!-- <el-button
          class="ml-3"
          @click="exportData('/api/v1/plan/order/export')"
        >
          <template #icon> <i-ep-download /> </template>导出
        </el-button> -->
      </template>
    </avue-crud>
    <ExcelUpload ref="uploadRef" @finished="uploadFinished" />
  </div>
</template>
<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import dictDataUtil from "@/common/configs/dictDataUtil";
import ButtonPermKeys from "@/common/configs/buttonPermission";
import { useCommonStoreHook, useDictionaryStore } from "@/store";

// 数据字典相关
const { dicts } = useDictionaryStore();

// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/op/baseOperation",
  });
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 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/op/baseOperation/import");
  }
};

// 设置表格列或者其他自定义的option
const switchOp = [
  {
    label: "",
    value: 0,
  },
  {
    label: "",
    value: 1,
  },
];
option.value = Object.assign(option.value, {
  selection: true,
  column: [
    // {
    //   label: "工序OP号",
    //   prop: "operationOp",
    //   search: true,
    //   span: 12,
    //   dispaly: false,
    //   rules: [
    //     {
    //       required: true,
    //       message: "工序OP号不能为空",
    //       trigger: "blur",
    //     },
    //   ],
    // },
    {
      label: "工序名称",
      prop: "operationName",
      span: 12,
      search: true,
      rules: [
        {
          required: true,
          message: "工序名称不能为空",
          trigger: "blur",
        },
      ],
    },
    {
      label: "工序编码",
      prop: "operationCode",
      span: 12,
      search: true,
      rules: [
        {
          required: true,
          message: "工序编码不能为空",
          trigger: "blur",
        },
      ],
    },
    {
      label: "工段",
      prop: "workSection",
      type: "select",
      search: true,
      dicData: dicts.workshop_section,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      span: 12,
      rules: [
        {
          required: true,
          message: "请选择工段",
          trigger: "change",
        },
      ],
    },
    {
      label: "工序类型",
      prop: "operationType",
      type: "select",
      search: true,
      dicData: dicts.process_type,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      span: 12,
      rules: [
        {
          required: true,
          message: "请选择工序类型",
          trigger: "change",
        },
      ],
    },
    {
      label: "加工要求",
      prop: "processAsk",
      span: 12,
      rules: [
        {
          required: true,
          message: "加工要求不能为空",
          trigger: "blur",
        },
      ],
    },
    {
      label: "技能要求",
      prop: "skillAsk",
      span: 12,
      type: "select",
      search: true,
      filterable: true,
      dicData: dicts.skill_requirements,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      rules: [
        {
          required: true,
          message: "请选择技能要求",
          trigger: "change",
        },
      ],
    },
    {
      label: "人工工时",
      prop: "preparationTime",
      type: "number",
      span: 12,
      rules: [
        {
          required: true,
          message: "人工工时不能为空",
          trigger: "blur",
        },
      ],
    },
    /*{
      label: "标准机时",
      prop: "timeingNum",
      type: "number",
      span: 12,
      rules: [
        {
          required: true,
          message: "标准机时不能为空",
          trigger: "blur",
        },
      ],
    },*/
    {
      label: "标准工时",
      prop: "standardWorktime",
      type: "number",
      span: 12,
      rules: [
        {
          required: true,
          message: "标准工时不能为空",
          trigger: "blur",
        },
      ],
    },
    // {
    //   label: '岗位',
    //   prop: 'type',
    //   type: 'select',
    //   dicData: [
    //     {
    //       label: '岗位1',
    //       value: 'position1',
    //     },
    //   ],
    //   props: {
    //     label: 'label',
    //     value: 'value',
    //   },
    //   span: 12,
    // },
    {
      label: "工位类型",
      prop: "stanType",
      search: true,
      type: "select",
      dicData: dicts.station_type,
      props: {
        label: "dictLabel",
        value: "dictValue",
      },
      span: 12,
      row: true,
      rules: [
        {
          required: true,
          message: "请选择工位类型",
          trigger: "blur",
        },
      ],
    },
    {
      label: "是否可跳过",
      prop: "skipped",
      span: 8,
      hide: true,
      type: "switch",
      dicData: switchOp,
      value: 0,
    },
    {
      label: "是否禁用",
      prop: "enabled",
      hide: true,
      span: 8,
      type: "switch",
      dicData: switchOp,
      value: 0,
    },
    //  {
    //    label: "是否外协",
    //    prop: "externalCooperation",
    //    span: 8,
    // hide: true,
    //    type: "switch",
    //    dicData: switchOp,
    //    value: 0,
    //  },
    {
      label: "是否首检",
      prop: "firstCheck",
      span: 8,
      hide: true,
      type: "switch",
      dicData: switchOp,
      value: 0,
    },
    {
      label: "是否巡检",
      prop: "inspection",
      span: 8,
      hide: true,
      type: "switch",
      dicData: switchOp,
      value: 0,
    },
    {
      label: "是否自检",
      prop: "selfCheck",
      span: 8,
      hide: true,
      type: "switch",
      dicData: switchOp,
      value: 0,
    },
    //  {
    //    label: "是否工艺数量",
    //    prop: "common",
    //    span: 8,
    // hide: true,
    //    type: "switch",
    //    dicData: switchOp,
    //    value: 0,
    //  },
    //  {
    //    label: "是否分批",
    //    prop: "batch",
    //    span: 8,
    // hide: true,
    //    type: "switch",
    //    dicData: switchOp,
    //    value: 0,
    //  },
    {
      label: "是否合批",
      prop: "merge",
      span: 8,
      hide: true,
      type: "switch",
      dicData: switchOp,
      value: 0,
      //联动隐藏
      control: (val, form) => {
        return {
          mergeNum: {
            display: val == 1 ? true : false,
          },
        };
      },
    },
    {
      label: "是否委外",
      prop: "outsourcing",
      span: 8,
      hide: true,
      type: "switch",
      dicData: switchOp,
      // click: ({ column,value }) => {
      //  if(value == 0){
      // console.log('column',column)
      //  }
      // },
      //联动隐藏
      control: (val, form) => {
        return {
          outTime: {
            display: val == 1 ? true : false,
          },
        };
      },
      value: 0,
    },
    {
      label: "合批数量",
      prop: "mergeNum",
      span: 5,
      hide: true,
      display: false,
      value: 0,
    },
    {
      label: "分批数量",
      prop: "batchNum",
      span: 5,
      display: false,
      hide: true,
      value: 0,
      // slot:true, // 自定义列
    },
    {
      label: "委外时间",
      prop: "outTime",
      span: 5,
      display: false,
      hide: true,
      value: 0,
      // slot:true, // 自定义列
    },
    {
      label: "后置时间",
      prop: "forceTime",
      hide: true,
      span: 6,
      value: 0,
      rules: [
        {
          required: true,
          message: "后置时间不能为空",
          trigger: "blur",
        },
      ],
      // slot:true, // 自定义列
    },
    {
      label: "批量报工",
      prop: "batchReport",
      span: 24,
      type: "switch",
      dicData: switchOp,
      value: 0,
    },
    {
      label: "工序描述",
      prop: "operationDesc",
      hide: true,
      span: 24,
      type: "textarea",
    },
    /*{
      label: "备注",
      prop: "remark",
      hide: true,
      span: 24,
      type: "textarea",
    },*/
  ],
});
</script>
