<template>
  <div>
    <el-row class="handle-box" style="margin-bottom: 10px">
      <el-col :span="2">
        <div style="text-align: center"><span>楼盘</span></div>
      </el-col>
      <el-col :span="4">
        <el-select
          v-model="search.groupId"
          style="width: 100%"
          clearable
          filterable
          @change="groupChange"
        >
          <el-option
            v-for="item in groupOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-col>
      <el-col :span="2">
        <div style="text-align: center"><span>分期</span></div>
      </el-col>
      <el-col :span="4">
        <el-select
          v-model="search.discId"
          style="width: 100%"
          clearable
          filterable
          @change="discChange"
        >
          <el-option
            v-for="item in discOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-col>
      <el-col :span="2">
        <div style="text-align: center"><span>单元/楼栋</span></div>
      </el-col>
      <el-col :span="4">
        <el-select
          v-model="search.buildId"
          style="width: 100%"
          clearable
          filterable
          @change="handleSearch"
        >
          <el-option
            v-for="item in buildOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-col>
      <el-col :span="2">
        <div style="text-align: center"><span>关键字</span></div>
      </el-col>
      <el-col :span="4">
        <el-input v-model="search.roomNo" />
      </el-col>
    </el-row>
    <el-row class="handle-box" style="margin-bottom: 10px">
      <el-col :span="24" style="margin-top: 20px">
        <el-button :loading="excelFlag" size="small" class="ch-button-export" style="float: right;" @click="handleExcel"><i class="el-icon-menu" />&nbsp;导出EXCEL</el-button>
        <el-button class="ch-button-warning" size="small" style="float: right" @click="handleReset()"><i class="el-icon-search" />&nbsp;重置</el-button>
        <el-button class="ch-button" size="small" style="float: right" @click="handleSearch()"><i class="el-icon-search" />&nbsp;查询</el-button>
      </el-col>
    </el-row>
    <el-row class="handle-box">
      <el-col :span="24">
        <el-table v-loading="loading" :data="AllData" row-class-name="g_table_row" stripe>
          <el-table-column label="序号" type="index" width="60" />
          <el-table-column label="楼盘" prop="groupName" />
          <el-table-column label="分期" prop="discName" />
          <el-table-column label="单元/楼栋号" prop="buildName" />
          <el-table-column label="户室号" prop="roomNo" />
          <el-table-column label="套内面积（㎡）" prop="actualInternalArea" />
          <el-table-column label="建筑面积(㎡)" prop="actualBuildArea" />
          <el-table-column label="装修情况" prop="decorationSituationStr" />
          <el-table-column label="交易次数" prop="transactionCount" />
          <el-table-column header-align="center" label="操作" width="180">
            <template scope="scope">
              <el-button size="mini" type="text" @click="handleView(scope.row)">详情档案</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-page">
          <el-pagination
            :current-page.sync="currentPage"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="pageSize"
            background
            layout="total, sizes, prev, pager, next, jumper"
            :total="allpage"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </div>
      </el-col>
    </el-row>

    <!-- 详情页面 -->
    <el-dialog
      :title="'详情'"
      :visible.sync="dialogVisible"
      width="75%"
      top="50px"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
      append-to-body
    >
      <park-room-index-detail
        v-if="dialogVisible"
        ref="parkRoomIndexDetail"
      />
    </el-dialog>

  </div>
</template>
<script>
import Base from '@/views/base/base'
import BaseData from '@/views/base/baseData'
import ParkRoomIndexDetail from '@/views/reportForms/components/parkRoomIndexDetail.vue'

