<template>
  <header-collapse>
    <base-form slot="content" ref="ruleForm" :data="searchData">
      <div slot="footer">
        <el-button :loading="listLoading" icon="el-icon-search" class="xl-form-btn bgc1" @click="searchHandle">查询</el-button>
        <el-button :loading="listLoading" icon="el-icon-document" class="xl-form-btn bgc2" @click="toExportExcel">导出</el-button>
        <!-- <el-button :loading="listLoading" icon="el-icon-plus" class="xl-form-btn bgc2" @click="addHandle">添加客户</el-button> -->
      </div>
    </base-form>
  </header-collapse>
</template>
<script>
export default {
  name: 'custIndex',
  mixins: [],
  props: {
    listLoading: Boolean
  },
  inject: ['parentData'],
  data() {
    return {
      searchData: [
        { label: '客户状态', key: 'report_state', type: 'select', options: this.$dictData.report_state},
        { label: '报备进度', key: 'report_step', type: 'select', options: this.$dictData.report_step},
        { label: '意向楼盘', key: 'estate_id', type: 'selectRemote',
          remoteParams: { skey: 'estate_name', api: `house.admestatelist`, opKey: 'estate_name', opVal: 'id' },
        },
        { label: '时间类型', key: 'section', defaultValue: 'create_at', type: 'select', options: [{key: '创建时间', val: 'create_at'}, {key: '报备时间', val: 'report_at'}, {key: '到访时间', val: 'visit_at'}, ]},
        { label: '时间', label2: '开始时间', label3: '结束时间', key: 'startEndTime', type: 'datePicker'},
        { label: '中介渠道', key: 'user_id', type: 'selectRemote',
          remoteParams: { skey: 'search', api: `user.admusersearch`, opKey: 'nickname', opVal: 'id' },
        },
        { label: '电话', key: 'phone' },
        { label: '姓名', key: 'name' },
        { label: '性别', key: 'sex', type: 'select', options: this.$dictData.sex},
        { label: '备注', key: 'remark' },
      ]
    }
  },
  methods: {
    addHandle () {
      this.parentData.isDtlShow = true
      this.parentData.curObj = {}
    },
    searchHandle() {
      const oldform = this.$refs.ruleForm.baseForm
      const newForm = { ...oldform }
      if (newForm.startEndTime) {
        newForm.start_at = newForm.startEndTime[0]
        newForm.end_at = newForm.startEndTime[1]
      }
      delete newForm.startEndTime
      this.$emit('change', newForm)
    },
    toExportExcel () {
      const oldform = this.$refs.ruleForm.baseForm
      const newForm = { ...oldform }
      if (newForm.startEndTime) {
        newForm.start_at = newForm.startEndTime[0]
        newForm.end_at = newForm.startEndTime[1]
        delete newForm.startEndTime
      } else {
        this.$msgw('请选择导出的时间范围')
        return
      }
      const token = window.sessionStorage.getItem('fp_token')
      window.open(`https://api.fangpiaovip.com/adm/report/export?token=${encodeURIComponent(token)}&section=${newForm.section}&start_at=${newForm.start_at}&end_at=${newForm.end_at}&estate_id=${newForm.estate_id}&report_state=${newForm.report_state}&report_step=${newForm.report_step}&user_id=${newForm.user_id}`)
    }
  }
}
</script>
