<template>
  <div>
    <div>
      <el-row class="handle-box">
        <el-col :span="24">
          <span class="card_title">认购</span>
        </el-col>
      </el-row>
      <el-divider />
      <el-row class="handle-box">
        <el-col :span="24">
          <el-table
            ref="multipleTable"
            v-loading="loading"
            :data="AllData"
            stripe
            row-class-name="g_table_row"
            :header-cell-style="{textAlign: 'center'}"
            :cell-style="{ textAlign: 'center' }"
            @selection-change="handleSelectionChange"
          >

            <el-table-column
              type="selection"
              width="55"
            />
            <el-table-column type="index" label="序号" width="60" />
            <el-table-column label="单元/楼栋号" prop="buildName" width="180" />
            <el-table-column label="户室号" prop="roomNo" />
            <el-table-column label="认购金金额（元）" prop="receivableMoney" />
            <el-table-column label="收取状态" prop="statusStr" />
            <el-table-column label="收据流水号" prop="serialNumber" />
            <el-table-column label="经办人" prop="createdId" />
            <el-table-column label="经办时间" prop="collectionTime" />
            <el-table-column label="操作" width="180">
              <template scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  @click="handleAdd(scope.row)"
                >登记
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  @click="handleEdit(scope.row)"
                > 修改
                </el-button>
                <el-button
                  size="mini"
                  type="text"
                  @click="downLoad(scope.row)"
                > 定金收据
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </div>
    <div style="text-align: right;margin-top: 50px">
      <el-button @click="cancel">取 消</el-button>
      <el-button type="primary" @click="confirmSubmit()">确 定</el-button>
    </div>

    <!--认购金登记-->
    <el-dialog
      :visible.sync="dialogAddVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      title=""
      width="90%"
      top="20px"
      class="statistic_base"
      :append-to-body="true"
      :modal-append-to-body="true"
      custom-class="tagdialog"
      @close="getData"
    >
      <add-subscribe v-if="dialogAddVisible" ref="addSubscribe" @cancel="cancel" />
    </el-dialog>

  </div>
</template>

<script>
import Base from '@/views/base/base.vue'
import BaseData from '@/views/base/baseData.vue'
import AddSubscribe from '@/views/customerManagement/subscribe/addSubscribe.vue'

export default {
    name: 'AddHouse',
    components: { AddSubscribe },
    mixins: [Base, BaseData],
    data() {
        return {
            dc_key: ['DECORATION_SITUATION'],
            loading: false,
            AllData: [],
            customerManagementId: '',
            dataStr: '',
            username: '',
            dialogAddVisible: false
        }
    },
    mounted() {
        const myDate = new Date()
        const dateStr = myDate.getFullYear() + '-' + (myDate.getMonth() + 1) + '-' + myDate.getDate()
        const username = this.$common.currUser().username
        this.dataStr = dateStr
        this.username = username
    },
    methods: {
        initData(data) {
            this.initDict(this.dc_key).then(res => {
                this.customerManagementId = data.id
                this.getData(data.id)
            })
        },
        handleAdd() {
            this.dialogAddVisible = true
            // 新vue时调用的方法
            this.$nextTick(() => {
                this.$refs.addSubscribe.initData()
            })
        },
        handleEdit() {

        },
        getData: function(val) {
            const _this = this
            _this.loading = true
            _this.AllData = []
            const data = {
                customerManagementId: val
            }
            this.baseRequest('listAll', data).then((res) => {
                if (res.data) {
                    res.data.forEach(function(item) {
                        const json = _this.getItemJson(item)
                        _this.AllData.push(json)
                    })
                }
                _this.loading = false
            }).catch(() => {
            })
        },
        getItemJson: function(item) {
            item.statusStr = item.status === 1 ? '未收取' : '已收取'
            return item
        },
        confirmSubmit: function() {
            if (!this.AllData || this.AllData.length === 0) {
                this.$message({
                    message: '请选择房间',
                    type: 'warning'
                })
            }
            const data = {
                houseIds: this.AllData.map(obj => { return obj.id }).join(','),
                customerManagementId: this.customerManagementId
            }
            this.baseRequest('submit', data).then(res => {
                if (res.data.code === 200) {
                    this.$message({
                        message: '提交成功',
                        type: 'success'
                    })
                    this.cancel()
                }
            }).catch((err) => {
                this.$message({
                    message: err,
                    type: 'error'
                })
            })
        },
        cancel() {
            this.$emit('cancel')
        },
        downLoad() {

        },
        baseRequest(opUrl, postData) {
            return this.$channel.globeRequest('RoomSelectionInfoController', opUrl, postData, 'project')
        },
        baseInfoRequest: function(opUrl, postData) {
            return this.$channel.baseRequest('ParkInfoController', opUrl, postData, 'User')
        }

    }
}
</script>

<style scoped>

</style>
