import * as API from 'api/virtual/companies';
export default {
  name: 'Companies',
  components: {
    UiEdit: resolve => require(['views/virtual/companies/edit'], resolve),
    UiDetail: resolve => require(['views/virtual/companies/detail'], resolve)
  },
  data() {
    return {
      params: {
        name: '',
        status: ''
      },
      trashed: null,
      list_data: null,
      editObj: {
        show: false,
        data: null
      },
      detailObj: {
        show: false,
        data: null
      },
      search: {
        show: false
      },
      table_titles: [
        {
          title: 'ID',
          key: 'id',
          width: 80
        },
        {
          title: '企业名称',
          key: 'name',
          width: 300
        },
        {
          title: '联系人',
          key: 'contacts'
        },
        {
          title: '电话',
          key: 'mobile'
        },
        {
          title: '地址',
          key: 'address'
        },
        {
          title: '状态',
          key: '',
          width: 100,
          render: (h, { row, column, index }) => {
            let type = ['primary', 'warning', 'error'];
            let text = ['已启用', '已禁用', '已删除'];

            return h('Button', {
              props: {
                type: type[row.status],
                size: 'small'
              }
            }, text[row.status]);
          }
        },
        {
          title: '创建时间',
          key: 'created_at',
          width: 170
        },
        {
          title: '操作',
          key: 'action',
          render: (h, {
            row,
            column,
            index
          }) => {
            let html = [];

            if (row.deleted_at) {
              return h('Tag', { props: { color: 'default' } }, '该企业已被删除');
            }

            if (this.haveJurisdiction('show')) {
              html.push(h('Button', {
                props: {
                  type: 'success',
                  size: 'small',
                  disabled: false,
                  icon: 'md-eye'
                },
                class: ['btn'],
                on: {
                  click: (event) => {
                    this.detailObj = {
                      show: true,
                      data: row
                    };
                  }
                }
              }, '查看'));
            }

            if (this.haveJurisdiction('update')) {
              html.push(h('Button', {
                props: {
                  type: 'primary',
                  size: 'small',
                  disabled: false,
                  icon: 'md-create'
                },
                class: ['btn'],
                on: {
                  click: (event) => {
                    this.openEdit(true, row);
                  }
                }
              }, '编辑'));
            }

            if (this.haveJurisdiction('destroy')) {
              html.push(h('Button', {
                props: {
                  type: 'error',
                  size: 'small',
                  disabled: false,
                  icon: 'md-trash'
                },
                class: ['btn'],
                on: {
                  click: () => {
                    this.$Modal.confirm({
                      title: '提示',
                      content: '删除后该企业不可使用，请谨慎操作',
                      onOk: () => {
                        API.destroy({
                          ids: row.id
                        }).then(res => {
                          if (res.code == 0) {
                            this.$Message.success('删除成功');
                            this.request();
                          }
                        });
                      }
                    });
                  }
                }
              }, '删除'));
            }

            if (html.length) {
              return h('div', html);
            }
          }
        }
      ]
    };
  },
  created() {
    this.index(1);
  },
  methods: {
    /**
     * [index 列表]
     * @param  {Number} page [description]
     * @return {[type]}      [description]
     */
    index(page = 1) {
      let params = Object.assign(this.params, { 'orderBy': 'id', 'sortedBy': 'asc' });

      if (params.status === 2) {
        params.status = undefined;
        params.trashed = 'only';
      } else {
        params.trashed = 'without';
      }

      let data = this.searchDataHandle({}, { page }, params);

      this.isShowLoading(true);
      API.index(data).then(res => {
        this.isShowLoading(false);
        if (res.code == 0) {
          this.list_data = res.data;
        }
      }).catch(() => {
        this.isShowLoading(false);
      });
    },

    /**
     * [openEdit 打开编辑弹窗]
     * @return {[type]} [description]
     */
    openEdit(bool, data = null) {
      this.editObj = {
        show: bool,
        data
      };
    },

    /**
     * [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.trashed = null;
      this.index(1);
    }
  }
};
