<template>
  <a-card :bordered="false">

    <a-tabs :active-key="tabKey" @change="callback">
      <a-tab-pane key="1" tab="全部信息">
        <a-table
          size="default"
          rowKey="id"
          :columns="columns"
          :data-source="workerData"
        >
          <span slot="name" slot-scope="text, record">
           <a-tag color="blue">
          消息提示
          </a-tag>
          </span>
          <span slot="content" slot-scope="text, record">
            <a @click="toRepair(record.repairId)">{{ record.content }}</a>
          </span>
          <span slot="action" slot-scope="text, record">
        <a v-if="editEnabel" @click="hanleDelete(record.id)">删除</a>
      </span>
        </a-table>
      </a-tab-pane>
      <a-tab-pane key="2" tab="未读通知" force-render>
        <a-table
          size="default"
          rowKey="id"
          :columns="columns"
          :data-source="noReadData"
        >
          <span slot="name" slot-scope="text, record">
           <a-tag color="blue">
          消息提示
          </a-tag>
          </span>
          <span slot="content" slot-scope="text, record">
            <a @click="toRepair(record.repairId)">{{ record.content }}</a>
          </span>
          <span slot="action" slot-scope="text, record">
        <a v-if="editEnabel" @click="hanleDelete(record.id)">删除</a>
      </span>
        </a-table>
      </a-tab-pane>
      <a-tab-pane key="3" tab="已读通知" force-render>
        <a-table
          size="default"
          rowKey="id"
          :columns="columns"
          :data-source="readData"
        >
          <span slot="name" slot-scope="text, record">
           <a-tag color="blue">
          消息提示
          </a-tag>
          </span>
          <span slot="content" slot-scope="text, record">
            <a @click="toRepair(record.repairId)">{{ record.content }}</a>
          </span>
          <span slot="action" slot-scope="text, record">
        <a v-if="editEnabel" @click="hanleDelete(record.id)">删除</a>
      </span>
        </a-table>
      </a-tab-pane>
    </a-tabs>
  </a-card>
</template>

<script>
import { STable } from '@/components'
import { delRepairRemind, getRepairRemindList } from '@/api/admin/repair/repairRemind'
import { checkPermission } from '@/utils/permissions'

export default {
  name: 'TableList',
  components: {
    STable
  },
  data () {
    return {
      tabKey: '1',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      form: this.$form.createForm(this),
      mdl: {},
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 表头
      columns: [
        {
          title: '提醒类型',
          dataIndex: 'name',
          scopedSlots: { customRender: 'name' }
        },
        {
          title: '提醒内容',
          dataIndex: 'content',
          scopedSlots: { customRender: 'content' }

        },
        {
          title: '时间',
          dataIndex: 'createTime'
        },
        {
          title: '操作',
          width: '200px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      // loadData: parameter => {
      //   return getRepairList(Object.assign(parameter, this.queryParam))
      // },
      addEnable: checkPermission('admin:repair:add'),
      editEnabel: checkPermission('admin:repair:edit'),
      removeEnable: checkPermission('admin:repair:list'),
      readData: [],
      workerData: [],
      noReadData: []
    }
  },

  created () {
    if (this.$route.query.tabKey != null) this.tabKey = this.$route.query.tabKey
    this.workerList()
  },
  methods: {
    // admin/repair/stats/workerList
    workerList () {
      getRepairRemindList().then(res => {
        this.workerData = res.rows

        this.noReadData = res.rows.filter(item => item.read == 0)

        this.readData = res.rows.filter(item => item.read == 1)

      })
    },
    callback (value) {
      this.tabKey = value
    },

    handleAdd () {
      this.$refs.modal.add()
    },
    handleView (workerId) {
      this.$router.push({ name: 'repair', query: { repairUserId: workerId } })
    },

    toRepair (repairId) {
      this.$router.push({ name: 'repairView', query: { repairId: repairId } })
    },

    handleEdit (record) {
      this.$refs.modal.edit(record)
    },
    handleOk () {
      this.$refs.table.refresh(true)
      console.log('handleSaveOk')
    },
    hanleDelete (id) {
      const _this = this
      this.$confirm({
        title: '警告',
        content: '确认要删除工单吗?',
        okText: '是',
        okType: 'warning',
        cancelText: '否',
        onOk () {
          delRepairRemind({ id: id }).then(res => {
            if (res.code === 0) {
              _this.$message.success('操作成功')
              _this.workerList()
            } else {
              _this.$message.error(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          })
        },
        onCancel () {
        }
      })
    },
    handleComplete (id) {
      const _this = this
      this.$confirm({
        title: '警告',
        content: '确认要完成工单吗?',
        okText: '是',
        okType: 'warning',
        cancelText: '否',
        onOk () {
          complete(id).then(res => {
            if (res.code === 0) {
              _this.$message.success('操作成功')
              _this.$refs.table.refresh(true)
            } else {
              _this.$message.error(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          })
        },
        onCancel () {
        }
      })
    }
  },
  watch: {}
}
</script>
