<template>
  <div>
    <el-row class="handle-box" style="margin-bottom: 10px">
      <el-col :span="24">
        <span>
          姓名
        </span>
        <el-input
          v-model="search.buyerName"
          style="margin-left:16px ;"
          size="small"
          placeholder="请输入"
          class="ch-input ch-input-size"
        />
        <el-radio-group v-model="search.status" style="margin-left:16px ;">
          <el-radio
            v-for="item in dc_data.BUYING_HOUSE_STATUS"
            :key="item.value"
            :label="item.value"
          >
            {{ item.label }}
          </el-radio>
        </el-radio-group>
      </el-col>
      <el-col :span="24">
        <div style="display: flex;align-items: center">
          <div style="    white-space: nowrap;">项目</div>
          <div class="listBox">
            <div
              v-for="item in groupOption"
              :key="item.value"
              class="list"
              :class="{ checked: groupIds.includes(item.value) }"
              @click="checkedBox(item.value)"
            >
              {{ item.label }}
            </div>

          </div>
        </div>
      </el-col>
      <el-col v-if="discOption.length > 0" :span="24">
        <div style="display: flex;align-items: center">
          <div style="    white-space: nowrap;">分期</div>
          <div class="listBox">
            <div
              v-for="item in discOption"
              :key="item.value"
              class="list"
              :class="{ checked: discIds.includes(item.value) }"
              @click="checkedFQBox(item.value)"
            >
              {{ item.label }}
            </div>
          </div>
        </div>
      </el-col>
      <el-col :span="24" style="margin-top: 20px">
        <el-button size="small" class="ch-button" style="float: right; margin-top: 4px" @click="downLoad"><i class="el-icon-menu" />&nbsp;选房通知单</el-button>
        <el-button size="small" class="ch-button-danger" style="float: right; margin-top: 4px;margin-right: 10px;" @click="handleDelete"><i class="el-icon-menu" />&nbsp;删除</el-button>
        <el-button size="small" class="ch-button-export" style="float: right;margin-top: 4px" @click="batchImport()"><i class="el-icon-menu" />&nbsp;批量导入</el-button>
        <el-button size="small" class="ch-button-add" style="float: right; margin-right: 10px; margin-top: 4px" @click="handleAdd()"><i class="el-icon-menu" />&nbsp;新增</el-button>
        <el-button size="small" class="ch-button" style="float: right; margin-top: 4px" @click="handleSearch()"><i class="el-icon-menu" />&nbsp;查询</el-button>
        <el-button size="small" class="ch-button-warning" style="float: right;margin-top: 4px" @click="handleReset()"><i class="el-icon-menu" />&nbsp;重置</el-button>
      </el-col>
    </el-row>
    <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"
          border
          :header-cell-style="{background:'#f2f2f2'}"
          :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="batchNumber" />
          <el-table-column label="选房号" prop="roomSelectionNumber" />
          <el-table-column label="买受人" prop="buyerName" width="300" />
          <el-table-column label="选房日期" prop="roomSelectionDate" width="180" />
          <el-table-column label="购房状态" prop="statusStr" />
          <el-table-column label="意向金状态" prop="intentionalDepositStatusStr" width="130"/>
          <el-table-column label="小区-分期" prop="groupDiscName" width="180" />
          <el-table-column label="意向房屋" prop="houseName" width="180" />
          <el-table-column label="操作" width="600">
            <template scope="scope">
              <el-button
                size="mini"
                type="text"
                @click="handleEdit(scope.row)"
              >修改
              </el-button>
              <el-button
                v-if="scope.row.roomSelectionCount <= 0"
                size="mini"
                type="text"
                :disabled="scope.row.statusStr === '已签约' || scope.row.statusStr === '已入驻' || scope.row.statusStr === '已退房' || scope.row.statusStr === '放弃资格'"
                @click="handleHouse(scope.row)"
              >选房
              </el-button>
              <el-button
                size="mini"
                type="text"
                :disabled="scope.row.statusStr === '已签约' || scope.row.statusStr === '已入驻' || scope.row.statusStr === '已退房' || scope.row.statusStr === '放弃资格'"
                @click="handleSubscribe(scope.row)"
              >认购
              </el-button>
              <el-button
                size="mini"
                type="text"
                @click="handleGiveUp(scope.row)"
              >放弃资格
              </el-button>
              <el-button
                size="mini"
                type="text"
                :disabled="scope.row.statusStr === '已选房' || scope.row.statusStr === '已签约' || scope.row.statusStr === '已入驻' || scope.row.statusStr === '已退房' || scope.row.statusStr === '放弃资格'"
                @click="handleIntentionalDeposit(scope.row)"
              >意向金
              </el-button>
              <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
      :visible.sync="dialogAddVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :title="dialogAddTitle"
      width="90%"
      top="20px"
      class="statistic_base"
      :append-to-body="true"
      :modal-append-to-body="true"
      custom-class="tagdialog"
      @close="getData"
    >
      <add-customer v-if="dialogAddVisible" ref="addCustomer" @cancel="cancel" />
    </el-dialog>

    <!--选房-->
    <el-dialog
      :visible.sync="dialogHouseVisible"
      :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-house v-if="dialogHouseVisible" ref="addHouse" @cancel="cancel" />
    </el-dialog>

    <!--认购金-->
    <el-dialog
      :visible.sync="dialogSubscribeVisible"
      :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"
    >
      <subscribe-index v-if="dialogSubscribeVisible" ref="subscribeIndex" @cancel="cancel" />
    </el-dialog>

    <!--意向金-->
    <el-dialog
      :visible.sync="dialogIntentionalVisible"
      :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-intentional-deposit v-if="dialogIntentionalVisible" ref="addIntentionalDeposit" @cancel="cancel" />
    </el-dialog>

    <!--放弃资格-->
    <el-dialog
      :visible.sync="dialogAbandonVisible"
      :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-abandon v-if="dialogAbandonVisible" ref="addAbandon" @cancel="cancel" />
    </el-dialog>

    <!-- 批量导入 -->
    <upload-cost
      v-if="importVisible"
      :dialog-visible="importVisible"
      :import-type="importType"
      :upload-title="uploadTitle"
      :import-title="importTitle"
      @cancelUpload="cancelImport"
    />

  </div>
