<template>
  <div class="p-2 h-full flex flex-col">
    <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="outNo">
              <el-input maxlength="20" v-model="queryParams.outNo" 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">
          <!-- <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-has-permi="['warehouse:add:issueout']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-has-permi="['system:stockOut:remove']"
              >删除</el-button
            >
          </el-col> -->
          <right-toolbar
            :disable-delete="multiple"
            :disable-edit="single"
            :show-add="checkPermi(['system:stockOut:add'])"
            :show-edit="false"
            :show-delete="checkPermi(['system:stockOut:remove'])"
            @update:add="handleAdd"
            @update:delete="handleDelete()"
          ></right-toolbar>
        </el-row>
      </template>
      <el-table v-loading="loading" border :data="stockOutList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" flexd width="55" align="center" />
        <el-table-column label="出库单号" flexd align="center" prop="outNo" />
        <el-table-column label="出库日期" width="100" align="center" prop="outDate" />
        <el-table-column label="领用人" align="center" prop="supplierName" />
        <el-table-column label="出库仓库" align="center" prop="warehouseName" />
        <el-table-column label="操作状态" width="80" align="center" prop="operateStatus">
          <template #default="scope">
            <dict-tag :options="com_stock_out_status" :value="scope.row.operateStatus"></dict-tag>
          </template>
        </el-table-column>
        <el-table-column label="负责人" show-overflow-tooltip align="center" prop="leader" />
        <el-table-column label="备注" show-overflow-tooltip align="center" prop="remark" />
        <el-table-column label="操作" width="180" align="center" fixed="right" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" @click="handleMain(scope.row)" v-has-permi="['system:stockOut:query']">查看清单</el-button>
            <el-button link type="primary" @click="handleDelete(scope.row)" v-has-permi="['system:stockOut:remove']">删除</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="StockOut" lang="ts">
import { listStockOut, delStockOut, addStockOut, updateStockOut } from '@/api/system/TissueOut/index';
import { StockOutVO, StockOutQuery, StockOutForm } from '@/api/system/TissueOut/type';
import { checkPermi } from '@/utils/permission';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { com_stock_out_status } = toRefs<any>(proxy?.useDict('com_stock_out_status'));

const stockOutList = ref<StockOutVO[]>([]);
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 stockOutFormRef = ref<ElFormInstance>();

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

const initFormData: StockOutForm = {
  id: undefined,
  outNo: undefined,
  outDate: undefined,
  supplierId: undefined,
  warehouseId: undefined,
  operateStatus: undefined,
  leader: undefined,
  remark: undefined
};
const data = reactive<PageData<StockOutForm, StockOutQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    outNo: undefined,
    outDate: undefined,
    supplierId: undefined,
    warehouseId: undefined,
    operateStatus: undefined,
    leader: undefined,
    params: {}
  },
  rules: {
    id: [{ required: true, message: '主键不能为空', trigger: 'blur' }],
    outNo: [{ required: true, message: '出库单号不能为空', trigger: 'blur' }],
    outDate: [{ required: true, message: '出库日期不能为空', trigger: 'blur' }],
    supplierId: [{ required: true, message: '领用人(供应商id)不能为空', trigger: 'blur' }],
    warehouseId: [{ required: true, message: '出库仓库id不能为空', trigger: 'blur' }]
  }
});

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

/** 查询出库主列表 */
const getList = async () => {
  loading.value = true;
  const res = await listStockOut(queryParams.value);
  stockOutList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};

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

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

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

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

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

const router = useRouter();
/** 新增按钮操作 */
const handleAdd = () => {
  router.push({
    path: '/warehouse/addissueOut'
  });
};

/** 修改按钮操作 */
const handleMain = async (row?: StockOutVO) => {
  router.push({
    path: '/warehouse/stockoutMain',
    query: {
      id: row.id
    }
  });
};
/** 提交按钮 */
const submitForm = () => {
  stockOutFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateStockOut(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addStockOut(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getList();
    }
  });
};

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

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download(
    'system/stockOut/export',
    {
      ...queryParams.value
    },
    `stockOut_${new Date().getTime()}.xlsx`
  );
};

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

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