<template>
  <div class='p-2'>
    <transition :enter-active-class='proxy?.animate.searchAnimate.enter'
                :leave-active-class='proxy?.animate.searchAnimate.leave'>
      <div v-show='showSearch' class='mb-[10px]'>
        <el-card shadow='hover'>
          <el-form ref='queryFormRef' :model='queryParams' :inline='true'>
            <el-form-item label='物料大类' prop='materialCategories'>
              <el-input v-model='queryParams.materialCategories' placeholder='请输入物料大类' clearable
                        @keyup.enter='handleQuery' />
            </el-form-item>
            <el-form-item label='物料编码' prop='materialCode'>
              <el-input v-model='queryParams.materialCode' placeholder='请输入物料编码' clearable
                        @keyup.enter='handleQuery' />
            </el-form-item>
            <el-form-item label='物料名称' prop='materialName'>
              <el-input v-model='queryParams.materialName' placeholder='请输入' clearable
                        @keyup.enter='handleQuery' />
            </el-form-item>
            <el-form-item>
              <el-button type='primary' icon='Search' @click='handleQuery'>搜索</el-button>
              <el-button icon='Refresh' @click='resetQuery'>重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow='never'>
      <el-table v-loading='loading' :data='outstockDetailList' @selection-change='handleSelectionChange' ref="tableRef"
                @row-click="handleRowClick"
                highlight-current-row>
<!--        <el-table-column type='selection' width='55' align='center' />-->
        <el-table-column label='表主键' align='center' prop='outstockDetailId' v-if='columns[0].visible' />
        <el-table-column label='出库单号' align='center' prop='outstockCode' v-if='columns[1].visible' />
        <el-table-column label='出库单 主键' align='center' prop='outstockId' v-if='columns[2].visible' />
        <el-table-column label='物料ID' align='center' prop='materialId' v-if='columns[4].visible' />
        <el-table-column label='物料大类' align='center' prop='materialCategoryName' v-if='columns[6].visible' />
        <el-table-column label='物料编码' align='center' prop='materialCode' v-if='columns[7].visible' />
        <el-table-column label='物料名称' align='center' prop='materialName' v-if='columns[8].visible' />
        <el-table-column label='出库数量' align='center' prop='outstockQty' v-if='columns[5].visible' />
      </el-table>

      <pagination v-show='total > 0' :total='total' v-model:page='queryParams.pageNum'
                  v-model:limit='queryParams.pageSize' @pagination='getList' />
    </el-card>


  </div>
</template>

<script setup name='OutstockDetail' lang='ts'>
import {
  listOutstockDetail,
} from '@/api/wms/outstockDetail';
import { OutstockDetailVO, OutstockDetailQuery, OutstockDetailForm } from '@/api/wms/outstockDetail/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;

const outstockDetailList = ref<OutstockDetailVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const outstockDetailFormRef = ref<ElFormInstance>();

const selectedRow = ref({});
const tableRef = ref()

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const emits = defineEmits(['selection']);  // 定义事件

// 列显隐信息
const columns = ref<FieldOption[]>([
  { key: 0, label: `表主键`, visible: false },
  { key: 1, label: `出库单号`, visible: true },
  { key: 2, label: `出库单 主键`, visible: false },
  { key: 3, label: `创建时间`, visible: false },
  { key: 4, label: `物料id`, visible: true },
  { key: 5, label: `出库数量`, visible: true },
  { key: 6, label: `物料大类`, visible: false },
  { key: 7, label: `物料编码`, visible: true },
  { key: 8, label: `物料名称`, visible: true },
]);

const initFormData: OutstockDetailForm = {
  outstockDetailId: undefined,
  outstockCode: undefined,
  outstockId: undefined,
  materialId: undefined,
  outstockQty: undefined,
  outSum: undefined,
  isAgv: undefined,
  completeQty: undefined,
  agvStatus: undefined
};
const data = reactive<PageData<OutstockDetailForm, OutstockDetailQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    outstockDetailId: undefined,
    outstockCode: undefined,
    outstockId: undefined,
    materialId: undefined,
    outstockQty: undefined,
    outSum: undefined,
    isAgv: undefined,
    completeQty: undefined,
    agvStatus: undefined,
    params: {}
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询出库单-物料列表 */
const getList = async () => {
  loading.value = true;
  const res = await listOutstockDetail(queryParams.value);
  outstockDetailList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};

/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  outstockDetailFormRef.value?.resetFields();
};

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};

/** 多选框选中数据 */
const handleSelectionChange = (selection: OutstockDetailVO[]) => {
  ids.value = selection.map(item => item.outstockDetailId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

/** 选中列赋值 */
const handleRowClick = (row) => {
  selectedRow.value = row;
  // 触发选择事件，将完整的行数据传递给父组件
  emits('selection', row);
};
defineExpose({tableRef})

onMounted(() => {
  getList();
});
</script>
