<script>
import { dicts } from "./dicts";
import useColumns from "./columns";
import { getDivisionList, delDivision } from "@/api/purchase/basic.js";
export default {
  name: "MaterialClassDivision",
  dicts: [...dicts],
  components: {
    ElSuperUxTable: () => import("@/components/super-ux-table/index.vue"),
    ElSuperSearch: () => import("@/components/super-search/index.vue"),
    Division: () => import("./details/index.vue"),
  },
  data() {
    const { TableColumns, SearchColumns } = useColumns();
    const page = this.$init.page();
    const params = this.$init.params(SearchColumns);

    return {
      size: "mini",
      loading: false,
      params: params,
      page: page,
      SearchColumns: SearchColumns,
      TableColumns: TableColumns,
      tableData: [],
      selectData: [],
    };
  },
  methods: {
    // 查询
    async useQuery(prop, page) {
      try {
        this.loading = true;
        let params = {
          ...prop,
          ...page,
        };
        let { code, rows, total } = await getDivisionList(params);
        if (code === 200) {
          this.tableData = rows;
          this.page.total = total;
        }
      } catch (error) {
      } finally {
        this.loading = false;
      }
    },
    // 重置
    useReset() {
      this.page = this.$init.page();
      this.params = this.$init.params(this.SearchColumns);
      this.useQuery(this.params, this.page);
    },
    // 新增
    openDivision(status, row) {
      let { setStatus, fetchItem } = this.$refs.Division;
      setStatus(true, status);
      row && fetchItem(row);
    },
    selectionChange(selection) {
      this.selectData = selection;
    },
    // 删除
    useDelete(row) {
      this.$modal
        .confirm("是否确认删除？")
        .then(async () => {
          try {
            let { code, msg } = await delDivision(row.id);
            if (code === 200) {
              this.$notify.success(msg);
              this.useQuery(this.params, this.page);
            }
          } catch (error) {
          } finally {
          }
        })
        .catch(() => {});
    },
  },
  created() {
    this.useQuery(this.params, this.page);
  },
};
</script>

<template>
  <el-card
    v-loading="loading"
    :body-style="{
      height: '100%',
      padding: 0,
      display: 'flex',
      'flex-direction': 'column',
    }"
  >
    <el-super-search
      v-model="params"
      :size="size"
      :dict="dict"
      :columns="SearchColumns"
      @reset="useReset"
      @submit="useQuery(params, page)"
    ></el-super-search>

    <el-row class="my-4" style="text-align: right">
      <el-button-group>
        <el-button type="primary" :size="size" @click="openDivision('add')"
          >新增</el-button
        >
      </el-button-group>
    </el-row>

    <el-super-ux-table
      v-model="tableData"
      :size="size"
      :dict="dict"
      :page="page"
      :columns="TableColumns"
      ref="MaterialClassDivision"
      index
      checkbox
      pagination
      convenitentOperation
      storage-key="MaterialClassDivisionSuperTable"
      @row-dblclick="(row) => openDivision('see', row)"
      @selection-change="selectionChange"
      @pagination="useQuery(params, page)"
    >
      <ux-table-column fixed="right" title="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button
            type="text"
            :size="size"
            @click="openDivision('see', scope.row)"
            >查看</el-button
          >
          <el-button
            type="text"
            :size="size"
            @click="openDivision('edit', scope.row)"
            >编辑</el-button
          >
          <el-button type="text" :size="size" @click="useDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </ux-table-column>
    </el-super-ux-table>

    <Division
      ref="Division"
      :dict="dict"
      :size="size"
      @success="useQuery(params, page)"
    ></Division>
  </el-card>
</template>

<style scoped lang="scss">
.el-card {
  width: calc(100% - 32px);
  height: calc(100vh - 32px);
  margin: 16px;
  padding: 16px;
  border-radius: 8px;
}

.el-button-group + .el-button-group {
  margin: 0 0 0 8px;
}
</style>
