<template>
    <el-dialog v-model="visible" title="政策关联" width="80%" top="5vh" @closed="$emit('closed')">
        <el-card class="tjm_card_style_custom" shadow="never">
            <div class="tjm_card_select">
                <el-form class="tjm_card_select_left" :model="params" inline label-width="80px" label-position="left">
                    <el-form-item label="政策编号">
                        <el-input v-model="params.businessNo" clearable placeholder="请输入政策编号"></el-input>
                    </el-form-item>
                    <el-form-item label="项目名称">
                        <el-input v-model="params.name" clearable placeholder="请输入项目名称"></el-input>
                    </el-form-item>
                    <el-form-item label="项目等级">
                        <el-select v-model="params.zcLevel" clearable placeholder="请选择项目等级">
                            <el-option v-for="item in levelDic" :key="item" :label="item" :value="item"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="项目类别">
                        <el-select v-model="params.zcType" clearable placeholder="请选择项目类别">
                            <el-option v-for="item in typeDic" :key="item" :label="item" :value="item"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="状态">
                        <el-select v-model="params.isLand" clearable placeholder="请选择状态">
                            <el-option v-for="(label, key) in statusDic" :key="key" :label="label" :value="parseInt(key)"></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="填报日期">
                        <el-date-picker v-model="createTime" type="daterange" value-format="YYYY-MM-DD" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" icon="search" @click="reloadTable">搜索</el-button>
                        <el-button icon="refresh-right" @click="reset">重置</el-button>
                    </el-form-item>
                </el-form>
            </div>
            <el-divider></el-divider>

            <div class="tjm_card_table">
                <el-table v-loading="loading" ref="selectStrive" row-key="id" header-cell-class-name="tjm_card_table_header" height="400" :data="tableData" border @select="select" @row-click="(row, column) => !column.fixed && column.type != 'selection' && table_detail(row)">
                    <el-table-column type="selection" align="center" width="55" :selectable="selectable"></el-table-column>
                    <el-table-column type="index" label="序号" width="55"></el-table-column>
                    <template v-for="(item, index) in link_columns" :key="index">
                        <el-table-column :label="item.label" :prop="item.props" :width="item.width || 180" show-overflow-tooltip>
                            <template #default="scope">{{ columnFormat(scope.row, item.props) }}</template>
                        </el-table-column>
                    </template>
                    <el-table-column label="操作" fixed="right" width="100">
                        <template #default="scope">
                            <el-button type="primary" link icon="tickets" @click="table_detail(scope.row)">详情</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="tjm_card_pagination">
                <yh-pagination v-model:pageNo="params.page" v-model:pageSize="params.size" :total="total" @paginationChange="reloadTable"></yh-pagination>
            </div>
        </el-card>

        <template #footer>
            <el-button type="primary" @click="visible = false, $emit('selected', selectData)">确 定</el-button>
        </template>
    </el-dialog>

    <policy-strive-detail v-if="dialog" ref="policyStriveDetail" @closed="dialog = false"></policy-strive-detail>
</template>

<script>
import API from "@/api/policy/strive";
import { levelDic, typeDic } from "@/views/policyShare/main";
import { statusDic } from "@/views/manage/policyStrive/main";
import { link_columns } from "@/views/publicDomain/main";

import yhPagination from "@/components/Pagination/index.vue";
import policyStriveDetail from "@/views/policyStrive/explain.vue";

export default {
    emits: ["closed"],
    props: {
        striveId: null
    },
    
    components: {
        yhPagination,
        policyStriveDetail
    },

    data() {
        return {
            link_columns, levelDic, typeDic, statusDic,
            visible: false,

            loading: false,
            createTime: [],
            params: {
                page: 1,
                size: 10,
                status: "done",
                isCase: 0
            },

            total: 0,
            tableData: [],

            dialog: false,
            selectData: null
        }
    },

    methods: {
        open() {
            this.visible = true;
            return this;
        },

        columnFormat(row, props) {
            if (props == "status") return statusDic[row.isLand] || "";
            if (props == "landAmount") return row.isLand == 1 && row[props] || "";
            return row[props];
        },

        reloadTable() {
            this.params.beginCreateTime = this.createTime && this.createTime.length && this.createTime[0] + " 00:00:00" || null;
            this.params.endCreateTime = this.createTime && this.createTime.length && this.createTime[1] + " 23:59:59" || null;

            this.loading = true;
            API.get(this.params).then(res => {
                this.loading = false;
                if (res.code === 200) {
                    this.tableData = res.data.records;
                    this.total = res.data.total;

                    if (this.striveId) { // 回显
                        nextTick(() => this.$refs.selectStrive.toggleRowSelection(res.data.records.find(r => r.id == this.striveId)))
                        this.selectData = Object.assign({}, res.data.records.find(r => r.id == this.striveId));
                    }
                } else ElMessage.error(res.msg);
            }).catch(() => this.loading = false);
        },

        reset() {
            this.createTime = [];
            this.params = {
                page: 1,
                size: 10,
                status: "done",
                isCase: 0
            }
                
            this.reloadTable();
        },

        table_detail(row) {
            this.dialog = true;
            nextTick(() => this.$refs.policyStriveDetail.open("detail").setData(row.id));
        },

        selectable(row) {
            return !this.selectData || this.selectData.id == row.id;
        },

        // 表格勾选事件
        select(rows, row) {
            this.selectData = rows.length && rows.indexOf(row) !== -1 && Object.assign({}, row) || null;
        }
    }
}
</script>

<style lang="scss" scoped>
.tjm_card_style_custom {
  border: none;

  :deep(.el-card__body) {
    padding: 0;

    .el-table__header .el-checkbox {
      display: none;
    }
  }
}
</style>