<template>
    <div v-if="detail.show" class="domain-policy-detail">
        <el-page-header title="返回" icon="arrow-left" :content="detail.name" @back="detail.show = false"></el-page-header>
        <policy-detail ref="policyDetail"></policy-detail>
    </div>

    <el-card v-else class="tjm_card_style_custom">
        <div class="tjm_card_title">查询表格</div>
        <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.inWhType" clearable placeholder="请选择入库类别">
                        <el-option v-for="item in storageTypeDic" :key="item" :label="item" :value="item"></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" row-key="id" header-cell-class-name="tjm_card_table_header" height="400" :data="tableData" border @row-click="table_detail">
                <el-table-column type="index" label="序号" width="55"></el-table-column>
                <template v-for="(item, index) in columns" :key="index">
                    <el-table-column :label="item.label" :prop="item.props" :min-width="item.width || 180" show-overflow-tooltip>
                        <template #default="scope">{{ columnFormat(scope.row, item.props) }}</template>
                    </el-table-column>
                </template>
            </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>

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

import yhPagination from "@/components/Pagination/index.vue";
import policyDetail from "@/views/home/policyDetail.vue";

export default {
    components: {
        yhPagination,
        policyDetail
    },

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

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

            total: 0,
            tableData: [],

            detail: {
                show: false,
                name: null
            }
        }
    },

    mounted() {
        if (this.$route.query.zcType) this.params["zcType"] = this.$route.query.zcType;
        if (this.$route.query.inWhType) this.params["inWhType"] = this.$route.query.inWhType;
        if (this.$route.query.name) this.params["name"] = this.$route.query.name;

        this.reloadTable();
    },

    methods: {
        columnFormat(row, props) {
            if (props == "createTime") return row[props].split(" ")[0];
            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,
                isInWh: 1,
                status: "done"
            }
            if (this.$route.query.zcType) this.params["zcType"] = this.$route.query.zcType;
            if (this.$route.query.inWhType) this.params["inWhType"] = this.$route.query.inWhType;
            
            this.reloadTable();
        },

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

<style lang="scss" scoped>
.domain-policy-detail {
  .el-page-header {
    position: relative;
    top: calc(-1 * $base-padding);
    left: calc(-1 * $base-padding);
    width: calc(100% - $base-padding);
    padding: 15px;
    background: #fff;
    box-shadow: var(--el-box-shadow-light);

    :deep(.el-page-header__back:hover) {
      color: var(--el-color-primary);
    }
  }

  :deep(.tjm_card_style_custom) > .el-card__body {
    max-height: calc(
      $base-main-height - $base-padding - 2 * var(--el-card-padding) - 54px
    );
    overflow: auto;
  }
}
</style>