<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.appid" allow-clear placeholder="请输入小程序appid" />
              </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('mini:add')">
          <a-button @click="AddorEdit({}, 0)" icon="plus" type="primary" v-if="hasPerm('mini: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.picurl })"
            :alt="record.name"
            :src="record.picurl"
            :srcSet="record.picurl"
          />
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('mini:edit')" @click="AddorEdit(record, 1)">编辑</a>
        </span>
      </s-table>
      <editform ref="editform" :title="title" @ok="handleOk" />
      <previewimg ref="PreImg"></previewimg>
    </a-card>
  </div>
</template>
<script>
import { STable, XCard } from '@/components'
import { List } from '@/api/modular/main/wx'
import { GetClientDataItem, GetClientDataList } from '@/utils/clientdata'
import editform from './minieditform'
import previewimg from '@/views/components/previewimg'
export default {
  components: {
    XCard,
    STable,
    editform,
    previewimg
  },
  data() {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      title: '添加小程序',
      // 表头
      columns: [
        {
          title: '小程序',
          dataIndex: 'name'
        },
        {
          title: 'appid',
          dataIndex: 'appid'
        },
        {
          title: '密钥',
          dataIndex: 'appsecret'
        },
        {
          title: '标题',
          dataIndex: 'title'
        },
        {
          title: '主题色',
          dataIndex: 'themecolor'
        },
        {
          title: '说明',
          dataIndex: 'desc'
        },
        {
          title: '添加时间',
          dataIndex: 'createdtime'
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return List(Object.assign(parameter, this.queryParam)).then(res => {
          return res.data
        })
      }
    }
  },
  created() {
    if (this.hasPerm('mini:edit')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
  },
  methods: {
    GetClientDataList,
    GetClientDataItem,
    handleOk() {
      this.$refs.table.refresh()
    },
    // 新增或者编辑
    AddorEdit(record, type) {
      this.title = type === 1 ? '编辑小程序' : '添加小程序'
      this.$refs.editform.init(record)
    }
  }
}
</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>
