<template>
  <div class="mainContentBox">
    <avue-crud
      ref="crudRef"
      v-model="form"
      v-model:page="page"
      v-model:search="search"
      :data="data"
      :option="option"
      @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 type="primary" @click="addTask">新增</el-button>
      </template>
      <template #menu="{ size, row, index }">
        <el-button @click="lookTask(row)" type="primary" link>查看</el-button>
        <el-button @click="updateTask(row)" type="primary" link>修改</el-button>
        <el-button @click="deleteTask(row.id)" type="danger" link
          >删除</el-button
        >
      </template></avue-crud
    >
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="850px"
      @close="dialog.visible = false"
    >
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        :rules="rules"
        label-width="auto"
        class="demo-ruleForm"
      >
        <el-form-item label="任务名称" prop="taskName">
          <el-input :disabled="lookStatus" v-model="ruleForm.taskName" />
        </el-form-item>
        <el-form-item label="物料名称" prop="materialName">
          <el-input
            :disabled="lookStatus"
            v-model="ruleForm.materialName"
            @click="open"
          />
        </el-form-item>
        <el-form-item label="物料编号" prop="materialNo">
          <el-input
            :disabled="lookStatus"
            v-model="ruleForm.materialNo"
            @click="open"
          />
        </el-form-item>
        <el-form-item label="是否启用" prop="state">
          <el-switch
            :disabled="lookStatus"
            v-model="ruleForm.state"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
      </el-form>

      <el-tabs v-model="activeName" class="demo-tabs">
        <el-tab-pane label="任务00" name="task0">
          <el-input :disabled="lookStatus" v-model="ruleForm.task0" />
        </el-tab-pane>
        <el-tab-pane label="任务01" name="task1">
          <el-input :disabled="lookStatus" v-model="ruleForm.task1"
        /></el-tab-pane>
        <el-tab-pane label="任务02" name="task2">
          <el-input :disabled="lookStatus" v-model="ruleForm.task2"
        /></el-tab-pane>
        <el-tab-pane label="任务03" name="task3">
          <el-input :disabled="lookStatus" v-model="ruleForm.task3"
        /></el-tab-pane>
        <el-tab-pane label="任务04" name="task4">
          <el-input :disabled="lookStatus" v-model="ruleForm.task4"
        /></el-tab-pane>
        <el-tab-pane label="任务05" name="task5">
          <el-input :disabled="lookStatus" v-model="ruleForm.task5"
        /></el-tab-pane>
        <el-tab-pane label="任务06" name="task6">
          <el-input :disabled="lookStatus" v-model="ruleForm.task6"
        /></el-tab-pane>
        <el-tab-pane label="任务07" name="task7">
          <el-input :disabled="lookStatus" v-model="ruleForm.task7"
        /></el-tab-pane>
      </el-tabs>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialog.visible = false">取消</el-button>
          <el-button v-if="!lookStatus" type="primary" @click="submit">
            确定
          </el-button>
        </div>
      </template>
    </el-dialog>
    <CommonTable
      ref="ctableRef"
      tableTitle="BOM添加"
      tableType="MARTERIAL"
      @selected-sure="onSelectedFinish"
    />
  </div>
</template>
<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import {
  addScrewdriverTask,
  updateScrewdriverTask,
  deleteScrewdriverTask,
} from "@/api/screwdriver";
const activeName = ref("task0");
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/screwdriverTask",
  });
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords;
const multipleRow = ref(true);
const ctableRef = ref(null);
const rules = {
  taskName: {
    required: true,
    trigger: "blur",
  },
  materialName: {
    required: true,
    trigger: "blur",
  },
  materialNo: {
    required: true,
    trigger: "blur",
  },
  state: {
    required: true,
    trigger: "blur",
  },
};
const submit = async () => {
  if (addStatus.value) {
    const { data, code } = await addScrewdriverTask({
      ...ruleForm.value,
    });
    dialog.visible = false;
    if (code == "200") {
      ElMessage.success("操作成功");
      dataList();
    }
  } else {
    const { data, code } = await updateScrewdriverTask({
      ...ruleForm.value,
    });
    if (code == "200") {
      ElMessage.success("操作成功");
      dataList();
    }
    dialog.visible = false;
  }
};
const open = () => {
  if (ctableRef.value) {
    ctableRef.value.startSelect();
  }
};
const onSelectedFinish = (selectedValueList) => {
  if (
    Object.keys(selectedValueList).length === 0 ||
    selectedValueList == undefined ||
    selectedValueList == null ||
    selectedValueList.length <= 0
  ) {
    ElMessage({
      message: "没有选择任何物料",
      type: "error",
    });
    return false;
  }
  ruleForm.value.materialName = selectedValueList.materialName;
  ruleForm.value.materialNo = selectedValueList.materialCode;
};
const dialog = reactive({
  title: "设备选择",
  visible: false,
});
const ruleForm = ref({
  materialName: "",
  materialNo: "",
  state: 0,
  taskName: "",
});
const addStatus = ref(false);
const lookStatus = ref(false);
const resetForm = () => {
  activeName.value = "task0";
  ruleForm.value = {};
};
const addTask = () => {
  resetForm();
  lookStatus.value = false;
  addStatus.value = true;
  dialog.title = "新增任务";
  dialog.visible = true;
};
const updateTask = (row) => {
  resetForm();
  lookStatus.value = false;
  addStatus.value = false;
  ruleForm.value = row;
  dialog.title = "编辑任务";
  dialog.visible = true;
};
const lookTask = (row) => {
  resetForm();
  addStatus.value = false;
  lookStatus.value = true;
  ruleForm.value = row;
  dialog.title = "查看任务";
  dialog.visible = true;
};
const deleteTask = async (id) => {
  const { code } = await deleteScrewdriverTask({ id });
  if (code == "200") {
    ElMessage.success("操作成功");
    dataList();
  }
};
option.value = Object.assign(option.value, {
  delBtn: false,
  addBtn: false,
  selection: false,
  labelWidth: 150,
  viewBtn: false,
  editBtn: false,
  column: [
    {
      label: "任务名称",
      prop: "taskName",
      addDisabled: true,
      search: true,
    },
    {
      label: "物料名称",
      prop: "creator",
      addDisabled: true,
      display: false,
    },
    {
      label: "物料编号",
      addDisabled: true,
      prop: "materialNo",
    },
    {
      label: "创建人",
      prop: "creator",
      display: false,
    },
    {
      label: "时间",
      prop: "created",
      display: false,
    },
  ],
});
onMounted(() => {
  dataList();
});
</script>
<style lang="scss" scoped></style>
