// 门牌幢列表
<template>
  <div class="routerWapper">
    <div class="portalDetailTitle" v-if="$route.name=='buildings'&&$permission('00010103')">
			<span class="title">小区门牌栋信息查询</span>
			<div class="detailOperations">
				<a-button @click="backPage"> 返回 </a-button>
			</div>
		</div>
    <div v-if="$route.name=='buildings'" 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="['unitAddr_l']" />
              </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="23" class="algin-right">
              <a-button @click="reset">重置</a-button>
              <a-button type="primary" @click="searchBtn">搜索</a-button>
            </a-col>
          </a-row>
      </a-form>
        <!-- <p class="gayLine noline" ></p> -->
        <!-- table -->
        <div class="portalTableOperates">
          <tableAndMut ref="childTab" :tableData="tableData" :pagination="pagination" :tbl="tbl" />
        </div>
    </div>
    <RouterWapper v-else />
  </div>
</template>

<script>

export default {
  name: 'buildings',
  data() {
    return {
      columns: [ // 表格展示的列
        {
          title: '门牌幢',
          dataIndex: 'unitNo',
          key: 'unitNo',
          width: '15%'
        },
        {
          title: '门牌幢地址',
          dataIndex: 'unitAddr',
          key: 'unitAddr',
          width: '40%'
        },
        {
          title: '门牌属性',
          dataIndex: 'unitKind',
          key: 'unitKind',
          width: '15%',
          customRender: text => {
            if (text == '1') {
              return '高层'
            } else if(text == '2'){
              return '多层'
            } else if(text == '3'){
              return '小高层'
            } else if(text == '4'){
              return '联体别墅'
            } else if(text == '5'){
              return '单体别墅'
            } else {
              return ' '
            }
          }
        },
        {
          title: '操作',
          dataIndex: 'action',
          key: 'action',
          width: '20%',
          scopedSlots: {
            customRender: 'action'
          }
        }
      ],
      pagination: { // 分页信息
        pageNo: 1,
        pageSize: 10,
        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'],
      },
      customKeyEnable: false, // cookie里存的 是否开启自定义字段
      tbl: 'tbl_building', // 当前表名,
      searchList: [], // 动态渲染的搜索列表
      keyCodeList: [], // 动态搜索的keycode列表
      searchParams: {}, //搜索的列表
      tableData: [],
    }
  },

  beforeCreate() { // 页面进来创建form
    this.form = this.$form.createForm(this)
  },
  created () { // 进页面获取数据
    this.customKeyEnable=this.$cookie.get('customKeyEnable')

  },
  mounted(){
    if (this.$route.name == 'buildings') {
      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)
    },
    toView(params){ // 查看详情
      this.$router.push({path: `/houseData/buildings/${params.id}`})
    },
    searchBtn() { // 点击搜索按钮
      //需要发送ajax
      this.pagination.current = 1
      this.pagination.pageNo = 1
      this.getList()
    },
    reset(){ // 重置表单
      this.pagination.pageNo = 1
      this.pagination.current = 1
      this.pagination.pageSize = 20
      this.form.resetFields()
      this.getList()
    },
    backPage(){ // 返回按钮
      this.$router.back()
    },
    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()
    },
    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({}, {
        'unitAddr_l': !this.form.getFieldValue('unitAddr_l')? '': this.form.getFieldValue('unitAddr_l'),
      }, {
        'sectId': this.$route.query.id
      }, obj)
      this.searchParams = searchParams
      this.$ajax.get({
        url: this.$api.GET_BUILDINGS_LIST,
        params: Object.assign({}, searchParams, {
          pageSize: this.pagination.pageSize,
          pageNo: this.pagination.pageNo,
        })
      }).then(res => {
        if (res.code === '200') {
          /**
           *      在请求数据的成功的时候   保存请求的各项数据及token   后面下载用
           */
          searchParams.token = this.$cookie.get('token')
          this.pagination.total= this.$com.confirm(res, 'data.totalRows', 0)
          this.tableData = this.$com.confirm(res, 'data.content', [])
          this.tableData.forEach(col => {
            if (col.unitKind == '1') {
              col.unitKind = '高层'
            } else if(col.unitKind == '2') {
              col.unitKind = '多层'
            }else if(col.unitKind == '3') {
              col.unitKind = '小高层'
            }else if(col.unitKind == '4') {
              col.unitKind = '联体别墅'
            }else if(col.unitKind == '5') {
              col.unitKind = '单体别墅'
            }
            if (col.unitFlag == false) {
              col.unitFlag = '无'
            } else {
              col.unitFlag = '有'
            }
          })
        }
        //
        // 存储当前页面列表的搜索信息
        //
        this.$com.storeSearchParams(
          this.$route.name,
          this.pagination,
          searchParams
        )
      })
    },
    /**
     * 从vuex中或已存储的搜索条件，判断此条件是否为当前路由的 。如果是则使用store里面的数据
     */
    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 'unitAddr_l':
              this.form.setFieldsValue({ 'unitAddr_l': searchParams.params[elem] })
              break
            default:
              break
            }
          })
        }
        if(!!searchParams.pagination){
          if(!!searchParams.pagination.pageNo){
            this.pagination.pageNo = searchParams.pagination.pageNo
            this.pagination.current = searchParams.pagination.pageNo
            this.pagination.pageSize = searchParams.pagination.pageSize
          }
        }
      }
      this.getList()
    },

  },
}
</script>

<style scoped>

</style>

