<template>
  <div class="page-body">

    <!-- 工具条 -->
    <div class="mk-toolbar">
      <a-button type="primary" @click="pageAdd">
        新增
      </a-button>
      <a-button @click="pageEdit">编辑</a-button>
      <a-dropdown>
        <a-menu slot="overlay" @click="handleMenuClick">
          <a-menu-item key="delete">
            删除
          </a-menu-item>
        </a-menu>
        <a-button> 操作
          <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>



    <!-- 搜索区 -->
    <vxe-form :data="searchFormData" :items="searchFormItems" titleColon @submit="onSearch">
      <template #date="{}">
     
        <a-form-item label="创建时间"  :style="{ display: 'inline-block',width:120 }">
        </a-form-item>
        <a-form-item :style="{ display: 'inline-block', width: 'calc(100% - 120px )' }">
          <a-range-picker @change="onDateChange" />
        </a-form-item>
     


      </template>
    </vxe-form>


    <!-- 表格区 -->
    <vxe-grid ref='xGrid' v-bind="gridOptions"></vxe-grid>

  </div>
</template>

<script>
import BASE_URL from '@/services/base/api.js';

let editPage = () => import("./edit");

export default {
  name: 'BasePermissionList', 
  data() {

    // 页面数据变量
    var pageData = {

      keyName : 'id',
      actions: {
        getList: `${BASE_URL.BASE_URL}/BasePermission/v1/base/permission/list`,
        delete: `${BASE_URL.BASE_URL}/BasePermission/v1/base/permission/batchDelete`
      },

      start_time :0,
      end_time :0,

      //搜索区
      searchFormData: {
        title: '',
        permission_code: '',
      },
      searchRules: [
        { key: "title", mode: "like" },
        { key: "permission_code", mode: "like" }
      ],



      searchFormItems: [
        {
          title: '左侧',
          span: 20,
          children: [
            { field: 'title', title: '权限名', span: 5, itemRender: { name: '$input', props: { placeholder: '请输入权限名' } } },
            { field: 'permission_code', title: '编号', span: 5, itemRender: { name: '$input', props: { placeholder: '请输入编号' } } },
            { span: 8, slots: { default: 'date' } },
            { align: 'right', span: 4, itemRender: { name: '$buttons', children: [{ props: { type: 'submit', content: '搜索', status: 'primary' } },
            { props: { type: 'reset', content: '重置' } }] } }
          ]
        }
      ],



      //数据区
      gridOptions: {
        height: 600,
        id: 'datagrid_1',

        proxyConfig: {
          sort: true, // 启用排序代理，当点击排序时会自动触发 query 行为
          filter: true, // 启用筛选代理，当点击筛选时会自动触发 query 行为 
          props: {
            result: 'BasePermission', // 配置响应结果列表字段
            total: 'total' // 配置响应结果总页数字段
          },
          // 接收Promise
          ajax: {
            // 当点击工具栏查询按钮或者手动提交指令 query或reload 时会被触发
            query: (options) => {
              const { page, sorts } = options;
              var params = {};
              params.page = page.currentPage;
              params.limit = page.pageSize;
              params.order_bys = [];
              params.search_rules = this.getSearchParms();
              if (sorts) {
                sorts.forEach((v) => {
                  params.order_bys.push({
                    column: v.property,
                    order: v.order
                  })
                });
              }
              return this.loadData({ params });
            }
          }
        },
        columns: [
          { type: 'checkbox', width: 50 },
          { type: 'seq', width: 50 },
          { field: 'title', sortable: true, title: '权限名' },
          { field: 'permission_code', sortable: true, title: '编号', showHeaderOverflow: true },
          { field: 'service', sortable: true, title: 'service', showHeaderOverflow: true },
          { field: 'desc', sortable: true, title: '描述', showHeaderOverflow: true }
        ]
      }
    };

    pageData.gridOptions = Object.assign({},this.$mk.config.defaults.gridOptions, pageData.gridOptions);

    return pageData;
  },
  // 监听 - 页面每次【加载时】执行 
  onLoad() {

  },
  // 函数
  methods: {
    onDateChange(date) {
      if(date && date.length){
        this.start_time = parseInt(date[0]._d.getTime()/ 1000);
      this.end_time = parseInt(date[1]._d.getTime()/ 1000);
      }else{
        this.start_time = 0;
        this.end_time = 0;
      }
     
    },
    getSearchParms() {
      var rules = [];
      let findMode = k => {
        for (let i in this.searchRules) {
          if (this.searchRules[i].key == k) return this.searchRules[i].mode;
        }
        return "equal";
      };

      for (let key in this.searchFormData) {
        let value = this.searchFormData[key];
        if (value) {
          let mode = findMode(key);
          if(mode == "like"){
            value = "%" + value +"%";
          }
          rules.push({
            column: key,
            mode: mode,
            value: value
          });
        }
      }
      return rules;
    },
    getSelectdRow() {
      let row = this.$refs.xGrid.getCurrentRecord();
      if (!row) {
        let rows = this.$refs.xGrid.getCheckboxRecords();
        if (rows && rows.length) { row = rows[0]; }
      }
      return row;
    },
    loadData({ params }) {

      params.start_time = this.start_time;
      params.end_time = this.end_time;


      return this.$mk.getPagedData({ url: this.actions.getList, data: params });
    },

    pageAdd() {
      this.$mk.dialog.open({
        page: editPage,
        title: "新增权限信息",
        pageMode: "add",
        dataId: 0,
        callback: ({ success }) => {
          success && this.$refs.xGrid.commitProxy('query')
        }
      })
    },

    pageEdit() {
      let row = this.getSelectdRow();
      if (!row) {
        this.$mk.msg("请选择行");
        return;
      }
      this.$mk.dialog.open({
        page: editPage,
        title: "编辑权限信息",
        pageMode: "edit",
        dataId: row[this.keyName],
        callback: ({ success }) => {
          success && this.$refs.xGrid.commitProxy('query')
        }
      })
    },

    handleMenuClick(e) {
      if (e.key == "delete") {

        let rows = this.$refs.xGrid.getCheckboxRecords();
        let ids = [];
        rows.forEach((row) => {
          ids.push(row[this.keyName]);

        });

        if (!ids.length) {
          this.$mk.error("请选择行");
          return;
        }

        this.$mk.confirm('您确定要删除吗？').then(type => {
          if (type == 'confirm') {
            this.$mk.post({
              url: this.actions.delete,
              loading: "删除中...",
              data: {
                ids: ids
              }
            }).then(() => {
              this.$mk.success("删除成功");
              this.onSearch();
            }).catch((a) => {
              this.$mk.error(a.data.msg);
            });
          }
        });


      }
    },
    onSearch() {
      this.$refs.xGrid.commitProxy('query')
    }
  },
  // 监听属性
  watch: {

  }
};

</script>
<style scoped lang="less">
.page-body {
  padding: 30px ;
  background: @base-bg-color;
}
</style> 