<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="(!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('appnavbar:add')">
          <a-button @click="AddorEdit({}, 0)" icon="plus" type="primary" v-if="hasPerm('appnavbar:add')"
            >新增导航</a-button
          >
        </template>
        <span slot="headimg" slot-scope="text, record">
          <a-avatar
            shape="square"
            :size="64"
            @click="$refs.PreImg.Show({ alt: record.name, img: record.imageurl })"
            :alt="record.name"
            :src="record.imageurl"
            :srcSet="record.imageurl"
          />
        </span>
        <span slot="moreinfo" slot-scope="text">
          <ellipsis :length="10" tooltip>{{ text }}</ellipsis>
        </span>
        <span slot="status" slot-scope="text, record">
          <a-tag color="#f50" v-if="record.status === 1"> {{ getenumval('commonstatus', text) }}</a-tag>
          <a-tag color="#1dbb1d" v-else> {{ getenumval('commonstatus', text) }}</a-tag>
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('appnavbar:edit')" @click="AddorEdit(record, 1)">编辑</a>
          <a-divider type="vertical" v-if="hasPerm('appnavbar:status') && record.status === 1" />
          <a-popconfirm
            v-if="hasPerm('appnavbar:status') && record.status === 1"
            placement="topRight"
            title="确认启用？"
            @confirm="() => setstatus(record, 0)"
          >
            <a>启用</a>
          </a-popconfirm>
          <a-divider type="vertical" v-if="hasPerm('appnavbar:status') && record.status == 0" />
          <a-popconfirm
            v-if="hasPerm('appnavbar:status') && record.status == 0"
            placement="topRight"
            title="确认禁用？"
            @confirm="() => setstatus(record, 1)"
          >
            <a>禁用</a>
          </a-popconfirm>
        </span>
      </s-table>
      <editForm ref="editForm" :title="title" @ok="handleOk" />
    </a-card>
    <previewimg ref="PreImg"></previewimg>
  </div>
</template>
<script>
import { STable, XCard, Ellipsis } from '@/components'
import { List, SetStatus } from '@/api/modular/main/navbar'
import { GetClientDataItem, GetClientDataList } from '@/utils/clientdata'
import previewimg from '@/views/components/previewimg'
import editForm from './editForm'
import { copyTextByContentType, textcontent } from '@/utils/util'
export default {
  components: {
    XCard,
    STable,
    editForm,
    previewimg,
    Ellipsis
  },
  data() {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      title: '',
      // 表头
      columns: [
        {
          title: '排序',
          dataIndex: 'sort'
        },
        {
          title: '封面图',
          dataIndex: 'image',
          scopedSlots: {
            customRender: 'headimg'
          }
        },
        {
          title: '标题',
          dataIndex: 'name'
        },
        {
          title: '链接',
          dataIndex: 'link',
          scopedSlots: {
            customRender: 'moreinfo'
          }
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: {
            customRender: 'status'
          }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return List(Object.assign(parameter, this.queryParam)).then(res => {
          return res.data
        })
      }
    }
  },
  created() {
    if (this.hasPerm('appnavbar:status') || this.hasPerm('appnavbar:edit')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
  },
  methods: {
    GetClientDataList,
    textcontent,
    copyTextByContentType,
    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) {
      SetStatus({
        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>
