<template>
  <div>
    <div class="div_search">
      <div class="resi-cell">
        <div class="resi-cell-label">房主姓名</div>
        <div class="resi-cell-value">
          <el-input v-model="ownerName"
                    class="resi-cell-input"
                    size="small"
                    clearable
                    placeholder="请输入内容">
          </el-input>

        </div>
      </div>
      <div class="resi-cell">
        <div class="resi-cell-label">房主手机</div>
        <div class="resi-cell-value">
          <el-input v-model="ownerPhone"
                    class="resi-cell-input"
                    size="small"
                    clearable
                    placeholder="请输入内容">
          </el-input>

        </div>
      </div>
      <el-button style="margin-left:10px"
                 type="primary"
                 size="small"
                 @click="handleSearch">查询</el-button>
    </div>
    <div class="div_btn">
      <el-button type="warning"
                 size="small"
                 @click="handleExport">导出</el-button>
      <el-button type="success"
                 size="small"
                 @click="handleAdd">新增楼宇</el-button>
      <el-button type="primary"
                 size="small"
                 @click="handleExportModule">下载楼宇模板</el-button>
      <el-button type="danger"
                 size="small"
                 @click="handleInport">导入楼宇数据</el-button>
    </div>

    <div class="div_table">
      <el-table :data="tableData"
                border
                style="width: 100%">
        <el-table-column prop="neighborHoodName"
                         label="小区名称"
                         width="180">
        </el-table-column>
        <el-table-column prop="orgName"
                         label="所属组织"
                         width="160">
        </el-table-column>
        <el-table-column prop="gridName"
                         label="所属网格"
                         width="140">
        </el-table-column>
        <el-table-column prop="address"
                         label="详细地址">
        </el-table-column>
        <el-table-column prop="remark"
                         label="备注">
        </el-table-column>
        <el-table-column label="操作"
                         fixed="right"
                         width="140"
                         header-align="center"
                         align="center"
                         class="operate">
          <template slot-scope="scope">

            <el-button type="text"
                       style="color:#1C6AFD;text-decoration: underline;"
                       size="small"
                       @click="handleDetail(scope.row)">查看</el-button>
            <el-button type="text"
                       style="color:#00A7A9;text-decoration: underline;"
                       size="small"
                       @click="handleEdit(scope.row)">修改</el-button>

            <el-button type="text"
                       style="color:#D51010;text-decoration: underline;"
                       size="small"
                       @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div>
        <el-pagination @size-change="handleSizeChange"
                       @current-change="handleCurrentChange"
                       :current-page.sync="params.pageNo"
                       :page-sizes="[20, 30, 50]"
                       :page-size="params.pageSize"
                       layout="sizes, prev, pager, next"
                       :total="total">
        </el-pagination>
      </div>
    </div>

    <!-- 修改弹出框 -->
    <el-dialog :visible.sync="formShow"
               :close-on-click-modal="false"
               :close-on-press-escape="false"
               :title="formTitle"
               width="800px"
               @closed="diaClose">
      <community-form ref="ref_form"
                      @dialogCancle="addFormCancle"
                      @dialogOk="addFormOk"></community-form>
    </el-dialog>

  </div>
</template>

<script>

import CommunityForm from './communityForm'

import { requestPost } from "@/js/dai/request";
import { mapGetters } from 'vuex'
import { Loading } from 'element-ui' // 引入Loading服务

let loading // 加载动画
export default {
  data () {
    return {
      loading: false,
      total: 0,
      params: {
        pageSize: 20,
        pageNo: 0,
        ownerName: '',
        ownerPhone: ''
      },

      ownerName: '',
      ownerPhone: '',
      tableData: [],

      //form相关
      formShow: false,
      formTitle: '新增小区'

    }
  },
  components: {
    CommunityForm
  },
  async mounted () {

  },
  computed: {
    rowHeight () {
      return (this.clientHeight - 200) + 'px'
    },
    ...mapGetters(['clientHeight'])
  },
  methods: {
    handleSearch () {
      this.loadTable()
    },
    async loadTable () {
      // const url = "/gov/org/neighborhood/neighborhoodlist"
      const url = "http://yapi.elinkservice.cn/mock/245/gov/org/neighborhood/neighborhoodlist"
      let params = {
        pageSize: this.pageSize,
        pageNo: this.pageNo,
        ownerName: this.ownerName,
        ownerPhone: this.ownerPhone
      }

      const { data, code, msg } = await requestPost(url, params)

      if (code === 0) {
        this.total = data.total
        this.tableData = data.list
      } else {
        this.$message.error(msg)
      }
    },

    diaClose () {
      this.formShow = false
    },

    handleDetail (row) {
      this.formTitle = '小区详情'
      this.formShow = true
      this.$nextTick(() => {
        this.$refs.ref_form.initForm('detail', row)
      })
    },

    handleAdd () {
      this.formTitle = '新增小区'
      this.formShow = true
      this.$nextTick(() => {
        this.$refs.ref_form.initForm('add')
      })
    },

    handleEdit (row) {
      this.formTitle = '修改小区'
      this.formShow = true
      this.$refs.ref_form.initForm('edit', row)
    },

    addFormCancle () {
      this.formShow = false
    },
    addFormOk () {
      this.formShow = false
      this.loadTable()
    },

    async handleDelete (row) {

      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteCommunity(row)
        })
        .catch(err => {
          if (err == "cancel") {
            // this.$message({
            //   type: "info",
            //   message: "已取消删除"
            // });
          }

        });


    },

    async deleteCommunity (row) {
      // const url = "/gov/org/neighborhood/neighborhooddel"
      const url = "http://yapi.elinkservice.cn/mock/245/gov/org/neighborhood/neighborhooddel"
      let params = {
        neighborHoodId: row.neighborHoodId
      }

      const { data, code, msg } = await requestPost(url, params)

      if (code === 0) {
        this.$message({
          type: "success",
          message: "删除成功"
        });
      } else {
        this.$message.error(msg)
      }
    },

    handleExport () {

    },


    handleExportModule () {

    },
    handleInport () {

    },


    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
    },

    // 开启加载动画
    startLoading () {
      loading = Loading.service({
        lock: true, // 是否锁定
        text: '正在加载……', // 加载中需要显示的文字
        background: 'rgba(0,0,0,.7)' // 背景颜色
      })
    },
    // 结束加载动画
    endLoading () {
      // clearTimeout(timer);
      if (loading) {
        loading.close()
      }
    }
  },
  watch: {
    filterText (val) {
      this.$refs.tree.filter(val);
    }
  },
  props: {

  }
}
</script>
<style lang="scss" scoped >
.div_search {
  display: flex;

  .resi-cell {
    display: flex;
    align-items: center;
    .resi-cell-label {
      width: 70px;
      box-sizing: border-box;
      margin-right: 15px;
      text-align: right;
      // line-height: 32;
    }

    .resi-cell-input {
      width: 180px;
    }
  }
}

.div_table {
  margin-top: 20px;
}
.div_search {
  display: flex;

  .resi-cell {
    display: flex;
    align-items: center;
    .resi-cell-label {
      width: 70px;
      box-sizing: border-box;
      margin-right: 15px;
      text-align: right;
      // line-height: 32;
    }

    .resi-cell-input {
      width: 180px;
    }
  }
}

.div_btn {
  margin-top: 20px;
}

.el-row {
  /* margin-bottom: 20px; */
  display: flex;
  flex-wrap: wrap;
  margin-top: 10px;
  margin-right: 50px;
}
</style>
