<template>
  <div class="common-box">
    <!--条件-->
    <el-form :model="listQuery" ref="queryForm" :inline="true">
      <el-form-item label="模板名称">
        <el-input
          v-model="listQuery.tmplName"
          placeholder="请输入模板名称"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getData('search')">查询 </el-button>
        <el-button @click="reset()">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="right-button">
      <el-button
        class="lake-Blue"
        type="primary"
        icon="el-icon-plus"
        @click="handleAdd()"
        >新增模板
      </el-button>
    </div>
    <!--列表-->
    <div>
      <el-table
        v-loading="listLoading"
        element-loading-text="加载中"
        :data="list"
        fit
        stripe
        border
      >
        <template slot="empty">
          <img src="@/assets/nopage.png" alt />
          <p>暂无数据</p>
        </template>
        <el-table-column
          fixed
          type="index"
          label="序号"
          width="60"
        ></el-table-column>
        <el-table-column label="模板名称" prop="tmplName" />
        <el-table-column label="制作人" prop="crteUsrName" width="150" />
        <el-table-column label="制作日期" prop="crteTime" width="150" />
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button class="cyan" type="text" @click="handleAdd(scope.row)"
              >编辑</el-button
            >
            <el-button class="red" type="text" @click="handleDelete(scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--分页-->
    <yl-pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="getData"
    />
  </div>
</template>

<script>
import ylPagination from "@/components/yl-pagination";
import {
  getTempList,
  deleteDetl
} from "@/api/purchasing-management/plan-template-maintenance";
export default {
  components: {
    ylPagination
  },
  data() {
    return {
      list: [],
      listLoading: false,
      listQuery: {
        //条件查询
        tmplName: "",
        current: 1,
        size: 10
      },
      total: 0 //分页
    };
  },

  created() {
    this.getData();
  },

  methods: {
    //获取列表数据
    getData(type) {
      this.listLoading = true;
      if (type == "search") {
        this.listQuery.current = 1;
      }

      getTempList(this.listQuery)
        .then((response) => {
          this.list = response.data.records;
          this.total = response.data.total;
          this.listLoading = false;
        })
        .catch((err) => {
          this.listLoading = false;
        });
    },

    reset() {
      this.listQuery.tmplName = "";
      this.getData();
    },

    // 跳转新增模板
    handleAdd(row) {
      if (row) {
        this.$router.push({
          path: "/purchasing-management/plan-template-maintenance-add",
          query: {
            id: row && row.id ? row.id : "",
            tmplName: row.tmplName ? row.tmplName : ""
          }
        });
      } else {
        this.$router.push({
          path: "/purchasing-management/plan-template-maintenance-add"
        });
      }
    },

    // 删除
    handleDelete(row) {
      this.$confirm("确认删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          deleteDetl({ id: row.id })
            .then((response) => {
              this.$message({
                message: "删除成功",
                type: "success"
              });
            })
            .catch((err) => {});
        })
        .catch(() => {});
    }
  }
};
</script>

<style>
</style>