<template>
  <div class="flex flex-col h-full p-2">
    <pageHeader></pageHeader>
    <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" @submit.prevent="handleQuery" :inline="true">
            <el-form-item label="品牌名称" prop="brand">
              <el-input maxlength="20" v-model="queryParams.brand" placeholder="请输入品牌" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="型号名称" prop="model">
              <el-input maxlength="20" v-model="queryParams.model" placeholder="请输入型号" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="物料名称" prop="materialName">
              <el-input maxlength="20" v-model="queryParams.materialName" placeholder="请输入物料名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleQuery">搜索</el-button>
              <el-button @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card class="flex-1" shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <right-toolbar
            :disable-delete="multiple"
            :disable-edit="single"
            :show-add="false"
            :show-edit="false"
            :show-delete="false"
            @update:add="handleAdd"
            @update:edit="handleUpdate()"
            @update:delete="handleDelete()"
          ></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" border :data="materialList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="物料名称" align="center" prop="materialName" />
        <el-table-column label="品牌" align="center" prop="brand" />
        <el-table-column label="规格型号" align="center" prop="model" />
        <el-table-column label="单位" width="60" align="center" prop="unit" />
        <el-table-column label="成本" width="100" align="center" prop="costPrice" />
        <el-table-column label="当前库存" width="80" align="center" prop="totalStockNum" />
        <el-table-column label="备注" show-overflow-tooltip align="center" prop="remark" />
        <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleUpdate(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </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="Material" lang="ts">
import pageHeader from '@/components/Pageheader/index.vue';
import Pagination from '@/components/Pagination/index.vue';
import RightToolbar from '@/components/RightToolbar/index.vue';

import { delMaterial } from '@/api/system/Tmaterial';
import { MaterialVO, MaterialQuery, MaterialForm } from '@/api/system/Tmaterial/type';
import { listStock } from '@/api/system/Tstock';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const router = useRouter();
const materialList = ref<MaterialVO[]>([]);
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 initFormData: MaterialForm = {
  id: undefined,
  materialName: undefined,
  brand: undefined,
  model: undefined
};
const data = reactive<PageData<MaterialForm, MaterialQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    materialName: undefined,
    brand: undefined,
    model: undefined
  }
});

const { queryParams } = toRefs(data);

/** 查询物料列表 */
const getList = async () => {
  loading.value = true;
  const res = await listStock(queryParams.value);
  materialList.value = res.rows;
  total.value = res.total ?? 0;
  loading.value = false;
};

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

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

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

const handleAdd = () => {
  router.push({
    path: '/warehouse/purchaseIn'
  });
};
/** 修改按钮操作 */
const handleUpdate = (row?: MaterialVO) => {
  const _ids = row?.materialId;
  router.push({
    path: '/warehouse/editInventory',
    query: {
      id: _ids
    }
  });
};

/** 删除按钮操作 */
const handleDelete = async (row?: MaterialVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除该物料？').finally(() => (loading.value = false));
  await delMaterial(_ids);
  proxy?.$modal.msgSuccess('删除成功');
  await getList();
};

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

<style lang="scss" scoped>
.el-table {
  height: calc(100% - 80px);
}
</style>
