// 非居列表
<template>
  <div class="routerWapper">
    <div v-if="$route.name == 'nonResidents'" class="layoutMargin layoutPadding">
      <a-form :form="form" class="protalForm" >
        <a-row class="formItemLine" type="flex" align='top' :gutter="18">
          <a-col span="8">
            <a-form-item label="项目名称" class="formItem" :label-col="{span:10}" :wrapper-col="{span:14}">
              <a-input placeholder="请输入查询内容" v-decorator="['stName_l']" />
            </a-form-item>
          </a-col>
          <a-col span="8" >
            <a-form-item label="项目地址" class="formItem" :label-col="{span:10}" :wrapper-col="{span:14}">
              <a-input placeholder="请输入查询内容" v-decorator="['stAddr_l']" />
            </a-form-item>
          </a-col>
          <a-col span="8" >
            <a-form-item label="项目类型" class="formItem" :label-col="{span:10}" :wrapper-col="{span:14}">
              <a-input placeholder="请输入查询内容" v-decorator="['stUseType']" />
            </a-form-item>
          </a-col>
          <template v-if="customKeyEnable">
            <a-col span="8"  v-for="i in searchList" :key="i.id" >
              <a-form-item :label="i.keyName" class="formItem" :label-col="{span:10}" :wrapper-col="{span:14}">
                <a-select @change="handleChangeZdy(i.keyCode,$event)" v-if="i.compntType == 'select' || i.compntType == 'radio'" showSearch allowClear placeholder="请选择查询内容" v-decorator="[i.keyCode]">
                  <a-select-option v-for="(item) in JSON.parse(i.sltJson)" :key="item" :value="item">{{ item }}</a-select-option>
                </a-select>
                <a-select @change="handleChangeZdy(i.keyCode,$event)" mode="multiple" v-else-if="i.compntType =='checkbox'"  placeholder="请选择查询内容(多选)" v-decorator="[i.keyCode]">
                  <a-select-option v-for="(item) in JSON.parse(i.sltJson)" :key="item" :value="item">{{ item }}</a-select-option>
                </a-select>
                <a-date-picker @change="handleChangeZdy(i.keyCode,$event)" v-else-if="i.compntType =='date'"  v-decorator="[i.keyCode]"  />
                <a-input v-else-if="i.compntType =='num'" type="tel" placeholder="请输入查询内容" v-decorator="[i.keyCode]" />
                <a-input v-else placeholder="请输入查询内容" v-decorator="[i.keyCode]" />
              </a-form-item>
            </a-col>
          </template>
          <a-col span="24" class="algin-right">
            <a-button  @click="exportList">导出数据</a-button>
            <a-button @click="reset">重置</a-button>
            <a-button type="primary" @click="search">搜索</a-button>
          </a-col>
        </a-row>
      </a-form>
      <!-- <p class="gayLine noline" /> -->
      <div class="portalTableOperates">
        <tableAndMut ref="childTab" :tableData="tableData" :pagination="pagination" :tbl="tbl" />
      </div>
    </div>
    <RouterWapper v-else />
  </div>
</template>

