<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.title" 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('shopstatus')"
                    :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">
        <span slot="headimg" slot-scope="text, record">
          <a-avatar
            shape="square"
            :size="64"
            @click="$refs.PreImg.Show({ alt: record.title, img: record.imageurl })"
            :alt="record.title"
            :src="record.imageurl"
            :srcSet="record.imageurl"
          />
        </span>
        <span slot="page" slot-scope="text, record">
          <span v-if="record.status === 4 || record.status === 5">不支持</span>
          <a v-else @click="copyTextByContentType(text, 9)">复制</a>
        </span>
        <span slot="status" slot-scope="text, record">
          <a-tag color="#f50" v-if="record.status === 1 || record.status === 5">
            {{ getenumval('shopstatus', text) }}</a-tag
          >
          <a-tag color="#1dbb1d" v-else-if="record.status === 2 || record.status === 6">
            {{ getenumval('shopstatus', text) }}</a-tag
          >
          <a-tag color="#b3b3b3" v-else> {{ getenumval('shopstatus', text) }}</a-tag>
        </span>
        <span slot="moreinfo" slot-scope="text">
          <ellipsis :length="10" tooltip>{{ text }}</ellipsis>
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('demand:view')" @click="detail(record)">详情</a>
        </span>
      </s-table>
      <detail ref="detail" @ok="handleOk" />
    </a-card>
  </div>
</template>
<script>
import { STable, XCard, Ellipsis } from '@/components'
import { List, SetStatus } from '@/api/modular/main/demand'
import { GetClientDataItem, GetClientDataList } from '@/utils/clientdata'
import detail from './detail'
import { copyTextByContentType, textcontent } from '@/utils/util'
export default {
  components: {
    XCard,
    STable,
    detail,
    Ellipsis
  },
  data() {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      rejectvisible: false,
      rejectconfirmloading: false,
      form: this.$form.createForm(this),
      // 表头
      columns: [
        {
          title: '需求名称',
          dataIndex: 'title',
          scopedSlots: {
            customRender: 'moreinfo'
          }
        },
        {
          title: '浏览数',
          dataIndex: 'browsecnt'
        },
        {
          title: '点赞数',
          dataIndex: 'likecnt'
        },
        {
          title: '状态',
          dataIndex: 'status',
          scopedSlots: {
            customRender: 'status'
          }
        },
        {
          dataIndex: 'createtime',
          title: '创建时间'
        },
        {
          title: '内链',
          dataIndex: 'id',
          scopedSlots: {
            customRender: 'page'
          }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return List(Object.assign(parameter, this.queryParam)).then(res => {
          return res.data
        })
      }
    }
  },
  created() {
    if (this.hasPerm('demand:view')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
    if (this.$store.state.admintype !== 3) {
      this.columns.unshift({
        title: '会员',
        width: '150px',
        dataIndex: 'businessname'
      })
    }
  },
  methods: {
    GetClientDataList,
    copyTextByContentType,
    textcontent,
    handleOk() {
      this.$refs.table.refresh()
    },
    // 获取枚举信息
    getenumval(type, item) {
      return GetClientDataItem(type, item)
    },
    // 新增或者编辑
    detail(record) {
      this.$refs.detail.init(record)
    },
    // 修改状态
    setstatus(record, status) {
      if (status === 5) {
        this.rejectvisible = true
        return
      }
      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)
        })
    },
    rejectok(e) {
      const that = this
      this.form.validateFields((err, values) => {
        if (!err) {
          this.rejectconfirmloading = true
          SetStatus({
            id: that.record.id,
            statusremark: values['statusremark']
          })
            .then(res => {
              if (res.code === 0) {
                this.$message.success('驳回成功')
                this.handleOk()
              } else {
                this.$myerror(res.message)
              }
              this.rejectcancel()
            })
            .catch(err => {
              this.$myerror(err.message)
              this.rejectcancel()
            })
        }
      })
    },
    rejectcancel() {
      this.rejectvisible = false
      this.rejectconfirmloading = false
    }
  }
}
</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>
