<template>
  <div class="main flex-1">
    <div style="width:100%" v-if="lists && lists.length > 0">
      <a-table
        :columns="columns"
        :data-source="lists"
        :loading="loading"
        :pagination="pagination"
        :row-key="record => record.id"
        :scroll="{ y: height }"
        @change="handleTableChange"
        bordered
        class="white"
      >
        <template slot="id" slot-scope="text, record, index">
          <span>{{ index + 1 }}</span>
        </template>

        <template slot="time" slot-scope="text, record">
          <span v-if="record.releaseTime">{{ record.releaseTime}}</span>
          <span v-if="record.closeTime">-{{record.closeTime}}</span>
        </template>

        <template slot="platforms" slot-scope="text, record">
          <a-tag
            :color="record.platforms === 0 ? 'green' : 'blue'"
          >{{ record.platforms === 0 ? '绿谷' : '创时代' }}</a-tag>
        </template>

        <template slot="auditStatus" slot-scope="text, record">
          <a-tag
            :color="record.auditStatus === 2 ? 'green' : record.auditStatus === 1 ? 'red' : 'blue'"
          >{{ record.auditStatus === 2 ? '已通过' : record.auditStatus === 1 ? '未通过' : '审核中' }}</a-tag>
        </template>

        <template slot="examine" slot-scope="text, record">
          <div class="d-flex flex-column align-center">
            <a-button
              @click="handleApply(record, 2)"
              size="small"
              type="primary"
              v-if="record.auditStatus !== 2"
            >通过</a-button>
            <a-button @click="handleApply(record, 1)" size="small" type="danger" v-else>不通过</a-button>
            <a-textarea class="fill-width mt-3" placeholder="备注" v-model="record.applyRemark" />
          </div>
        </template>
      </a-table>
    </div>
    <a-empty v-else />
  </div>
</template>

<script>
import { getAuditApply } from 'config/api';
const columns = [
  {
    title: '序号',
    align: 'center',
    dataIndex: 'id',
    key: 'id',
    width: 80,
    scopedSlots: { customRender: 'id' },
  },
  {
    title: '企业名',
    align: 'center',
    dataIndex: 'companyName',
    key: 'companyName',
  },
  {
    title: '申请人姓名',
    align: 'center',
    dataIndex: 'contactName',
    key: 'contactName',
  },
  {
    title: '联系方式',
    align: 'center',
    dataIndex: 'contactPhone',
    key: 'contactPhone',
  },
  {
    title: '申请时间',
    align: 'center',
    dataIndex: 'time',
    key: 'time',
  },
  {
    title: '申请平台',
    align: 'center',
    dataIndex: 'platforms',
    key: 'platforms',
    scopedSlots: { customRender: 'platforms' },
  },
  {
    title: '审核状态',
    align: 'center',
    dataIndex: 'auditStatus',
    key: 'auditStatus',
    scopedSlots: { customRender: 'auditStatus' },
  },
  {
    title: '备注',
    align: 'center',
    dataIndex: 'remark',
    key: 'remark',
    scopedSlots: { customRender: 'remark' },
  },
  {
    title: '审核',
    align: 'center',
    dataIndex: 'examine',
    key: 'examine',
    scopedSlots: { customRender: 'examine' },
  },
];

export default {
  name: 'ActivityDate',
  props: { lists: { type: Array, default: () => [] }, pagination: { type: Object, default: () => {} } },

  data() {
    return {
      columns,
      loading: false,
      height: '',
      editVisible: false,
      editItem: null, // 修改的那条
      spinning: false,
    };
  },

  mounted() {
    let th = 250;
    let wh = window.innerHeight;
    this.height = wh - th;
    window.onresize = () => {
      return (() => {
        wh = window.innerHeight;
        this.height = wh - th;
      })();
    };
  },

  methods: {
    async closeModal() {
      this.editVisible = false;
      await this.$emit('getSelectTeam');
    },

    // 换页
    handleTableChange(pagination) {
      const { current, pageSize } = pagination;
      const condition = { current, pageSize };
      this.$emit('getSelectTeam', condition);
    },

    // 审核
    async handleApply(record, auditStatus) {
      try {
        const params = { param: { applyId: record.applyActivityId, auditStatus, remark: record.applyRemark } };
        const res = await getAuditApply(params);
        const { data, msg, code } = res.data;
        if (code === 200) {
          this.$message.success('审核成功');
          this.$emit('getSelectTeam');
        } else {
          throw msg;
        }
      } catch (error) {
        this.$message.error(error || '审核失败');
      }
    },
  },
};
</script>

<style scoped lang="stylus"></style>
