import * as API from 'api/base/logs';

export default {
  name: 'Logs',
  data() {
    return {
      params: {
        request_param: ''
      },
      other: {
        time: []
      },
      list_data: null,
      search: {
        show: false
      },
      selection: [], // 复选框选中项
      table_titles: [
        {
          type: 'selection',
          width: 60,
          align: 'center'
        },
        {
          title: '序号',
          width: 80,
          key: '',
          render: (h, { row, column, index }) => {
            return h('span', index + 1);
          }
        },
        {
          title: '账号',
          width: 120,
          key: 'creator_username'
        },
        {
          title: '动作',
          width: 150,
          key: 'action'
        },
        {
          title: '请求方法',
          width: 120,
          key: 'request_method'
        },
        {
          title: '请求毫秒',
          width: 120,
          key: 'microtime'
        },
        {
          title: 'IP',
          width: 150,
          key: 'ip'
        },
        {
          title: '访问设备',
          width: 170,
          key: 'request_device'
        },
        {
          title: '访问浏览器',
          width: 200,
          key: 'request_browser'
        },
        {
          title: '创建时间',
          key: 'created_at',
          width: 170
        },
        {
          title: '操作',
          key: 'action',
          render: (h, { row, column, index }) => {
            let html = [];

            if (this.haveJurisdiction('destroy')) {
              html.push(h('Button', {
                props: {
                  type: 'error',
                  size: 'small',
                  disabled: false,
                  icon: 'md-trash'
                },
                class: ['btn'],
                on: {
                  click: () => {
                    this.destroy({ ids: row.id });
                  }
                }
              }, '删除'));
            }

            if (html.length) {
              return h('div', html);
            }
          }
        }
      ]
    };
  },
  created() {
    this.index();
  },
  methods: {
    /**
     * [index 列表]
     * @param  {Number} page [description]
     * @return {[type]}      [description]
     */
    index(page = 1) {
      this.scrollTop();
      let data = this.searchDataHandle(this.params, { page }, this.other);
      this.isShowLoading(true);
      API.index(data).then(res => {
        this.isShowLoading(false);
        if (res.code == 0) {
          let result = res.data;
          result.data = this.tableCheckboxHandle(result.data, this.selection);
          this.list_data = result;
        }
      }).catch(err => {
        this.isShowLoading(false);
      });
    },

    /**
     * [selectionChange 复选框事件]
     * @return {[type]} [description]
     */
    selectionChange(selection) {
      this.selection = selection;
    },

    /**
     * [destroyBatch 批量删除触发按钮]
     * @return {[type]} [description]
     */
    destroyBatch() {
      if (this.selection.length) {
        let arry = this.selection.map(item => {
          return item.id;
        });
        this.destroy({ ids: arry.join(',') });
      } else {
        this.$Message.info('请勾选要删除的项');
      }
    },

    /**
     * [destroy 删除操作]
     * @param  {[type]} data [description]
     * @return {[type]}      [description]
     */
    destroy(data) {
      this.$Modal.confirm({
        title: '提示',
        content: '确认执行删除操作？',
        onOk: () => {
          API.destroy(data).then(res => {
            if (res.code == 0) {
              // 当有勾选项，删除操作的地方为每行的按钮，将复选框勾选项去除此id
              const ids = data.ids.toString().split(',');
              if (ids.length == 1) {
                for (let i = 0, len = this.selection.length; i < len; i++) {
                  if (ids[0] == this.selection[i].id) {
                    this.selection.splice(i, 1);
                    break;
                  }
                }
              }
              this.$Message.success('删除成功');
              this.request();
            }
          });
        }
      });
    },

    /**
     * [request 刷新]
     * @return {[type]} [description]
     */
    request() {
      const result = this.list_data;
      let page = result.current_page;

      if (this.list_data.data.length == 1) {
        page = this.returnPage(result.total, result.current_page, result.per_page);
      }

      this.index(page);
    },

    resetSearch() {
      for (let k in this.params) {
        this.params[k] = '';
      }
      this.other.time = [];
      this.index(1);
    },

    handleSelectAll(bool) {
      this.$refs.table.selectAll(bool);
    }
  }
};
