<template>
    <basic-container>
        <avue-crud 
            :data="data" 
            :option="option"
            :page.sync="page"
            @search-change="searchChange"
            @current-change="currentChange"
            @size-change="sizeChange"
            :table-loading="loading"
            @on-load="onLoad"
            @search-reset="searchReset"
            @row-update="rowUpdate"
            @row-save="rowSave"
            @row-del="rowDel"
        >
        <template
            slot-scope="scope"
            slot="menu"
        >
        <el-button
          type="text"
          icon="el-icon-edit"
          size="small"
          @click.stop="editUnit(scope.row, scope.index)"
        >编辑计量系统单元
        </el-button>
        </template>
        </avue-crud>
    </basic-container>
  </template>
  
  
  <script>
import { getListPage,update,remove } from "@/api/measure/template.js";
export default {
  data () {
    return {
      data: [],
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 16
      },
      loading:true,
      option: {
        height: 'auto',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: true,
        menuWidth: 400,
        dialogClickModal: false,
        column: [
          {
            label: "模版名称",
            prop: "name",
            search: true,
            rules: [{
              required: true,
              message: "请输入名称",
              trigger: "blur"
            }]
          },
    

          {
            label: "备注",
            prop: "remarks",
            editDisplay: true,
            addDisplay: true,
            rules: [{
              message: "请输入表数量",
              trigger: "blur",
            }]
          }
        ]
      },
      query: {},
    };
  },

  methods: {
    onLoad (page, params = {}) {
      this.loading = true;
      getListPage(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;

      });
    },
    searchChange (params, done) {
     console.log(params,'params');
     this.query = params;
     this.onLoad(this.page);
      done();
    },
    searchReset () {
      this.query = {};
      this.onLoad(this.page);
    },
    editUnit(row){
        console.log(row);
        this.$router.push({
        path: '/measure/template',
        query: {
         id:row.id
        }
      })
    },
    currentChange (currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange (pageSize) {
      this.page.pageSize = pageSize;
    },
    rowUpdate (row, index, done, loading) {
      update(row).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
        console.log(error);
      });
    },
    rowSave (row, done, loading) {
      update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
    },
    rowDel (row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return remove(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
  },
 
}
  
  </script>
  
  <style>
  </style>
  