</template>
<script>
import Base from '@/views/base/base'
import BaseData from '@/views/base/baseData'
import BaseDept from '@/views/base/baseDept.vue'
import AddCustomer from '@/views/customerManagement/numberManagement/addCustomer.vue'
import AddHouse from '@/views/customerManagement/roomChose/addHouse.vue'
import SubscribeIndex from '@/views/customerManagement/subscribe/subscribeIndex.vue'
import AddIntentionalDeposit from '@/views/customerManagement/intentionalDeposit/addIntentionalDeposit.vue'
import AddAbandon from '@/views/customerManagement/numberManagement/addAbandon.vue'
import constant from '@/static/utils/constant'
import uploadCost from '@/views/customerManagement/component/uploadCost.vue'

export default {
    name: 'NumberManagement',
    components: { AddIntentionalDeposit, AddHouse, AddCustomer, SubscribeIndex, AddAbandon, uploadCost },
    mixins: [Base, BaseData, BaseDept],
    data() {
        return {
            dc_key: ['BUYING_HOUSE_STATUS', 'INTENTIONAL_DEPOSIT_STATUS'],
            search: {},
            AllData: [],
            loading: false,
            groupIds: [],
            discIds: [],
            dialogAddVisible: false,
            dialogHouseVisible: false,
            dialogSubscribeVisible: false,
            dialogIntentionalVisible: false,
            dialogAbandonVisible: false,
            groupOption: [],
            discOption: [],
            dialogAddTitle: '',
            // 批量导入
            importVisible: false,
            importType: '',
            importTitle: '',
            uploadTitle: []
        }
    },
    mounted() {
        this.getGroupList(true)
        // this.getDiscList()
        this.initDict(this.dc_key).then((res) => {
            this.getData()
        })
    },
    methods: {
        getData: function() {
            const _this = this
            _this.loading = true
            _this.AllData = []

            this.search.pageNum = this.currentPage
            this.search.pageSize = this.pageSize
            this.baseRequest('list', 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(() => {
            })
        },
        getItemJson: function(item) {
            item.statusStr = this.dc_map.BUYING_HOUSE_STATUS[item.status]
            item.intentionalDepositStatusStr = this.dc_map.INTENTIONAL_DEPOSIT_STATUS[item.intentionalDepositStatus]
            return item
        },
        handleSearch() {
            this.getData()
        },
        handleReset() {
            for (const i in this.search) {
                if (i !== 'pageNum' && i !== 'pageSize') {
                    this.search[i] = ''
                }
            }
            this.groupIds = []
            this.discIds = []
            this.discOption = []
            this.handleSearch()
        },
        handleAdd() {
            this.dialogAddVisible = true
            this.dialogAddTitle = '新增'
            const data = {
                isView: false
            }
            // 新vue时调用的方法
            this.$nextTick(() => {
                this.$refs.addCustomer.initData(data)
            })
        },
        batchImport() {
            this.importVisible = true
            this.importType = 'insert'
            this.importTitle = '批量导入'
        },
        cancelImport(refresh) {
            this.importVisible = false
            this.importType = ''
            this.getData()
        },
        handleDelete() {
            console.log('this.multipleSelection', this.multipleSelection)
            if (!this.multipleSelection || this.multipleSelection.length === 0) {
                this.$message({
                    type: 'warning',
                    message: '请选择数据!'
                })
                return
            }
            const data = {
                ids: this.multipleSelection.map(obj => { return obj.id }).join(',')
            }
            this.$confirm('确认删除该数据，删除后将无法恢复，确认删除吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.baseRequest('delete', data).then(res => {
                    if (res.data.code === 200) {
                        this.getData()
                        this.$message({
                            type: 'success',
                            message: '删除成功!'
                        })
                    } else {
                        this.$message({
                            type: 'error',
                            message: res.data.msg
                        })
                    }
                }).catch((err) => {
                    this.$message({
                        type: 'error',
                        message: err
                    })
                })
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                })
            })
        },
        /* 编辑*/
        handleEdit: function(val) {
            this.dialogAddVisible = true
            this.dialogAddTitle = '修改'
            val.isView = false
            // 新vue时调用的方法
            this.$nextTick(() => {
                this.$refs.addCustomer.initData(val)
            })
        },
        /* 选房*/
        handleHouse: function(val) {
            this.dialogHouseVisible = true
            val.customerManagementId = val.id
            // 新vue时调用的方法
            this.$nextTick(() => {
                this.$refs.addHouse.initData(val)
            })
        },
        /* 认购*/
        handleSubscribe: function(val) {
            this.dialogSubscribeVisible = true
            // 新vue时调用的方法
            this.$nextTick(() => {
                this.$refs.subscribeIndex.initData(val)
            })
        },
        /* 放弃资格*/
        handleGiveUp(val) {
            this.dialogAbandonVisible = true
            // 新vue时调用的方法
            this.$nextTick(() => {
                this.$refs.addAbandon.initData(val)
            })
        },
        // 意向金
        handleIntentionalDeposit(val) {
            this.dialogIntentionalVisible = true
            // 新vue时调用的方法
            this.$nextTick(() => {
                this.$refs.addIntentionalDeposit.initData(val)
            })
        },
        handleView(val) {
            val.isView = true
            this.dialogAddVisible = true
            // 新vue时调用的方法
            this.$nextTick(() => {
                this.$refs.addCustomer.initData(val)
            })
        },
        downLoad() {
            this.multipleSelection.forEach(item => {
                const url = constant.BASE_URI + '/CustomerManagementController/download?id=' + item.id
                window.open(url, '_blank')
            })
        },
        checkedBox(i) {
            if (this.groupIds.includes(i)) {
                // includes()方法判断是否包含某一元素,返回true或false表示是否包含元素，对NaN一样有效
                // filter()方法用于把Array的某些元素过滤掉，filter()把传入的函数依次作用于每个元素，然后根据返回值是true还是false决定保留还是丢弃该元素：生成新的数组
                this.groupIds = this.groupIds.filter(function(ele) {
                    return ele !== i
                })
            } else {
                this.groupIds.push(i)
            }
            this.search.groupIds = this.groupIds.join(',')
            this.getDiscList()
        },
        checkedFQBox(i) {
            if (this.discIds.includes(i)) {
                // includes()方法判断是否包含某一元素,返回true或false表示是否包含元素，对NaN一样有效
                // filter()方法用于把Array的某些元素过滤掉，filter()把传入的函数依次作用于每个元素，然后根据返回值是true还是false决定保留还是丢弃该元素：生成新的数组
                this.discIds = this.discIds.filter(function(ele) {
                    return ele !== i
                })
            } else {
                this.discIds.push(i)
            }
            this.search.discIds = this.discIds.join(',')
        },
        cancel() {
            this.dialogAddVisible = false
            this.dialogHouseVisible = false
            this.dialogSubscribeVisible = false
            this.dialogIntentionalVisible = false
            this.dialogAbandonVisible = false
        },
        getGroupList(firstLoad) {
            this.groupOption = []
            this.baseParkRequest('listAll', {}).then(res => {
                if (res.data) {
                    // const idArr = res.data.map(obj => { return obj.id })
                    this.groupOption.push({
                        label: '全部',
                        // value: idArr.join(',')
                        value: ''
                    })
                    res.data.forEach(item => {
                        const obj = {
                            label: item.groupName,
                            value: item.id
                        }
                        this.groupOption.push(obj)
                    })
                    // if (firstLoad) {
                    //     this.checkedBox(this.groupOption[1].value)
                    // }
                }
            })
        },
        getDiscList() {
            this.discOption = []
            const data = {
                groupIds: this.search.groupIds
            }
            this.baseFQRequest('listAll', data).then(res => {
                if (res.data) {
                    // const idArr = res.data.map(obj => { return obj.id })
                    this.discOption.push({
                        label: '全部',
                        // value: idArr.join(',')
                        value: ''
                    })
                    res.data.forEach(item => {
                        const obj = {
                            label: item.name,
                            value: item.id
                        }
                        this.discOption.push(obj)
                    })
                }
            })
        },

        baseRequest(opUrl, postData) {
            return this.$channel.globeRequest('CustomerManagementController', opUrl, postData, '')
        },
        baseParkRequest: function(opUrl, postData) {
            return this.$channel.baseRequest('ParkInfoController', opUrl, postData, '')
        },
        baseFQRequest: function(opUrl, postData) {
            return this.$channel.baseRequest('ParkFloorDiscController', opUrl, postData, '')
        }
    }
}
</script>

<style scoped>
.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;
}

.listBox {
    margin-left: 20px;
    display: flex;
    flex-wrap: wrap;
}

.list {

    border-radius: 4px;
    margin-right: 20px;
    margin-top: 15px;
    cursor: pointer;
    margin-bottom: 15px;
    width: 138px;
    height: 28px;
    text-align: center;
    line-height: 28px;
    background: #EBEBEB;
    color: #777777;
}

.checked {
    color: #2C27D0;
    background: #EAEAF8;
    /*border: 1px solid #3377ff;*/
}

.ch-button-danger {
    margin-left: 10px;
    border-color: #ff4949;
    background-color: #ff4949;
    color: #fff;
}

</style>
