<script>
import { dicts } from "./dicts";
import useColumns from "./columns";
import { listTargetTemplate } from "@/api/business/spd/starget/targetTemplate";

export default {
  name: "targettemplate",
  dicts: dicts,
  components: {
    SeeButton: () => import("./see/index.vue"),
    AddButton: () => import("./add/index.vue"),
    DeleButton: () => import("./delete/index.vue"),
    ElSuperTable: () => import("@/components/super-table/index.vue"),
    ElSuperSearch: () => import("@/components/super-search/index.vue"),
  },
  data() {
    const { TableColumns, SearchColumns } = useColumns();
    const params = this.$init.params(SearchColumns);
    return {
      size: "mini",
      loading: false,
      params: params,
      tableData: [],
      selectData: [],
      SearchColumns: SearchColumns,
      TableColumns: TableColumns,
      page: { pageNum: 1, pageSize: 10, total: 0 },
    };
  },
  created() {
    console.log('this.$refs',this.$refs);
    this.useQuery(this.params, this.page);
  },
  methods: {
    async fetchList(prop, page) {
      try {
        this.loading = true;
        const { code, rows, total } = await listTargetTemplate(
          {...prop,...page}
        );
        if (code === 200) {
          this.tableData = rows;
          this.page.total = total;
          console.log('this.tableData',this.tableData);
        }
      } catch (err) {
        // catch
        console.error(err);
      } finally {
        // finally
        this.loading = false;
      }
    },
    // 查 询
    useQuery(prop, page) {
      this.selectData = [];
      this.fetchList(prop, page);
    },
    // 重 置
    useReset() {
      this.page.pageNum = 1;
      this.page.pageSize = 10;
      this.params = this.$init.params(this.SearchColumns);
      this.useQuery(this.params, this.page);
    },
    // 选 择
    useSelect(prop) {
      this.selectData = prop;
    },
    // 明 细
    async useSee(prop) {
      const { open } = this.$refs.SeeButton;
      await open([prop]);
    },
  },
};
</script>

<template>
  <el-card v-loading="loading" :body-style="{ padding: 0 }">
    <el-super-search
      v-model="params"
      :size="size"
      :dict="dict"
      :columns="SearchColumns"
      @reset="useReset"
      @submit="useQuery(params, page)"
    ></el-super-search>
    <div style="margin: 0 0 20px 0; text-align: right">
      <el-button-group>
        <add-button
          :size="size"
          :select-data="[]"
          :dict="dict"
          add-type="add"
          @success="useQuery(params, page)"
        ></add-button>
      </el-button-group>
      <el-button-group>
        <see-button
          v-show="false"
          :size="size"
          :dict="dict"
          :model="params"
          :select-data="selectData"
          ref="SeeButton"
          @success="useQuery(params, page)"
        ></see-button>
      </el-button-group>
    </div>
    <el-super-table
      v-model="tableData"
      :size="size"
      :dict="dict"
      :columns="TableColumns"
      stroage
      hideOperationColumns
      @row-dblclick="useSee"
      @selection-change="useSelect"
    >
      <el-table-column fixed="right" label="操作" align="center" width="150">
        <template slot-scope="scope">
          <add-button
          :size="size"
          :dict="dict"
          :select-data="scope.row"
          add-type="edit"
          @success="useQuery(params, page)"
        ></add-button>
        <dele-button
          :size="size"
          :select-data="scope.row"
          @success="useQuery(params, page)"
        ></dele-button>
        </template>
      </el-table-column>
      <!-- <el-table-column fixed width="55" align="center" type="selection">
      </el-table-column> -->
    </el-super-table>
    <pagination
      :total="page.total"
      :page.sync="page.pageNum"
      :limit.sync="page.pageSize"
      @pagination="useQuery(params, page)"
      style="height: 32px; margin: 20px 0 0 0; padding: 0 !important"
    />
  </el-card>
</template>
<style scoped lang="scss">
.el-card {
  width: calc(100% - 20px);
  height: 100%;
  margin: 10px;
  padding: 20px;
}
.el-button-group + .el-button-group {
  margin: 0 0 0 10px;
}
</style>
