<template>
  <div class="app-container">
    <div class="search">
      <el-input v-model="table.getdataListParm.parammaps.pastureName" placeholder="牧场" style="width: 180px;" class="filter-item" clearable />
      <el-select v-model="table.getdataListParm.parammaps.enable" placeholder="是否启用" class="filter-item" style="width: 120px;" clearable>
        <el-option v-for="item in enableList" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <div ref="selectInput" class="filter-item selectInput">
        <el-input type="text" name="" value="" placeholder="请点击选择搜索条件" class="Input" />
        <i v-if="arrowDown" icon="el-icon-arrow-down" class="el-icon-arrow-down" />
        <i v-if="arrowUp" icon="el-icon-arrow-up" class="el-icon-arrow-up" />
        <ul v-if="arrowUp" class="selectUl">
          <li><a>地址</a><el-input v-model="table.getdataListParm.parammaps.address" type="text" style="width: 245px;" /></li>
          <li><a>联系人</a><el-input v-model="table.getdataListParm.parammaps.contact" type="text" style="width: 245px;" /></li>
          <li><a>联系电话</a><el-input v-model="table.getdataListParm.parammaps.phone" type="text" style="width: 245px;" /></li>
          <li>
            <div style="float: right;">
              <el-button class="downminCancel" @click="arrowUp=false;arrowDown=true;">取消</el-button>
              <el-button class="miniPrimary" @click="handleSearch">搜索</el-button>
            </div>
          </li>
        </ul>
      </div>
      <el-button class="successBorder" @click="handleSearch">查询</el-button>
      <el-button class="successBorder" @click="handleRefresh">重置</el-button>
    </div>

    <div class="table">
      <el-table
        :key="table.tableKey"
        v-loading="table.listLoading"
        element-loading-text="给我一点时间"
        :data="table.list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        :row-style="rowStyle"
        :cell-style="cellStyle"
        class="elTable table-fixed"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" align="center" width="50" />
        <el-table-column label="序号" align="center" type="index" width="50px">
          <template slot-scope="scope">
            <span>{{ scope.$index + (table.pageNum-1) * table.pageSize + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="集团名称" min-width="130px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.groupsname }}</span>
          </template>
        </el-table-column>
        <el-table-column label="牧场名称" min-width="130px" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.pastureName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="地址" min-width="150px" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.address }}</span>
            <el-input v-if="scope.row.Edit" v-model="scope.row.address" placeholder="1-255字符" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" maxlength="255" style="width:95%;padding:10px 0;" />
          </template>
        </el-table-column>
        <el-table-column label="联系人" min-width="150px" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.contact }}</span>
            <el-input v-if="scope.row.Edit" v-model="scope.row.contact" placeholder="1-32字符" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" maxlength="32" style="width:95%;padding:10px 0;" />
          </template>
        </el-table-column>
        <el-table-column label="联系电话" min-width="150px" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.phone }}</span>
            <el-input v-if="scope.row.Edit" v-model="scope.row.phone" placeholder="请输入11位手机号" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" maxlength="11" style="width:95%;padding:10px 0;" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="80" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="{row}">
            <!-- 编辑 -->
            <el-button v-if="row.isUpdate && isRoleEdit" icon="el-icon-edit-outline" class="miniSuccess" @click="handleUpdate(row)" />
            <!-- 编辑保存 -->
            <el-button v-if="row.isUpdateSave && isRoleEdit" icon="el-icon-folder-checked" class="miniSuccess" @click="updateData(row)" />
            <span v-if="row.isUpdateSave && isRoleEdit" class="centerSpan">|</span>
            <el-button v-if="row.isUpdateSave && isRoleEdit" icon="el-icon-close" class="minCancel" @click="updateCancel(row)" />
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="table.total>=0" :total="table.total" :page.sync="table.getdataListParm.offset" :limit.sync="table.getdataListParm.pagecount" @pagination="getList" />
    </div>
  </div>

</template>

