<template>
  <div>
    <x-card>
      <div slot="content" class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-input v-model="queryParam.name" allow-clear placeholder="请输入名称" />
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-input v-model="queryParam.code" allow-clear placeholder="请输入编码" />
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item>
                <a-select placeholder="请选择状态" v-model="queryParam.status" allow-clear>
                  <a-select-option
                    v-for="(item, index) in GetClientDataList('basestatus')"
                    :key="index"
                    :value="item.code"
                    >{{ item.val }}</a-select-option
                  >
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :md="(!advanced && 6) || 24" :sm="24">
              <span
                class="table-page-search-submitButtons"
                :style="(advanced && { float: 'right', overflow: 'hidden' }) || {}"
              >
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="() => (queryParam = {})">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </x-card>
    <a-card :bordered="false">
      <s-table ref="table" :columns="columns" :data="loadData" :alert="true" :rowKey="record => record.id">
        <template slot="operator" v-if="hasPerm('VipAuth:add')">
          <a-button @click="AddorEdit({}, 0)" icon="plus" type="primary" v-if="hasPerm('VipAuth:add')"
            >新增权限</a-button
          >
        </template>
        <span slot="status" slot-scope="text, record">
          <span style="color: red" v-if="record.status === 2"> {{ getenumval('basestatus', text) }}</span>
          <span style="color: #1dbb1d" v-else> {{ getenumval('basestatus', text) }}</span>
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('VipAuth:edit')" @click="AddorEdit(record, 1)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('VipAuth:status') && record.status === 1" />
          <a-popconfirm
            v-if="hasPerm('VipAuth:status') && record.status === 1"
            placement="topRight"
            title="确认禁用？"
            @confirm="() => setstatus(record, 2)"
          >
            <a>禁用</a>
          </a-popconfirm>
          <a-divider type="vertical" v-if="hasPerm('VipAuth:status') && record.status == 2" />
          <a-popconfirm
            v-if="hasPerm('VipAuth:status') && record.status == 2"
            placement="topRight"
            title="确认启用？"
            @confirm="() => setstatus(record, 1)"
          >
            <a>启用</a>
          </a-popconfirm>
        </span>
      </s-table>
      <editForm ref="editForm" :title="title" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
import { STable, XCard } from '@/components'
import { AuthList, AuthStatus } from '@/api/modular/main/vip'
import { GetClientDataItem, GetClientDataList } from '@/utils/clientdata'
import editForm from './editForm'
export default {
  components: {
    XCard,
    STable,
    editForm
  },
  data() {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      title: '',
      // 表头
      columns: [
        {
          title: '权限',
          dataIndex: 'name'
        },
        {
          title: '编码',
          dataIndex: 'code'
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: {
            customRender: 'status'
          }
        },
        {
          dataIndex: 'remark',
          title: '备注'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return AuthList(Object.assign(parameter, this.queryParam)).then(res => {
          return res.data
        })
      }
    }
  },
  created() {
    if (this.hasPerm('VipAuth:status') || this.hasPerm('VipAuth:edit')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
  },
  methods: {
    GetClientDataList,
    handleOk() {
      this.$refs.table.refresh()
    },
    // 获取枚举信息
    getenumval(type, item) {
      return GetClientDataItem(type, item)
    },
    // 新增或者编辑
    AddorEdit(record, type) {
      this.title = type === 1 ? '编辑权限' : '新增权限'
      this.$refs.editForm.init(record)
    },
    // 修改状态
    setstatus(record, status) {
      AuthStatus({
        id: record.id,
        status: status
      })
        .then(res => {
          if (res.code === 0) {
            this.$message.success('状态设置成功')
            this.$refs.table.refresh()
          } else {
            this.$myerror(res.message)
          }
        })
        .catch(err => {
          this.$myerror(err.message)
        })
    }
  }
}
</script>
<style scoped lang="less">
/deep/ .ant-table td {
  white-space: nowrap;
}
/deep/ .ant-table-body {
  overflow-x: auto !important;
}
/deep/ .ant-table-thead > tr > th,
.ant-table-tbody > tr > td {
  white-space: nowrap;
}
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
