<template>
  <div id="root">
    <el-form :model="formInline" inline="true" class="form-inline">
      <el-form-item label="名称">
        <el-input v-model="formInline.name" placeholder="名称"/>
      </el-form-item>
      <el-form-item label="号码">
        <el-input v-model="formInline.tel" placeholder="号码" oninput="value=value.replace(/[^\d]/g,'')"/>
      </el-form-item>
      <el-form-item label="备注">
        <el-input v-model="formInline.remark" placeholder="备注"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <div style="margin-left: 20px;">
      <el-button type="primary" @click="add">添加</el-button>
      <el-button type="warning" @click="edi">修改</el-button>
      <el-button type="danger" @click="deleteIds">删除</el-button>
    </div>
    <el-table
      ref="multipleTable"
      :data="propertyList"
      border
      tooltip-effect="dark"
      style="width: 100%; margin-top: 20px;"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55"/>
      <el-table-column
        prop="name"
        label="名称"/>
      <el-table-column
        prop="tel"
        label="号码"/>
      <el-table-column
        prop="remark"
        label="备注"/>
      <el-table-column
        prop="updateName"
        label="编辑人"/>
      <el-table-column
        prop="updateDate"
        label="编辑时间">
        <template slot-scope="scope">{{ formatDate(scope.row.updateDate) }}</template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
  name: 'Property',
  data() {
    return {
      formInline: {
        name: '',
        tel: '',
        remark: '',
        pageNum: 1,
        pageSize: 100
      },
      ids: [], // id集合
      propertyList: [] // 物业数据列表
    }
  },
  mounted() {
    this.getPropertyData()
  },
  methods: {
    getPropertyData() { // 查询物业号码数据
      this.$store.dispatch('GetTelsList', this.formInline).then(res => {
        const resData = res.data
        this.propertyList = resData.list
      }).catch(() => {
        console.log('error GetTelsList')
      })
    },
    onSubmit() {
      this.getPropertyData()
    },
    add() { // 跳转添加物业号码
      this.$router.push({ name: 'property-add-index' })
    },
    edi() { // 修改页面
      if (this.ids.length === 0) {
        this.$message.error('请至少选择一项')
        return
      }
      if (this.ids.length > 1) {
        this.$message.error('只能选择一项进行操作')
        return
      }
      this.$router.push({ name: 'property-edi-index', query: { id: this.ids[0] }})
    },
    deleteIds() { // 批量删除id
      if (this.ids.length === 0) {
        this.$message.error('请至少选择一个项')
        return
      }
      this.$store.dispatch('TelDeleteProperty', this.ids).then(res => {
        const resCode = res.code
        if (resCode === '1') {
          this.$message.error(res.message)
          return
        }
        this.$message.success(res.message)
        this.getPropertyData()
      }).catch(() => {
        console.log('error TelDelete')
      })
    },
    handleSelectionChange(val) {
      console.log(val)
      this.ids = []
      val.map((item, index) => {
        this.ids.push(item.id)
      })
      console.log(this.ids)
    },
    formatDate(val) {
      if (val === null) {
        return ''
      }
      var value = new Date(val)
      var year = value.getFullYear()
      var month = value.getMonth() + 1
      var day = value.getDate()
      // var hour = value.getHours()
      // var minutes = value.getMinutes()
      // var seconds = value.getSeconds()
      // return year + '-' + month + '-' + day + ' ' + hour + ':' + minutes + ':' + seconds
      return year + '-' + month + '-' + day
    }
  }
}
</script>

<style scoped>
  .form-inline {
    margin-left: 20px;
    margin-top: 20px;
  }
</style>