<script>
import { GetDataByName, PostDataByName, failproccess, checkButtons } from '@/api/common'
import Cookies from 'js-cookie'
import Pagination from '@/components/Pagination'
export default {
  name: 'RanchInformation',
  components: { Pagination },
  data() {
    return {
      isRoleEdit: [],
      enableList: [{ id: 1, name: '是' }, { id: 0, name: '否' }],

      table: {
        getdataListParm: {
          name: 'getPastureList',
          page: 1,
          offset: 1,
          pagecount: parseInt(Cookies.get('pageCount')),
          returntype: 'Map',
          parammaps: {
            'pastureid': Cookies.get('pastureid'),
            'phone': '',
            'contact': '',
            'address': '',
            'pastureName': '',
            enable: ''
          }
        },
        tableKey: 0,
        list: [],
        total: 0,
        listLoading: true,
        temp: {}
      },
      requestParam: {},
      arrowDown: true,
      arrowUp: false,
      rowStyle: { maxHeight: 30 + 'px', height: 30 + 'px' },
      cellStyle: { padding: 0 + 'px' }

    }
  },

  created() {
    this.getList()
    this.getButtons()
  },
  mounted() {
    document.addEventListener('click', (e) => {
      if (this.$refs.selectInput !== undefined) {
        if (!this.$refs.selectInput.contains(e.target)) {
          this.arrowDown = true
          this.arrowUp = false
        } else {
          this.arrowDown = false
          this.arrowUp = true
        }
      }
    })
  },
  methods: {
    getButtons() {
      const Edit = 'RanchInformation'
      const isRoleEdit = checkButtons(JSON.parse(sessionStorage.getItem('buttons')), Edit)
      this.isRoleEdit = isRoleEdit
    },
    getList() {
      this.table.listLoading = true
      GetDataByName(this.table.getdataListParm).then(response => {
        console.log('table数据', response.data.list)
        if (response.data.list !== null) {
          for (let i = 0; i < response.data.list.length; i++) {
            response.data.list[i].enable = parseInt(response.data.list[i].enable)
            this.$set(response.data.list[i], 'Edit', false) // 编辑
            this.$set(response.data.list[i], 'NoEdit', true) // 不可编辑/输入
            this.$set(response.data.list[i], 'isCreate', false) // 新增操作
            this.$set(response.data.list[i], 'isUpdate', true) // 编辑操作
            this.$set(response.data.list[i], 'isUpdateSave', false) // 编辑保存
          }
          this.table.list = response.data.list
          this.table.pageNum = response.data.pageNum
          this.table.pageSize = response.data.pageSize
        } else {
          this.table.list = []
        }
        this.table.total = response.data.total
        setTimeout(() => {
          this.table.listLoading = false
        }, 100)
      })
    },
    handleEnableChange() {
      console.log('点击了是否启用')
    },
    handleConfirmStartChange() {
      console.log('点击了确认开始')
    },
    handleSearch() {
      console.log('点击了查询')
      this.getList()
      this.table.getdataListParm.offset = 1
      this.arrowDown = true
      this.arrowUp = false
    },
    handleRefresh() {
      console.log('点击了重置')
      this.table.getdataListParm.parammaps.empCode = ''
      this.table.getdataListParm.parammaps.phone = ''
      this.table.getdataListParm.parammaps.enable = ''
      this.table.getdataListParm.parammaps.contact = ''
      this.table.getdataListParm.parammaps.address = ''
      this.table.getdataListParm.parammaps.pastureName = ''
      this.table.getdataListParm.offset = 1
      this.getList()
    },
    handleSelectionChange(val) {
      console.log('勾选数据', val)
    },
    handleUpdate(row) {
      console.log('点击了编辑')
      for (let i = 0; i < this.table.list.length; i++) {
        if (this.table.list[i].Edit == true) {
          this.$message({ type: 'error', message: '当前内容未保存，请点击取消或保存继续进行操作', duration: 2000 })
          return false
        }
      }
      // 编辑true,不可编辑false
      row.Edit = true
      row.NoEdit = false
      // 新增false,编辑false,编辑保存true
      row.isCreate = false
      row.isUpdate = false
      row.isUpdateSave = true
    },
    updateData(row) {
      console.log('点击了编辑保存', row)
      this.table.temp.pastureid = row.pastureid
      this.table.temp.phone = row.phone
      this.table.temp.enable = row.enable
      this.table.temp.contact = row.contact
      this.table.temp.address = row.address
      this.table.temp.pastureName = row.pastureName

      // 检验手机号是否为11位数字
      if (this.table.temp.phone !== undefined && this.table.temp.phone !== '') {
        const phone = /^\d{11}$/
        if (!phone.test(this.table.temp.phone)) {
          this.$message({ type: 'error', message: '请输入11位手机号', duration: 2000 })
          return false
        }
      }
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      this.requestParam.name = 'updatepastureinfo'
      this.requestParam.parammaps = this.table.temp
      PostDataByName(this.requestParam).then(response => {
        console.log('新增保存发送参数', this.requestParam)
        if (response.msg !== 'fail') {
          this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
          this.getList()
        } else {
          this.$notify({ title: '保存失败', message: response.data, type: 'warning', duration: 2000 })
        }
      })
    },
    updateCancel(row) {
      console.log('点击了编辑取消')
      // 编辑false,不可编辑true
      row.Edit = false
      row.NoEdit = true
      // 新增false,编辑true,编辑保存false
      row.isCreate = false
      row.isUpdate = true
      row.isUpdateSave = false
      this.getList()
    }
  }
}
</script>
<style lang="scss" scoped>
  .search{margin-top:10px;}
  
  $width:350px;
  $left:325px;
  .selectInput{
    position: relative;
    display: inline-block;
    .Input{width: $width;position: relative;}
    .el-icon-arrow-down{width: 30px;height: 30px;position: absolute;left: $left;top:10px;color:#C0C4CC;}
    .el-icon-arrow-up{width: 30px;height: 30px;position: absolute;left: $left;top:10px;color:#C0C4CC;}
    .selectUl{
      z-index: 111;width: $width;background: #fff;border: 1px solid #E4E7ED;box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);margin: -1px 0 0 0;padding: 6px 0; margin: 0;box-sizing: border-box;position: absolute;
      li{
        list-style: none;font-size: 14px; padding: 0 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; color: #606266; height: 50px; line-height: 50px; box-sizing: border-box; cursor: pointer;
        a{float:left;width: 80px;}
        a:hover{color: rgba(0, 204, 102, 0.71); font-weight: 700;}
      }
    }
  }

 </style>
