<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="入库类型编码" prop="typeCode" class="form-item-no-wrap">
        <el-input
            v-model="queryParams.typeCode"
            placeholder="请输入入库类型编码"
            clearable
            @keyup.enter="handleQuery"
            style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="入库类型名称" prop="typeName" class="form-item-no-wrap">
        <el-input
            v-model="queryParams.typeName"
            placeholder="请输入入库类型名称"
            clearable
            @keyup.enter="handleQuery"
            style="width: 200px"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status" class="form-item-no-wrap">
        <el-select
            v-model="queryParams.status"
            placeholder="请选择状态"
            clearable
            style="width: 200px"
        >
          <el-option v-for="dict in statusList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['information:warehousingtype:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['information:warehousingtype:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['information:warehousingtype:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['information:warehousingtype:export']">导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="warehousingtypeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="入库类型编码" align="center" prop="typeCode" />
      <el-table-column label="入库类型名称" align="center" prop="typeName" />
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="dictTagData()" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="更新时间" align="center" prop="updatedAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['information:warehousingtype:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['information:warehousingtype: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-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="warehousingtypeRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="入库类型编码" prop="typeCode">
          <el-input v-model="form.typeCode" placeholder="请输入入库类型编码" />
        </el-form-item>
        <el-form-item label="入库类型名称" prop="typeName">
          <el-input v-model="form.typeName" placeholder="请输入入库类型名称" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option label="启用" :value="1" />
            <el-option label="禁用" :value="0" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.form-item-no-wrap {
  white-space: nowrap;
  margin-right: 10px;
  :deep(.el-form-item__label) {
    white-space: nowrap;
  }
}
</style>

<script setup name="Warehousingtype">
import { listWarehousingtype, getWarehousingtype, delWarehousingtype, addWarehousingtype, updateWarehousingtype } from "@/api/information/warehousingtype";

const { proxy } = getCurrentInstance();

const warehousingtypeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    typeCode: null,
    typeName: null,
    status: null
  },
  rules: {
    typeCode: [
      { required: true, message: "入库类型编码不能为空", trigger: "blur" }
    ],
    typeName: [
      { required: true, message: "入库类型名称不能为空", trigger: "blur" }
    ]
  }
});

const statusList = ref([
  { value: 0, label: '禁用', elTagType: "danger", elTagClass: null },
  { value: 1, label: '启用', elTagType: "primary", elTagClass: null }
])

const dictTagData = () => {
  return statusList.value.map(item => ({
    ...item,
    value: String(item.value)
  }));
}


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

function getList() {
  loading.value = true;
  listWarehousingtype(queryParams.value).then(response => {
    warehousingtypeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

function cancel() {
  open.value = false;
  reset();
}

function reset() {
  form.value = {
    id: null,
    typeCode: null,
    typeName: null,
    status: null,
    sort: null,
    remark: null,
    createdAt: null,
    updatedAt: null
  };
  proxy.resetForm("warehousingtypeRef");
}

function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加入库类型";
}

function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value;
  getWarehousingtype(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改入库类型";
  });
}

function submitForm() {
  proxy.$refs["warehousingtypeRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateWarehousingtype(form.value).then(() => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addWarehousingtype(form.value).then(() => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除入库类型编号为"' + _ids + '"的数据项？').then(() => {
    return delWarehousingtype(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

function handleExport() {
  proxy.download('information/warehousingtype/export', {
    ...queryParams.value
  }, `warehousingtype_${new Date().getTime()}.xlsx`);
}

getList();
</script>
