<template>
    <div v-if="show" class="reply-policy-detail">
        <policy-detail ref="policyDetail" @closed="show = false"></policy-detail>
    </div>

    <template v-else>
        <el-card class="tjm_card_style_custom">
            <div class="tjm_card_title">条件检索</div>
            <div class="tjm_card_select">
                <el-scrollbar>
                    <el-form class="tjm_card_select_left" :model="params" label-width="80px" label-position="left">
                        <el-row :gutter="15">
                            <el-col :lg="8" :md="12" :xs="24">
                                <el-form-item label="政策编号">
                                    <el-input v-model="params.businessNo" clearable placeholder="请输入政策编号"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :lg="8" :md="12" :xs="24">
                                <el-form-item label="政策名称">
                                    <el-input v-model="params.name" clearable placeholder="请输入政策名称"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :lg="8" :md="12" :xs="24">
                                <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-col>
                            <template v-if="paramsIsCollapse">
                                <el-col :lg="8" :md="12" :xs="24">
                                    <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-col>
                                <el-col :lg="8" :md="12" :xs="24">
                                    <el-form-item label="是否入库">
                                        <el-select v-model="params.isInWh" clearable placeholder="请选择入库状态">
                                            <el-option v-for="(label, key) in whetherDic" :key="key" :label="label" :value="parseInt(key)"></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :lg="8" :md="12" :xs="24">
                                    <el-form-item label="入库类别">
                                        <el-select v-model="params.inWhType" clearable placeholder="请选择入库类别">
                                            <el-option v-for="item in storageTypeDic" :key="item" :label="item" :value="item"></el-option>
                                        </el-select>
                                    </el-form-item>
                                </el-col>
                                <el-col :lg="8" :md="12" :xs="24">
                                    <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-col>
                            </template>
                        </el-row>            
                    </el-form>
                    <div class="tjm_card_select_right">
                        <el-button type="primary" icon="search" @click="reloadTable">搜索</el-button>
                        <el-button icon="refresh-right" @click="reset">重置</el-button>
                        <el-button type="primary" link @click="paramsIsCollapse = !paramsIsCollapse">
                            <template v-if="paramsIsCollapse">收起<tjm-icon-ep-arrow-up-bold /></template>
                            <template v-else>更多<tjm-icon-ep-arrow-down-bold /></template>
                        </el-button>
                    </div>
                </el-scrollbar>
            </div>          
            <el-divider></el-divider>

            <div v-loading="loading" class="tjm_card_reply">
                <el-empty v-if="total <= 0"></el-empty>
                <template v-else>
                    <template v-for="(item, index) in tableData" v-bind:key="index">
                        <el-descriptions direction="vertical" :column="columns.length + 1" border @click="table_detail(item)">
                            <el-descriptions-item v-for="column in columns" :key="column.props" :min-width="column.width || 180">
                                <template #label>{{ column.label }}</template>

                                <span v-if="column.props == 'index'">{{ index + 1 }}</span>
                                <span v-else class="desc-cell">
                                    <yh-tooltip :content="columnFormat(item, column.props)" placement="top"></yh-tooltip>
                                </span>
                            </el-descriptions-item>
                        </el-descriptions>
                        
                        <div class="tjm_card_reply__body">
                            <yh-reply-card :refId="item.id" @replyAll="table_detail(item, $event)"></yh-reply-card>
                        </div>
                    </template>
                </template>
            </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>
</template>

<script>
import API from "@/api/policy/share";
import { levelDic, typeDic, storageTypeDic, whetherDic } from "../main";
import { columns } from "./main";

import yhTooltip from "@/components/Tooltip/index.vue";
import yhReplyCard from "@/components/ReplyCard/index.vue";
import yhPagination from "@/components/Pagination/index.vue";
import policyDetail from "@/views/home/policyDetail.vue";

export default {
    components: {
        yhTooltip,
        yhReplyCard,
        yhPagination,
        policyDetail
    },

    data() {
        return {
            columns, levelDic, typeDic, storageTypeDic, whetherDic,

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

            show: false
        }
    },

    mounted() {
        this.reloadTable();
    },

    methods: {
        columnFormat(row, props) {
            if (props == "createTime") return row[props].split(" ")[0];
            if (props == "isInWh") return whetherDic[row[props]] || "";
            if (props == "inWhType") return row.isInWh == 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;
                } else ElMessage.error(res.msg);
            }).catch(() => this.loading = false);
        },

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

        table_detail(row, count) {
            this.show = true;
            nextTick(() => this.$refs.policyDetail.setData(row, count));
        }
    }
}
</script>

<style lang="scss" scoped>
.reply-policy-detail :deep(.tjm_card_style_custom) > .el-card__body {
  max-height: calc(
    $base-main-height - $base-padding - 2 * var(--el-card-padding) - 54px
  );
  overflow: auto;
}

.tjm_card_reply .el-descriptions {
  overflow-x: auto;
  border-left: var(--el-descriptions-table-border);
  border-right: var(--el-descriptions-table-border);

  :deep(.el-descriptions__body) tr {
    .el-descriptions__cell:first-child {
      border-left: none;
    }

    .el-descriptions__cell:last-child {
      border-right: none;
    }
  }

  .desc-cell {
    display: inline-block;
    width: 100%;
  }
}

.tjm_card_reply .tjm_card_reply__body {
  margin-bottom: 25px;
  border: 1px solid var(--el-border-color-lighter);
  border-top: none;

  > :deep(.el-card.reply-card) {
    border-radius: 0;
  }
}
</style>