<template>
  <div class="main flex-1">
    <div style="width: 100%" v-if="lists && lists.length > 0">
      <div class="mb-4">
        <a-button :disabled="!hasSelected" :loading="loading" @click="start(1)" type="primary">审核通过</a-button>
        <a-button :disabled="!hasSelected" :loading="loading" @click="start(0)" class="ml-3" type="danger">审核不通过</a-button>
        <span class="ml-2">
          <template v-if="hasSelected">{{ `选中 ${selectedRowKeys.length} 条` }}</template>
        </span>
      </div>
      <a-table
        :columns="columns"
        :data-source="lists"
        :loading="loading"
        :pagination="pagination"
        :row-key="record => record.id"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :scroll="{ y: height }"
        @change="handleTableChange"
        bordered
        class="white"
      >
        <template slot="id" slot-scope="text, record, index">
          <span>{{ index + 1 }}</span>
        </template>

        <template slot="type" slot-scope="text, record">
          <a-tag color="blue">{{
            record.type === 0
              ? '申报通知'
              : record.type === 1
              ? '公告公示'
              : record.type === 2
              ? '政策动态'
              : record.type === 3
              ? '申报指南'
              : ''
          }}</a-tag>
        </template>

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

        <template slot="edit" slot-scope="text, record">
          <a-icon @click="showEditModal(record)" class="pointer" theme="twoTone" type="edit" />
        </template>

        <div class="d-flex flex-column" slot="expandedRowRender" slot-scope="record" style="margin: 0">
          <div class="mb-3">
            <span class="font-bold-14">原文链接：</span>
            {{ record.titleUrl }}
          </div>
          <div class="mb-3">
            <span class="font-bold-14">简介：</span>
            <br />
            <span v-if="record.intro">{{ record.intro }}</span>
            <span v-else>暂无内容</span>
          </div>
          <div>
            <span class="font-bold-14">详情：</span>
            <span v-dompurify-html="record.content" v-if="record.content"></span>
            <span v-else>暂无内容</span>
          </div>
        </div>
      </a-table>
    </div>
    <a-empty v-else />

    <!-- 编辑 -->
    <policy-edit :editItem="editItem" :editVisible="editVisible" @closeModal="closeModal" @getSelectTeam="getSelectTeam" />
  </div>
</template>

<script>
import PolicyEdit from 'components/Policy/PolicyEdit.vue';
import { upPolicy } from 'config/api';

const columns = [
  {
    title: '序号',
    align: 'center',
    dataIndex: 'id',
    key: 'id',
    width: 80,
    scopedSlots: { customRender: 'id' },
  },
  {
    title: '标题',
    align: 'center',
    dataIndex: 'title',
    key: 'title',
  },
  {
    title: '发布部门',
    align: 'center',
    dataIndex: 'publishDepartName',
    key: 'publishDepartName',
  },
  {
    title: '政策类型',
    align: 'center',
    dataIndex: 'type',
    key: 'type',
    width: 100,
    scopedSlots: { customRender: 'type' },
  },
  {
    title: '状态',
    align: 'center',
    dataIndex: 'status',
    key: 'status',
    width: 100,
    scopedSlots: { customRender: 'status' },
  },
  {
    title: '发布时间',
    align: 'center',
    dataIndex: 'publishTime',
    key: 'publishTime',
  },
  {
    title: '编辑',
    align: 'center',
    dataIndex: 'edit',
    key: 'edit',
    width: 80,
    scopedSlots: { customRender: 'edit' },
  },
];

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

  components: {
    PolicyEdit,
  },
  data() {
    return {
      columns,
      loading: false,
      selectedRowKeys: [],
      editingKey: '',
      height: '',
      editVisible: false,
      editItem: null, // 修改的那条
    };
  },

  computed: {
    hasSelected() {
      return this.selectedRowKeys.length > 0;
    },
  },

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

  methods: {
    // 选中
    onSelectChange(selectedRowKeys) {
      console.log('selectedRowKeys changed: ', selectedRowKeys);
      this.selectedRowKeys = selectedRowKeys;
    },

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

    // 审批
    async start(state) {
      try {
        this.loading = true;
        const params = { param: { ids: this.selectedRowKeys, status: state } };
        const res = await upPolicy(params);
        const { data, msg, code } = res.data;
        if (code === 200) {
          this.$message.success('审批成功');
          this.$emit('getSelectTeam');
          setTimeout(() => {
            this.loading = false;
            this.selectedRowKeys = [];
          }, 1000);
        } else {
          throw msg;
        }
      } catch (error) {
        this.$message.error(error || '审批失败');
      }
    },

    showEditModal(record) {
      console.log('record: ', record);
      this.editItem = record;
      this.editVisible = true;
    },

    closeModal() {
      this.editVisible = false;
    },

    async getSelectTeam() {
      await this.$emit('getSelectTeam');
    },

    // 打开报名
    openSignUp() {
      const { query } = this.$route;
      this.$router.push({ path: '/sign-up', query });
    },
  },
};
</script>

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