<template>

  <div class="mainContentBox">
    <avue-form
      ref="formRef"
      v-model="form"
      :data="data2"
      :option="option2"
    >
    </avue-form>
    <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"
      @selection-change="selectionChanges"
    >



    </avue-crud>

  </div>
  <div style="overflow: hidden">
    <div class="detail-footer">
      <el-button type="primary" @click="onHandle"> 确定 </el-button>
      <el-button @click="cancelWindow">取消</el-button>

    </div>
  </div>
  <CommonTable
    ref="ctableRef"
    tableTitle="员工选择"
    tableType="USERS"
    @selected-sure="onSelectedFinish"
  />

</template>
<script setup lang="ts">
  import { ref, getCurrentInstance } from "vue";
  import { useCrud } from "@/hooks/userCrud";
  import ButtonPermKeys from "@/common/configs/buttonPermission";
  import { useCommonStoreHook, useDictionaryStore } from "@/store";
  const { isShowTable, tableType } = toRefs(useCommonStoreHook());
  import {addStock} from "@/api/inStock"
  import dictDataUtil from "@/common/configs/dictDataUtil";
  // 数据字典相关

  const test = () => {
    isShowTable.value = true;
    tableType.value = tableType.value == 1 ? 2 : 1;
  };
  const props = defineProps({
    workOrderCode: {
      type: String,
      default: () => {
        return 0;
      }
    },
    stockDialog:{
      type:Object,
      default:()=>{
        return {};
      }
    }

  })
  const ctableRef=ref(null);
  const crudRef=ref(null);
  // 传入一个url，后面不带/
  const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
    useCrud({
      src: "/api/v1/plan/seq",
    });
  const { dataNoPageList, 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 cancelWindow=()=>{
    props.stockDialog.visible=false;
  }

  const onHandle=()=>{
    if(form.value.recipient==undefined||form.value.recipient==null||form.value.recipient.trim()==""){
      ElMessage({
        message: "未输入 接收人",
        type: "error",
      });
      return false;
    }
    if(seqList.value.length==0){
      ElMessage({
        message: "未选择要入库的流转卡号",
        type: "error",
      });
      return false;
    }

    form.value.seqList=seqList.value;
    form.value.workOrderCode=props.workOrderCode;
    addStock(form.value).then(
      (data)=>{

        if(data.code=="200"){
          ElMessage({
            message: data.msg,
            type: "success",
          });
          props.stockDialog.visible=false;
        }else {
          ElMessage({
            message: data.msg,
            type: "error",
          });
        }

      }
    )


  }
  onMounted(() => {
    // console.log("crudRef", crudRef)
    search.value.workOrderCode=props.workOrderCode;
    //未入库
    search.value.isStock="0";
    //已完工
    search.value.state="2";
    dataNoPageList();


  });
  const seqList=ref([]);
  // 设置selection: true,后监听选中改变事件，将Id存入数组
  const selectionChanges=(rows) => {
    seqList.value=[];
    rows?.forEach((element) => {
      seqList.value.push({seqId:element.id,seqNo:element.seqNo});
    });

  }
  const onSelectedFinish=(selectedValue)=>{
    form.value.recipient=selectedValue.userName;
  }

  // 设置表格列或者其他自定义的option
  option.value = Object.assign(option.value, {
    selection: true,
    addBtn: false,
    editBtn:false,
    delBtn:false,
    viewBtn:false,
    menu:false,
    columnBtn: false,
    gridBtn: false,
    column: [
      {
        label: "流转卡号",
        prop: "seqNo",
      },
    ],
  });
  const option2=ref({});
  const data2=ref({});
  // 设置表格列或者其他自定义的option
  option2.value = {
    addBtn: false,
    editBtn:false,
    delBtn:false,
    viewBtn:false,
    menu:false,
    submitBtn:false,
    emptyBtn:false,
    columnBtn: false,
    gridBtn: false,
    refreshBtn:false,
    column: [
      {
        label: "接收人",
        prop: "recipient",
        span:24,
        click: ({ value, column }) => {
          ctableRef.value.startSelect();
        },
        rules: [
          {
            required: true,
            message: "请输入接收人",
            trigger: "blur",
          },
        ],
      },
      {
        label: "备注",
        prop: "remark",
        span:24,
        type: "textarea",
      },

    ],
  };


</script>

<style>
  .avue-crud__header{min-height: 0px;}
  .detail-footer{
    float: right;
  }
</style>
