<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="selectionChange1"
    >
      <template #menu="{size,row,index}">
        <el-button @click="deleteTep(row.id)" text type="primary" v-if ="info.workOrderState  === '1' || info.workOrderState  === '2' || info.workOrderState  === '0'"
        >删除</el-button
        >
      </template>
      <template #menu-left="{}">
        <el-button
          v-if ="info.workOrderState  === '1' || info.workOrderState  === '2' || info.workOrderState  === '0'"
          type="primary"
          icon="el-icon-plus"
          @click="addObj"
        >新增</el-button>
      </template>

    </avue-crud>
    <el-dialog
        v-model="editDialog.visible"
        :title="editDialog.title"
        width="650px"
        @close="handleClose"
    >
      <el-form
          ref="taskFormRef"
          status-icon
          :model="taskForm"
          :rules="rules"
          label-width="auto"
          class="demo-ruleForm">
        <el-form-item label="任务名称" prop="formName">
          <el-input v-model="taskForm.formName">
          </el-input>
        </el-form-item>
        <el-form-item label="Excel模板" prop="baseFormIds">
          <el-select v-model="taskForm.baseFormIds" multiple placeholder="Select">
            <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="handleClose">取消</el-button>
          <el-button type="primary" @click="onSelected"> 确定 </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script setup>
import {defineProps, ref} from "vue";
import { getForm, add, deleteTemp } from "@/api/excel";
import { useCrud } from "@/hooks/userCrud";
import { useCommonStoreHook,useDictionaryStore } from "@/store";
const { isShowTable, tableType } = toRefs(useCommonStoreHook());
const toPrintRef = ref(null);
const options = ref([]);
const test = () => {
  isShowTable.value = true;
  tableType.value = tableType.value == 1 ? 2 : 1;
};
const taskFormRef = ref();
const taskForm = ref({
  formName: "",
  baseFormIds: "",
  workOrderId: ""

});
const rules = reactive({
  formName: [{ required: true, trigger: "blur" }],
  baseFormIds: [{ required: true, trigger: "blur" }],
});
const editDialog = ref({ visible: false, title: "绑定模版" });
const value1 = ref([]);
const info = ref({})
const props = defineProps({
  workOrderInfo: {
    type: Object,
    default: () => {
      return null;
    }
  }
})
const handleClose = () => {
  editDialog.value.visible = false;
  value1.value = [];
};
const dialog = reactive({
  title: "二维码打印",
  visible: false,
});
const clickObjs = ref([])
const selectionChange1 =(row)=>{
  toDeleteIds.value = [];
  row?.forEach((element) => {
    toDeleteIds.value.push(element.id);
  });
  clickObjs.value = row
}
const crudRef = ref(null); //crudRef.value 获取avue-crud对象
const addObj =()=>{
  editDialog.value.visible = true
}
const deleteTep = async (id) => {
  const { data, code } = await deleteTemp({
    id,
  });
  if (code == "200") {
    ElMessage.success("删除成功！");
    dataList();
  }
};
//新增
const onSelected = () => {
  taskFormRef.value.validate((valid) => {
    if (valid) {
      add(taskForm.value).then((data)=>{
        if (data.code == "200") {
          ElMessage.success("添加成功！");
          dataList();
          handleClose();
          taskForm.value.baseFormIds = []
          taskForm.value.formName = ''
        }else{
          ElMessage.error(data.msg);
        }
      })
    }
  });
};
//获取Excel模版列表
const getTemplate = async () => {
  const { data } = await getForm({
    pageNo: 1,
    pageSize: 9999999,
    queryCheck: true
  });
  options.value = [];
  data.records.forEach((element) => {
    options.value.push({
      value: element.id,
      label: element.formName,
    });
  });
};
watch(
    () => props.workOrderInfo,
    () => {
      form.value.workOrderCode = props.workOrderInfo.workOrderCode
      info.value = props.workOrderInfo
      search.value.workOrderCode = props.workOrderInfo.workOrderCode
      dataList();
    }
);
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/opExcelForm",
  });
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } = Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具
const { dicts } = useDictionaryStore();


// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  delBtn: false,
  selection: false,
  search: false,
  filterBtn: false,
  columnBtn: false,
  editBtn: false,
  addBtn: false,
  viewBtn: false,
  menu: true,
  column: [
    {
      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,
    },
  ],
});

onMounted(() => {
  info.value = props.workOrderInfo
  taskForm.value.workOrderId = info.value.id
  form.value.workOrderId = props.workOrderInfo.id
  search.value.workOrderId = props.workOrderInfo.id
  dataList();
  getTemplate()
});
</script>
