<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.phone" allow-clear placeholder="请输入手机号" />
              </a-form-item>
            </a-col>
            <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-select placeholder="请选择状态" v-model="queryParam.status" allow-clear @change="handleStatusChange">
                  <a-select-option
                    v-for="(item, index) in GetClientDataList('usermembershipapplicationstatusenum')"
                    :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="avatar" slot-scope="text, record">
          <a-avatar
            shape="square"
            :size="64"
            @click="$refs.PreImg.Show({ alt: record.name, img: record.avatar })"
            :alt="record.name"
            :src="record.avatar"
            :srcSet="record.avatar"
          />
        </span>
        <span slot="sex" slot-scope="text">
          {{ GetClientDataItem('gender', text) }}
        </span>
        <span slot="birthday" slot-scope="text">
          {{ moment(text).format('yyyy-MM-DD') }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="hasPerm('userApplyfor:audit')" @click="detail(record)">详情</a>
        </span>
        <span slot="moreinfo" slot-scope="text">
          <ellipsis :length="10" tooltip>{{ text }}</ellipsis>
        </span>
        <span slot="memberstatus" slot-scope="text, record">
          <a-tag color="#f50" v-if="record.memberstatus === 2">
            {{ GetClientDataItem('usermembershipapplicationstatusenum', text) }}</a-tag
          >
          <a-tag color="#1dbb1d" v-else-if="record.memberstatus === 3">
            {{ GetClientDataItem('usermembershipapplicationstatusenum', text) }}</a-tag
          >
          <a-tag color="#b3b3b3" v-else> {{ GetClientDataItem('usermembershipapplicationstatusenum', text) }}</a-tag>
        </span>
      </s-table>
      <detail ref="detail" @ok="handleOk" />
      <previewimg ref="PreImg"></previewimg>
    </a-card>
  </div>
</template>
<script>
import { STable, XCard, Ellipsis } from '@/components'
import { getUserSupplyForList } from '@/api/modular/system/userManage'
import { GetClientDataItem, GetClientDataList } from '@/utils/clientdata'
import previewimg from '@/views/components/previewimg'
import detail from './detail'
import moment from 'moment'
export default {
  components: {
    XCard,
    STable,
    detail,
    previewimg,
    Ellipsis
  },
  data() {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      statusDictTypeDropDown: [],
      // 表头
      columns: [
        {
          title: '头像',
          dataIndex: 'avatar',
          scopedSlots: {
            customRender: 'avatar'
          }
        },
        {
          title: '姓名',
          dataIndex: 'name'
        },
        {
          title: '手机号',
          dataIndex: 'phone'
        },
        {
          title: '行业',
          dataIndex: 'industryname'
        },
        {
          title: '企业',
          dataIndex: 'comname',
          scopedSlots: {
            customRender: 'moreinfo'
          }
        },
        {
          title: '企业职位',
          dataIndex: 'compos'
        },
        // {
        //   title: '生日',
        //   dataIndex: 'birthday',
        //   scopedSlots: {
        //     customRender: 'birthday'
        //   }
        // },
        {
          title: '性别',
          dataIndex: 'sex',
          scopedSlots: {
            customRender: 'sex'
          }
        },
        {
          title: '申请时间',
          dataIndex: 'createdtime'
        },
        {
          title: '来源',
          dataIndex: 'source'
        },
        {
          title: '提供资讯',
          dataIndex: 'provide',
          scopedSlots: {
            customRender: 'moreinfo'
          }
        },
        {
          title: '希望资源',
          dataIndex: 'demand',
          scopedSlots: {
            customRender: 'moreinfo'
          }
        },
        {
          title: '状态',
          dataIndex: 'memberstatus',
          scopedSlots: {
            customRender: 'memberstatus'
          }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        return getUserSupplyForList(Object.assign(parameter, this.queryParam)).then(res => {
          if (res.data && res.data.rows) {
            const rows = res.data.rows.map(item => {
              item.avatar = process.env.VUE_APP_API_BASE_URL + item.avatar
              return item
            })
            res.data.rows = rows
          }
          return res.data
        })
      }
    }
  },
  created() {
    if (this.hasPerm('userApplyfor:audit')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' }
      })
    }
  },
  methods: {
    GetClientDataItem,
    GetClientDataList,
    moment,
    handleOk() {
      this.$refs.table.refresh()
    },
    handleStatusChange(value) {
      if (value!=0&&value==1&&value==2&&value==3) {
        this.queryParam.status = null
      }
    },
    // 详情
    detail(record) {
      this.$refs.detail.init(record)
    },
    // 修改状态
    setstatus(record, status) {
      sysUserChangeStatus({
        id: record.id,
        status: status === 0 ? 1 : 0
      })
        .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>
