<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"
      @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 v-if="row.level === 'first'" @click="openEdit(row)" icon="el-icon-edit" text type="primary"
        >编辑</el-button
        >
        <el-button @click="deleteRow(row,null,null)" icon="el-icon-delete" text type="danger"
        >删除</el-button
        >
        <el-button @click="binding(row)" v-if="row.level === 'first' || row.level === 'second' || row.level === 'third'" icon="el-icon-link" text type="primary"
          >绑定子项</el-button
        >
      </template>
    </avue-crud>

    <CommonTable
      ref="ctableRef"
      tableTitle="绑定子项"
      tableType="FACTORY"
      @selected-sure="onSelectedFinish"
    />
    <CommonTable
      ref="workShopRef"
      tableTitle="绑定子项"
      tableType="PRODUCTIONLINE"
      @selected-sure="onSelectedLineFinish"
    />
    <CommonTable
      ref="productionLineRef"
      tableTitle="绑定子项"
      multiple=true
      tableType="STATION"
      @selectMultipleSure="onSelectedStationFinish"
    />
    <CommonTable
      ref="userRef"
      tableTitle="员工选择"
      tableType="USERS"
      @selected-sure="onUserSelectedFinish"
    />
    <ExcelUpload ref="uploadRef" @finished="uploadFinished" />
  </div>
</template>
<script setup lang="ts">
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import {useCommonStoreHook, useUserStoreHook} from "@/store";
const { isShowTable, tableType } = toRefs(useCommonStoreHook());
import { updateWorkShop } from "@/api/workShop";

import { updateProductionLine } from "@/api/productionLine";
import {bindStation, updateStation} from "@/api/station";
// 公共弹窗相关
const ctableRef = ref(null);
const workShopRef = ref(null);
const productionLineRef = ref(null);
const factory = ref({});
const binding = (row) => {
  factory.value = row;
  if (row.level === "first") {
    ctableRef.value.startSelect();
  }
  if (row.level === "second") {
    workShopRef.value.startSelect();
  }
  if (row.level === "third") {
    productionLineRef.value.startSelect();
  }
};
const userRef=ref(null);
const openEdit = (row) =>{
  crudRef.value && crudRef.value.rowEdit(row,null);
}
const deletes = (row) => {
  ElMessageBox.confirm("您确定要删除选择的数据吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(() => {
    if(row.children && row.children.length > 0){
      ElMessage.error("请先删除下级绑定");
      return;
    }
    /*if (row.level === "first") {
      deleteRow(row, index, done);
    }
    if (row.level === "second") {
      workShopRef.value.startSelect();
    }
    if (row.level === "third") {
      productionLineRef.value.startSelect();
    }*/
  });
};
const dialog = reactive({
  title: "绑定子项",
  visible: false,
});

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

const onUserSelectedFinish=(selectedValue)=>{
  form.value.manager=selectedValue.userName;
}
const formData = ref({});
// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  selection: false,
  menuWidth: 320,
  border: true,
  index: true,
  delBtn: false,
  viewBtn: false,
  editBtn: false,
  rowKey: "code",
  rowParentKey: "parentCode",
  column: [
    {
      label: "厂区名称",
      prop: "name",
      search: true,
      overHidden: true,
      span: 24,
      labelWidth: 100,
      rules: [
        {
          required: true,
          message: "请输入工厂名称",
          trigger: "blur",
        },
      ],
    },
    {
      label: "负责人",
      prop: "manager",
      span: 24,
      labelWidth: 100,
      rules: [
        {
          required: true,
          message: "请选择厂区负责人",
          trigger: "blur",
        },
      ],
      click: ({ value, column }) => {
        userRef.value.startSelect();
      },
    },
    {
      label: "厂区位置",
      prop: "position",
      overHidden: true,
      span: 24,
      labelWidth: 100,
      rules: [
        {
          required: true,
          message: "请输入厂区位置",
          trigger: "blur",
        },
      ],
    },
    {
      label: "创建时间",
      prop: "created",
      display: false,
    },
    {
      label: "备注",
      span: 24,
      labelWidth: 100,
      overHidden: true,
      prop: "remark",
      type: "textarea",
    },
  ],
});

const onSelectedFinish = (selectedValue) => {
  formData.value.code = selectedValue.code;
  formData.value.parentCode = factory.value.code;
  formData.value.id = selectedValue.id;
  updateWorkShop(formData.value).then((data: any) => {
    ElMessage({
      message: data.msg,
      type: "success",
    });
    // 上传完成后的刷新操作
    page.currentPage = 1;
    dataList();
  });
};
const onSelectedLineFinish = (selectedValue) => {
  formData.value.code = selectedValue.code;
  formData.value.parentCode = factory.value.code;
  formData.value.id = selectedValue.id;
  updateProductionLine(formData.value).then((data: any) => {
    ElMessage({
      message: data.msg,
      type: "success",
    });
    // 上传完成后的刷新操作
    page.currentPage = 1;
    dataList();
  });
};
const onSelectedStationFinish = (selectedValue) => {
  formData.value.parentCode = factory.value.code;
  formData.value.ids = selectedValue
  bindStation(formData.value).then((data: any) => {
    ElMessage({
      message: data.msg,
      type: "success",
    });
    // 上传完成后的刷新操作
    page.currentPage = 1;
    dataList();
  });
};
</script>