export default {
    name: 'ParkRoomArchives',
    components: { ParkRoomIndexDetail },
    mixins: [Base, BaseData],
    data() {
        return {
            dc_key: ['DECORATION_SITUATION', 'MAPPING_STATUS', 'SOLD_STATUS', 'GROUP_NATURE', 'HOUSE_PAYMENT_STATUS'],
            // 列表相关
            search: {
            },
            AllData: [],
            loading: false,
            // 弹框相关
            dialogVisible: false,
            rules: {},
            form: {},
            dialogTitle: '新增',
            groupOption: [],
            discOption: [],
            buildOption: [],
            excelFlag: false
        }
    },
    mounted() {
        this.getGroupOption()
        // this.getDiscOption()
        // this.getBuildOption()
        this.initDict(this.dc_key).then((res) => {
            this.getData()
        })
    },
    methods: {
        groupChange() {
            this.handleSearch()
            const data = {
                groupId: this.search.groupId
            }
            this.getDiscOption(data)
        },
        discChange() {
            this.handleSearch()
            const data = {
                discId: this.search.discId
            }
            this.getBuildOption(data)
        },
        getData: function() {
            const _this = this
            _this.loading = true
            _this.AllData = []

            this.search.pageNum = this.currentPage
            this.search.pageSize = this.pageSize
            this.baseRequest('reportList', this.search).then((res) => {
                if (res.data.rows) {
                    res.data.rows.forEach(function(item) {
                        const json = _this.getItemJson(item)
                        _this.AllData.push(json)
                    })
                    _this.allpage = res.data.total
                }
                _this.loading = false
            }).catch((e) => {
                // console.log(e)
            })
            // this.initOutData()
        },
        handleSearch: function() {
            this.getData()
        },
        handleReset: function() {
            for (const i in this.search) {
                if (i !== 'pageNum' && i !== 'pageSize') {
                    this.search[i] = ''
                }
            }
            this.handleSearch()
        },
        getItemJson: function(item) {
            item.decorationSituationStr = this.dc_map.DECORATION_SITUATION[item.decorationSituation]
            item.areaStatusStr = this.dc_map.MAPPING_STATUS[item.areaStatus]
            item.soldStatusStr = this.dc_map.SOLD_STATUS[item.soldStatus]
            item.natureStr = this.dc_map.GROUP_NATURE[item.nature]
            item.collectionStatusStr = this.dc_map.HOUSE_PAYMENT_STATUS[item.collectionStatus]
            return item
        },
        cancel: function() {
            this.dialogVisible = false
        },
        handleView(val) {
            val.activeName = '概览'
            this.dialogVisible = true
            this.$nextTick(() => {
                this.$refs.parkRoomIndexDetail.initData(val)
            })
        },
        confirmSubmit: function() {
            const _this = this

            this.$refs.form.validate(valid => {
                if (valid) {
                    let soaUrl = 'edit'
                    const extraData = {}
                    const postData = Object.assign({}, _this.form, extraData)

                    if (!this.form.id) {
                        soaUrl = 'add'
                    }
                    this.opRecord(postData, soaUrl)
                } else {
                    // console.log('error submit!!')
                    return false
                }
            })
        },
        getGroupOption() {
            this.baseGroupRequest('listAll', {}).then((res) => {
                const data = res.data
                this.groupOption = []
                data.forEach(item => {
                    const obj = {
                        value: item.groupId,
                        label: item.groupName,
                        key: item.groupId
                    }
                    this.groupOption.push(obj)
                })
            })
        },
        getDiscOption(data) {
            this.baseDiscRequest('listAll', data).then((res) => {
                const data = res.data
                this.discOption = []
                data.forEach(item => {
                    const obj = {
                        value: item.id,
                        label: item.name,
                        key: item.id
                    }
                    this.discOption.push(obj)
                })
            })
        },
        getBuildOption(data) {
            this.baseBuildRequest('listAll', data).then((res) => {
                const data = res.data
                this.buildOption = []
                data.forEach(item => {
                    const obj = {
                        value: item.id,
                        label: item.buildNum,
                        key: item.id
                    }
                    this.buildOption.push(obj)
                })
            })
        },
        // 导出
        handleExcel: function() {
            const _this = this
            _this.excelFlag = true
            _this.search.pageNum = _this.currentPage
            _this.search.pageSize = _this.pageSize
            this.OutData = []
            const title = ['房屋代码', '小区名称', '门牌', '户号', '施工号', '面积测绘状态', '销售状态', '房屋类别',
                '预测套内面积', '预测分摊面积', '预测建筑面积', '预测土地面积', '实测套内面积', '实测分摊面积', '实测建筑面积', '实测土地面积',
                '房屋总价', '备注', '选房时间', '批次号', '选房号', '网签合同备案号', '合同编号', '签约时间', '合同买受人', '合同买受人身份证',
                '买受人联系方式', '房屋买卖单价（元/㎡）', '总价款（元）', '买受人出资金额（元）', '应收房款（元）', '已付房款（元）',
                '尚欠房款（元）', '收款状态', '已付款详情', '实际入住时间'
            ]
            this.OutData.push(title)
            const temp = []
            this.baseRequest('projectHouseAllVoList', _this.search).then(res => {
                const data = res.data
                data.data.forEach(function(item) {
                    const json = _this.getItemJson(item)
                    temp.push(json)
                })
                temp.forEach(function(item) {
                    const jsonArray = []
                    jsonArray.push(item.roomNumber)
                    jsonArray.push(item.groupName)
                    jsonArray.push(item.buildName)
                    jsonArray.push(item.roomNo)
                    jsonArray.push(item.constructionNum)
                    jsonArray.push(item.areaStatusStr)
                    jsonArray.push(item.soldStatusStr)
                    jsonArray.push(item.natureStr)
                    jsonArray.push(item.predictionInternalArea)
                    jsonArray.push(item.predictionShareArea)
                    jsonArray.push(item.predictionBuildArea)
                    jsonArray.push(item.predictionLandArea)
                    jsonArray.push(item.actualInternalArea)
                    jsonArray.push(item.actualShareArea)
                    jsonArray.push(item.actualBuildArea)
                    jsonArray.push(item.actualLandArea)
                    jsonArray.push(item.houseTotalPrice)
                    jsonArray.push(item.remark)
                    jsonArray.push(item.roomSelectionDate)
                    jsonArray.push(item.batchNumber)
                    jsonArray.push(item.roomSelectionNumber)
                    jsonArray.push(item.recordNumber)
                    jsonArray.push(item.contractNumber)
                    jsonArray.push(item.signingDate)
                    jsonArray.push(item.buyerName)
                    jsonArray.push(item.buyerIdentityCard)
                    jsonArray.push(item.buyerPhone)
                    jsonArray.push(item.contractHousePrice)
                    jsonArray.push(item.contractTotalPrice)
                    jsonArray.push(item.contractBuyerMoney)
                    jsonArray.push(item.receivableMoney)
                    jsonArray.push(item.receivedMoney)
                    jsonArray.push(item.arrears)
                    jsonArray.push(item.collectionStatusStr)
                    jsonArray.push(item.payInfo)
                    jsonArray.push(item.checkInDate)
                    _this.OutData.push(jsonArray)
                })
                const OutSize = [{ wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 },
                    { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 },
                    { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }, { wch: 15 }]
                const fileName = '房源档案导出 ' + new Date().Format('yyyyMMddhhmm')
                this.$outputXlsxFile(this.OutData, OutSize, fileName)
                _this.excelFlag = false
            })
        },
        baseRequest(opUrl, postData) {
            return this.$channel.globeRequest('ParkRoomController', opUrl, postData, 'project')
        },
        baseGroupRequest(opUrl, postData) {
            return this.$channel.globeRequest('ParkInfoController', opUrl, postData, 'project')
        },
        baseDiscRequest(opUrl, postData) {
            return this.$channel.globeRequest('ParkFloorDiscController', opUrl, postData, 'project')
        },
        baseBuildRequest(opUrl, postData) {
            return this.$channel.globeRequest('MnpBuildingController', opUrl, postData, 'project')
        }
    }
}
</script>

<style scoped>
.zt_css{
    overflow:  hidden;
    white-space: nowrap;
    text-overflow:ellipsis;
}
.ch-input .el-input__inner {
    border-color: #32323A;
}
.ch-input-size {
    width: 150px;
}
.ch-button {
    border-color: #32323A;
    background-color: #32323A;
    color: #fff;
}
.ch-button-warning {
    margin-left: 10px;
    border-color: #E6A23C;
    background-color: #E6A23C;
    color: #fff;
}
.ch-button-export {
    margin-left: 10px;
    border-color: #98CC1F;
    background-color: #98CC1F;
    color: #fff;
}
/deep/.el-dialog__header {
    padding: 10px 20px;
}
/deep/.el-dialog__body {
    padding: 10px 20px;
}
</style>
