<template>
  <div>
    <el-card class="box-card">
      <byTable
        :source="sourceList.data"
        :pagination="sourceList.pagination"
        :config="config"
        :loading="loading"
        :searchConfig="searchConfig"
        highlight-current-row
        :action-list="[
          {
            text: '添加仓库',
            action: () => clickModal(),
          },
        ]"
        @get-list="getList"
        @clickReset="clickReset">
      </byTable>
    </el-card>

    <el-dialog :title="modalType == 'add' ? '添加仓库' : '编辑仓库'" v-if="openDialog" v-model="openDialog" width="700">
      <byForm :formConfig="formConfig" :formOption="formOption" v-model="formData.data" :rules="rules" ref="submit"> </byForm>
      <template #footer>
        <el-button @click="openDialog = false" size="large">取 消</el-button>
        <el-button type="primary" @click="submitForm()" size="large" v-preReClick>确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import byTable from "/src/components/byTable/index";
import byForm from "/src/components/byForm/index";
import { ElMessage, ElMessageBox } from "element-plus";
import { ref } from "vue";

const { proxy } = getCurrentInstance();
const sourceList = ref({
  data: [],
  pagination: {
    total: 0,
    pageNum: 1,
    pageSize: 10,
    name: "",
    type: "",
  },
});
const loading = ref(false);
const searchConfig = computed(() => {
  return [
    {
      type: "input",
      prop: "name",
      label: "仓库名称",
    },
    {
      type: "select",
      prop: "type",
      label: "仓库类型",
      data: proxy.useUserStore().allDict["warehouse_type"],
    },
  ];
});
const config = computed(() => {
  return [
    {
      attrs: {
        label: "仓库类型",
        prop: "type",
        width: 180,
      },
      render(val) {
        return proxy.dictKeyValue(val, proxy.useUserStore().allDict["warehouse_type"]);
      },
    },
    {
      attrs: {
        label: "仓库名称",
        prop: "name",
        width: 220,
      },
    },
    {
      attrs: {
        label: "仓库说明",
        prop: "remark",
      },
    },
    {
      attrs: {
        label: "操作",
        width: 120,
        align: "center",
        fixed: "right",
      },
      renderHTML(row) {
        return [
          {
            attrs: {
              label: "编辑",
              type: "primary",
              text: true,
            },
            el: "button",
            click() {
              clickUpdate(row);
            },
          },
          // {
          //   attrs: {
          //     label: "删除",
          //     type: "danger",
          //     text: true,
          //   },
          //   el: "button",
          //   click() {
          //     clickDelete(row);
          //   },
          // },
        ];
      },
    },
  ];
});
const getList = async (req, status) => {
  if (status) {
    sourceList.value.pagination = {
      pageNum: sourceList.value.pagination.pageNum,
      pageSize: sourceList.value.pagination.pageSize,
    };
  } else {
    sourceList.value.pagination = { ...sourceList.value.pagination, ...req };
  }
  loading.value = true;
  proxy.post("/warehouse/page", sourceList.value.pagination).then((res) => {
    sourceList.value.data = res.rows;
    sourceList.value.pagination.total = res.total;
    setTimeout(() => {
      loading.value = false;
    }, 200);
  });
};
getList();
const clickReset = () => {
  getList("", true);
};
const modalType = ref("add");
const openDialog = ref(false);
const submit = ref(null);
const formOption = reactive({
  inline: true,
  labelWidth: "100px",
  itemWidth: 100,
  rules: [],
  labelPosition: "right",
});
const formData = reactive({
  data: {},
});
const formConfig = computed(() => {
  return [
    {
      type: "select",
      prop: "type",
      label: "仓库类型",
      data: proxy.useUserStore().allDict["warehouse_type"],
    },
    {
      type: "input",
      prop: "name",
      label: "仓库名称",
      itemType: "text",
    },
    {
      type: "input",
      prop: "remark",
      label: "仓库说明",
      itemType: "textarea",
    },
  ];
});
const rules = ref({
  type: [{ required: true, message: "请选择仓库类型", trigger: "change" }],
  name: [{ required: true, message: "请输入仓库名称", trigger: "blur" }],
});
const clickModal = () => {
  modalType.value = "add";
  formData.data = {};
  openDialog.value = true;
};
const submitForm = () => {
  submit.value.handleSubmit(() => {
    proxy.post("/warehouse/" + modalType.value, formData.data).then(() => {
      ElMessage({
        message: modalType.value == "add" ? "添加成功" : "编辑成功",
        type: "success",
      });
      openDialog.value = false;
      getList();
    });
  });
};
const clickUpdate = (row) => {
  modalType.value = "edit";
  proxy.post("/warehouse/detail", { id: row.id }).then((res) => {
    formData.data = res;
    openDialog.value = true;
  });
};
const clickDelete = (row) => {
  ElMessageBox.confirm("你是否确认此操作", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      proxy.post("/warehouse/delete", { id: row.id }).then(() => {
        ElMessage({ message: "删除成功", type: "success" });
        getList();
      });
    })
    .catch(() => {});
};
</script>

<style lang="scss" scoped>
::v-deep(.el-input-number .el-input__inner) {
  text-align: left;
}
</style>
