<template>
    <div class="custom-table">
        <el-table
                :data="tableData"
                border
                style="width: 100%"
                v-loading="loading">
            <el-table-column type="index" align="center" label="序号" width="80"/>

            <el-table-column prop="media_id" show-overflow-tooltip label="记录编号" align="center">
                <template slot-scope="scope">{{scope.row.media_id }}</template>
            </el-table-column>
            <el-table-column prop="video_name" show-overflow-tooltip="" label="视频名称"></el-table-column>
            <el-table-column prop="video_type" show-overflow-tooltip="" label="视频分类"></el-table-column>
            <el-table-column prop="recordTime" show-overflow-tooltip label="记录时间" align="center"></el-table-column>
            <el-table-column prop="hik_device_id" show-overflow-tooltip label="执法地点" align="center">
                <template slot-scope="scope">{{scope.row.address }}</template>
            </el-table-column>
            <el-table-column prop="org_name" show-overflow-tooltip label="执法设备" align="center">
                <template slot-scope="scope">{{scope.row.device_name }}</template>
            </el-table-column>
            <el-table-column prop="enforce_org_name" show-overflow-tooltip label="执法机构" align="center" />
            <el-table-column prop="use_person_name" show-overflow-tooltip label="使用人员" align="center">
                <template slot-scope="scope">{{scope.row.use_person_name }}</template>
            </el-table-column>

            <el-table-column prop="device_status" show-overflow-tooltip label="任务关联" align="center">
                <template slot-scope="scope">
                     <span v-if="scope.row.enfore_job_id">
                        <i class="el-icon-s-opportunity success"></i>
                        已关联
                    </span>
                    <span v-else>
                         <i class="el-icon-s-opportunity danger"></i>
                        未关联
                    </span>
                </template>
            </el-table-column>
            <el-table-column prop="action" label="操作" align="center">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="handleDetail(scope.row.id)">查看</el-button>
                    <el-button type="text" size="small" @click="handleEdit(scope.row.id)">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
        <div class="custom-pagination">
            <div class="info">
                <span>共 {{ total }} 条记录</span>
                <span>第 {{ query.page }} / {{ pageTotal }} 页</span>
            </div>
            <el-pagination
                    background
                    class="custom-page"
                    layout="prev, pager, next, sizes"
                    :page-sizes="[10, 20, 50, 100]"
                    :current-page="query.page"
                    :page-size="query.size"
                    :total="total"
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange">
            </el-pagination>
        </div>
    </div>
</template>

<script>
    import {getVideo} from '@/api/device'
    import {jobMixin} from '@/mixin/job'
    import {resourceMixin} from '@/mixin/resource'
    import {formatDate} from '@/utils/tool'

    export default {
        computed: {
            pageTotal() {
                const page = Math.ceil(this.total / this.query.size)
                return page > 0 ? page : 1
            }
        },
        mixins: [jobMixin, resourceMixin],
        data() {
            return {
                loading: false,
                formData: {
                    job_status: '',
                    org: '',
                    account: '',
                    checkDate: '',
                    company: '',
                    limit: '',
                    createDate: '',
                    result: '',
                },
                query: {
                    page: 1,
                    size: 10,
                    sort: 'video_begin_time desc',
                },
                total: 0,
                tableData: []
            }
        },
        mounted() {
            this.handleQuery()
        },
        methods: {
            formatDate,
            edite(data) {
                this.$emit("editData", data)
            },
            dispatchUser(id) {
                this.$emit("dispatchUser", id)
            },
            deleteData(id) {
                this.$emit("deleteData", id)
            },
            bindDe(device) {
                this.$emit("bindDe", device)
            },

            detail(id){
                this.$emit("detail", id)
            },
            resetForm(data) {
                this.query.query = data
                this.handleQuery()
            },
            handleSizeChange(val) {
                this.query.size = val
                this.handleQuery()
            },
            handleCurrentChange(val) {
                this.query.page = val
                this.handleQuery()
            },
            handleQuery() {
                this.loading = true
                this.loading = true
                if (!this.query.query) {
                    this.query.query = ""
                }
                getVideo(this.query).then(res => {
                    if (res) {
                        this.total = res.total || 0
                        const list = res.data || []
                        list.map(item => {
                            item.recordTime = formatDate(item.video_begin_time, true);
                        })
                        this.tableData = list
                    }
                    this.loading = false
                })
            },
            handleDetail(id) {
                this.$router.push({
                    path: '/device/video/detail',
                    query: {
                        id
                    }
                })
            },
            handleEdit(id) {
                this.$router.push(`/device/video/edit/${id}`);
            }
        }
    }
</script>