<script>
export default {
  name: 'nonResidents',
  data() {
    return {
      customKeyEnable: false, // cookie里存的是否开启自定义字段
      tbl: 'tbl_non_resident', // 当前表名,
      searchList: [], // 动态渲染的搜索列表
      keyCodeList: [], // 动态搜索的keycode列表
      searchParams: {}, //搜索的列表
      searchForm: {},
      columns: [ //未开启自定义字段展示的列
        {
          title: '项目名称',
          dataIndex: 'stName',
          key: 'stName'
        },
        {
          title: '项目类型',
          dataIndex: 'stUseType',
          key: 'stUseType'
        }, {
          title: '是否有物业管理',
          dataIndex: 'cspName',
          key: 'cspName'
        },
        {
          title: '项目地址',
          dataIndex: 'stAddr',
          key: 'stAddr'
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: 160,
          scopedSlots: {
            customRender: 'action'
          }
        }
      ],
      pagination: { // 分页信息
        pageNo: 1,
        pageSize: 20,
        total: 0,
        current: 1,
        defaultCurrent: 1,
        defaultPageSize: 20,
        showQuickJumper: true,
        onChange: this.pageChange,
        showSizeChanger: true,
        showTotal: total => `总条数: ${total}`,
        onShowSizeChange: this.showSizeChange,
        pageSizeOptions: ['10', '20', '50', '100'],
      },
      tableData: [],
    }
  },
  beforeCreate() { // 页面进来创建form
    this.form = this.$form.createForm(this)
  },
  created () { // 进页面获取数据
    this.customKeyEnable=this.$cookie.get('customKeyEnable')
    if(this.customKeyEnable && this.customKeyEnable == 'true'){
      // this.getKeys()
      // this.getAllKeys()
    }else {
      this.getList()
    }
  },
  mounted(){
    if (this.$route.name == 'nonResidents') {
      this.$nextTick(function () {
        this.getSearchParams()
      })
    }
  },
  methods: {
    handleChangeZdy(i, e){ // 自定义字段 - 搜索条件的 change
      if (Object.prototype.toString.call(e)== '[object Array]') {
        e2 = e.join(',')
      } else if (Object.prototype.toString.call(e)== '[object Object]') {
        e2 = this.$moment(e).format('YYYY-MM-DD')
      }
      this.form.setFieldsValue({ // 传入的i 是i.keyCode   将form表单设置成最新的值
        i: e2
      })
      setTimeout(() => { // 延时器,去掉会拿不到最新的value
        this.getList()
      }, 0)
    },
    pageChange(page){ //分页切换
      this.pagination.pageNo = page
      this.pagination.current = page
      this.getList()
    },
    showSizeChange(current, size) { //每页条数切换
      this.pagination.pageNo = 1
      this.pagination.current = 1
      this.pagination.pageSize = size
      this.getList()
    },
    reset(){ // 重置表单
      this.form.resetFields()
      this.pagination.pageNo = 1
      this.pagination.current = 1
      this.pagination.pageSize = 20
      this.getList()
    },
    search() { // 搜索按钮
      this.pagination.current = 1
      this.pagination.pageNo = 1
      //需要发送ajax
      this.getList()
    },
    backPage(){ // 返回按钮
      this.$router.back()
    },
    getList(){ // 获取数据
      //处理动态的搜索条件,耦合到搜索列表
      const obj = {}
      this.keyCodeList.forEach(item => {
        let value = !this.form.getFieldValue(item)? '':this.form.getFieldValue(item)
        if (Object.prototype.toString.call(value)== '[object Array]') { //如果是数组
          value = value.join(',')
        }else if(Object.prototype.toString.call(value) === '[object Object]'){
          value = this.$moment(value).format('YYYY-MM-DD')
        }
        this.$set(obj, item, value)
      })

      const searchParams = Object.assign({}, {
        'stName_l': !this.form.getFieldValue('stName_l')?'':this.form.getFieldValue('stName_l'),
        'stAddr_l': !this.form.getFieldValue('stAddr_l')?'':this.form.getFieldValue('stAddr_l'),
        'stUseType': !this.form.getFieldValue('stUseType')?'':this.form.getFieldValue('stUseType'),
      }, obj)
      this.searchParams = searchParams
      this.$ajax.get({
        url: this.$api.GET_NONRESIDENTS_LIST,
        params: Object.assign({}, searchParams, {
          pageSize: this.pagination.pageSize,
          pageNo: this.pagination.current,
        })
      }).then(res => {
        if (res.code === '200') {
          /**
           *      在请求数据的成功的时候   保存请求的各项数据及token   后面下载用
           */
          searchParams.token = this.$cookie.get('token')
          let a = this.tbl + '?'
          for(const k in searchParams){
            a = a+k+'='+ searchParams[k]+ '&'
          }
          this.exportParams = a
          this.pagination.total= this.$com.confirm(res, 'data.totalRows', 0)
          this.tableData = this.$com.confirm(res, 'data.content', [])
          this.tableData.forEach(col => {
            if (col.stIsManaged == 1) {
              col.stIsManaged = '是'
            } else {
              col.stIsManaged = '否'
            }
            if (col.stUseType == '01') {
              col.stUseType = '旅馆'
            } else if(col.stUseType == '02') {
              col.stUseType = '办公楼'
            }else if(col.stUseType == '03') {
              col.stUseType = '工厂'
            }else if(col.stUseType == '04') {
              col.stUseType = '站场码头'
            }else if(col.stUseType == '05') {
              col.stUseType = '仓库堆栈'
            }else if(col.stUseType == '06') {
              col.stUseType = '商场'
            }else if(col.stUseType == '07') {
              col.stUseType = '店铺'
            }else if(col.stUseType == '08') {
              col.stUseType = '学校'
            }else if(col.stUseType == '09') {
              col.stUseType = '文化馆'
            }else if(col.stUseType == '10') {
              col.stUseType = '体育馆'
            }else if(col.stUseType == '11') {
              col.stUseType = '影剧院'
            }else if(col.stUseType == '12') {
              col.stUseType = '福利院'
            }else if(col.stUseType == '13') {
              col.stUseType = '医院'
            }else if(col.stUseType == '14') {
              col.stUseType = '农业建筑'
            }else if(col.stUseType == '15') {
              col.stUseType = '公共设施用房'
            }else if(col.stUseType == '16') {
              col.stUseType = '寺庙教堂'
            }else if(col.stUseType == '17') {
              col.stUseType = '宗祠山庄'
            }else if(col.stUseType == '18') {
              col.stUseType = '商业综合体'
            }else if(col.stUseType == '19') {
              col.stUseType = '产业园区'
            }else if(col.stUseType == '20') {
              col.stUseType = '其他'
            }
          })
        }
        //
        // 存储当前页面列表的搜索信息
        //
        this.$com.storeSearchParams(
          this.$route.name,
          this.pagination,
          searchParams
        )
      })
    },
    /**
     * 从vuex中或已存储的搜索条件，判断此条件是否为当前路由的 。如果是则使用
     */
    getSearchParams(){
      const searchParams = !this.$store.state.listSearchParams?null:this.$store.state.listSearchParams[this.$route.name]
      if(!!searchParams && !!searchParams.routeName && (this.$route.name == searchParams.routeName)){
        if(!!searchParams.params){
          Object.keys(searchParams.params).forEach(elem => {
            switch (elem) {
            case 'stName_l':
              this.form.setFieldsValue({ 'stName_l': searchParams.params[elem] })
              break
            case 'stAddr_l':
              this.form.setFieldsValue({ 'stAddr_l': searchParams.params[elem] })
              break
            case 'stUseType':
              this.form.setFieldsValue({ 'stUseType': searchParams.params[elem] })
              break
            default:
              break
            }
          })
        }
        if(!!searchParams.pagination){
          if(!!searchParams.pagination.pageNo){
            this.pagination.pageNo = searchParams.pagination.pageNo
            this.pagination.current = searchParams.pagination.current
            this.pagination.pageSize = searchParams.pagination.pageSize
          }
        }
      }
      this.getList()
    },
    toView(params){ // 操作里面去详情
      this.$router.push({path: `/houseData/nonResident/${params.id}`})
    },
    exportList(){ //导出功能
      const formValue = this.form.getFieldsValue()
      let newUrl = this.tbl + '?'
      let newObj = {}
      //拼接除 c.xxx 之外的数据
      for(const k in formValue){
        if (formValue[k]!= undefined ) { // 判断是否值为 undefined
          if ( k == 'c') { // 排除 c.xxx 的键
            newObj = formValue[k] //有c.xx存入 b对象  下面处理,
          } else if(Object.prototype.toString.call(formValue[k])== '[object Array]'){
            // 数组的话就拼接字符串
            newUrl += k +'=' + formValue[k].join(',') + '&'
          }else if (Object.prototype.toString.call(formValue[k]) === '[object Object]') {
            //时间格式对象就转换成字符串
            newUrl += k +'=' +this.$moment(formValue[k]).format('YYYY-MM-DD') +'&'
          }else {
            newUrl += k +'=' +formValue[k] +'&' //没有特殊情况  直接拼接到 newUrl
          }
        }
      }
      // 处理 c.xxx 里面的数据
      for(const k in newObj ){
        if (newObj[k] != undefined) { // 排除值为undefined的键
          newUrl += 'c.'+ k +'=' +newObj[k].split('#')[1] +'&' // 拼接到 newUrl
        }
      }
      const token = this.$cookie.get('token') // 获取token 并拼接到 newUrl
      newUrl += '&token=' + token
      window.open (this.$api.BASE_URL+'/service-basicdatasync-ddd/export/'+ newUrl)
    },
    toBul(record){ //到非居门牌幢列表
      this.$router.push({path: '/houseData/nonBuildingList', query: { nrStId: record.id, stName: record.stName }})
    },
  },
}
</script>

<style>
.routerWapper[data-v-5f2c575d] .ant-table-small>.ant-table-content>.ant-table-body {
  margin: 0;
}
</style>
