<template>
    <div class="custom-table">
        <el-table
                :data="list"
                border
                v-loading="loading">
            <el-table-column type="index" align="center" label="序号" width="80"/>
            <el-table-column prop="dwmc" label="抽查单位" show-overflow-tooltip align="center"/>
            <el-table-column prop="dwlx" show-overflow-tooltip label="单位类别"/>
            <el-table-column prop="dwlxr" show-overflow-tooltip label="消防安全责任人" />
            <el-table-column prop="dwlxrdh" show-overflow-tooltip label="责任人电话"/>
            <el-table-column prop="sjjg" label="所属检查机构信息" show-overflow-tooltip align="center"/>
            <el-table-column prop="jcr_z" label="检查人(主协)" align="center" width="120">
                <template slot-scope="scope">
                    <span>{{scope.row.jcr_z ? scope.row.jcr_z : ''}}{{scope.row.jcr_x ? ',' + scope.row.jcr_x : ''}}</span>
                </template>
            </el-table-column>
            <el-table-column prop="gzrq" label="检查时间" show-overflow-tooltip align="center">
                <template slot-scope="scope">{{ formatDate(scope.row.gzrq, true) }}</template>
            </el-table-column>
            <el-table-column prop="jcjgzt" label="检查结果" show-overflow-tooltip/>
            <el-table-column prop="action" label="操作" align="center" width="80">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="handleDetail(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>
        <el-dialog title="选择流程" :visible.sync="showTemplate" v-if="showTemplate">
            <choose-template-v2 @changeTemplate="handleTemplate" :place_type="place_type"
                                :enforce_obj_type="enforce_obj_type">
            </choose-template-v2>
        </el-dialog>
        <el-dialog title="设置标签" :visible.sync="showTagDialog" v-if="showTagDialog">
            <!--<tag-setting :obj_id="objId" @hide="hideTagDialog"></tag-setting>-->
        </el-dialog>
    </div>
</template>

<script>
    import {getEnforceList, removeEnforce, updateEnforce, getEnforceItem} from '@/api/resource'
    import {getExcutorRecordList} from '@/api/random'
    import {getPlaceTypes} from '@/api/resource'
    import {resourceMixin} from '@/mixin/resource'
    import ChooseTemplateV2 from '@/component/ChooseTemplateV2.vue'
    import { formatDate, dateFormat } from '@/utils/tool'
    // import TagSetting from './TagSetting.vue'

    export default {
        computed: {
            pageTotal() {
                const page = Math.ceil(this.total / this.query.size)
                return page > 0 ? page : 1
            }
        },
        components: {
            ChooseTemplateV2,
            // TagSetting,
        },

        mixins: [resourceMixin],
        data() {
            return {
                query: {
                    page: 1,
                    size: 10,
                    query: null,
                    // tag_id: null,
                    sort: 'gzrq desc'
                    // query=enforce_obj_name==重庆东田实业有限公司;legal_person==法人
                },

                formData: {
                    enforce_obj_name: '',
                    legal_person: '',
                    unitSize: '',
                    enforce_org_id: '',
                    safe: '',
                    check: '',
                },
                showTemplate: false,
                loading: false,
                list: [],
                placeTypeList: [],
                total: 0,
                objId: -1,
                objData: {},
                place_type: '',
                enforce_obj_type: '',
                showTagDialog: false,
            }
        },
        mounted() {
            this.getPlaceTypesAll();
            this.handleQuery()
        },
        methods: {
            formatDate,
            async getPlaceTypesAll() {
                const list = await getPlaceTypes();
                if (list && list.length > 0) {
                    this.placeTypeList = list;
                }
            },
            configProcess(objId) {
                this.objId = objId;
                getEnforceItem(this.objId).then((res) => {
                    this.objData = res;
                    this.enforce_obj_type = this.objData.enforce_obj_type
                    this.place_type = this.objData.place_type
                    this.showTemplate = true;
                });
                // this.showTemplate=true;
            },
            handleTemplate(data) {
                // let result ={
                //     enforce_template_id:data.id,
                //     enforce_template_name:data.template_name
                // };
                this.objData.enforce_template_id = data.id;
                this.objData.enforce_template_name = data.template_name;
                updateEnforce(this.objId, this.objData).then(() => {
                    this.$message.success('修改成功')
                    this.showTemplate = false;
                }).catch(reason => {
                    this.$message.error('修改失败')
                    this.showTemplate = false;
                })
            },
            getPlaceTypeName(placeType) {
                let name = "";
                this.placeTypeList.forEach(item => {
                    if (item.code === placeType) {
                        name = item.name;
                    }
                });
                return name;
            },
            resetForm(data) {
                this.query.query = data
                this.query.page = 1
                this.query.size = 10
                this.handleQuery()
            },
            handleSizeChange(val) {
                this.query.size = val
                this.handleQuery()
            },
            handleCurrentChange(val) {
                this.query.page = val
                this.handleQuery()
            },
            handleQueryParam() {
                const res = []
                Object.keys(this.formData).forEach(key => {
                    if (this.formData[key]) {
                        res.push(`${key}==${this.formData[key]}`)
                    }
                })
                this.query.query = res.join(';')
            },
            handleQuery() {
                this.loading = true
                const vm = this
                getExcutorRecordList(this.query).then(res => {
                    if (res) {
                        vm.total = res.total || 0
                        const list = res.data || []
                        list.map(item => {
                            item.unitSize = vm.parseUnitSize(item.enforce_obj_type)
                            item.status = vm.parseStatus(item.last_enforce_status)
                        })
                      list.sort((a, b) => {
                        return (new Date(b.gzrq)).getTime() - (new Date(a.gzrq)).getTime()
                      })
                        vm.list = list
                    }
                    vm.loading = false
                }).catch(e => {
                  vm.loading = false
                })
            },
            handleDetail(id) {
                this.$router.push({
                    path: '/random/record/detail',
                    query: {
                        id
                    }
                })
            },
            handleTag(id) {
                this.objId = id
                this.showTagDialog = true
            },
            handleAddResource() {
                this.$router.push('/resource/add')
            },
            handleEdit(id) {
                this.$router.push({
                    path: '/resource/edit',
                    query: {
                        id
                    }
                })
            },
            handleDelete(data) {
                this.$confirm(`是否删除执法对象 ${data.enforce_obj_name}?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    removeEnforce(data.id).then(res => {
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        })
                        this.handleCurrentChange(1)
                    })
                })
            },
            hideTagDialog() {
                this.showTagDialog = false
            },
        }
    }
</script>
