<template>
  <div class="app-container">
    <div class="search">
      <el-input v-model="tab1.table.getdataListParm.parammaps.groupsname" placeholder="集团名称" style="width: 180px;" class="filter-item" clearable />
      <el-select v-model="tab1.table.getdataListParm.parammaps.enable" placeholder="状态" class="filter-item" style="width: 120px;" clearable>
        <el-option v-for="item in stateList" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-button class="successBorder" @click="form_search">查询</el-button>
      <el-button class="successBorder" @click="handleRefresh">重置</el-button>
    </div>
    <div class="operation">
      <el-button v-if="isRoleEdit" icon="el-icon-plus" class="success" @click="handleCreate">新增</el-button>
    </div>
    <div class="table">
      <el-table
        :key="tab1.table.tableKey"
        v-loading="tab1.table.listLoading"
        element-loading-text="给我一点时间"
        :data="tab1.table.list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        :row-style="rowStyle"
        :cell-style="cellStyle"
        class="elTable table-fixed"
      >
        <el-table-column label="序号" align="center" type="index" width="50px">
          <template slot-scope="scope">
            <span>{{ scope.$index + (tab1.table.pageNum-1) * tab1.table.pageSize + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="集团ID" min-width="130px" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.groupscode }}</span>
            <el-input v-if="scope.row.Edit" v-model="scope.row.groupscode" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" maxlength="32" placeholder="1-32字符" style="width:95%;padding:10px 0;" />
          </template>
        </el-table-column>
        <el-table-column label="集团名称" min-width="130px" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.groupsname }}</span>
            <el-input v-if="scope.row.Edit" v-model="scope.row.groupsname" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" maxlength="32" placeholder="1-32字符" style="width:95%;padding:10px 0;" />
          </template>
        </el-table-column>
        <el-table-column label="联系人" min-width="130px" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.emp }}</span>
            <el-input v-if="scope.row.Edit" v-model="scope.row.emp" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" maxlength="32" placeholder="1-32字符" style="width:95%;padding:10px 0;" />
          </template>
        </el-table-column>
        <el-table-column label="联系方式" min-width="130px" 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.trim="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="管理员用户名" min-width="130px" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.user }}</span>
            <el-input v-if="scope.row.Edit" v-model="scope.row.user" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" maxlength="32" placeholder="1-32字符" style="width:95%;padding:10px 0;" />
          </template>
        </el-table-column>
        <el-table-column label="状态" min-width="110px" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.enable" :disabled="scope.row.NoEdit==true" active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0" />
          </template>
        </el-table-column>
        <el-table-column label="备注" min-width="130px" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.NoEdit">{{ scope.row.note }}</span>
            <el-input v-if="scope.row.Edit" v-model="scope.row.note" type="textarea" :autosize="{ minRows: 1.3, maxRows: 4}" maxlength="255" placeholder="1-255字符" style="width:95%;padding:10px 0;" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="180" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="{row}">
            <!-- 页面权限 -->
            <a v-if="row.NoEdit && isRoleEdit" class="correcting" @click="handlePageConfiguration(row)">页面配置</a>
            <!-- 新增 -->
            <el-button v-if="row.isCreate && isRoleEdit" icon="el-icon-folder-checked" class="miniSuccess" :disabled="isokDisable" @click="createData(row)" />
            <span v-if="row.isCreate" class="centerSpan">|</span>
            <el-button v-if="row.isCreate && isRoleEdit" icon="el-icon-close" class="minCancel" @click="createCancel(row)" />
            <!-- 编辑 -->
            <span v-if="row.isUpdate" class="centerSpan">|</span>
            <el-button v-if="row.isUpdate && isRoleEdit" icon="el-icon-edit-outline" class="miniSuccess" @click="handleUpdate(row)" />
            <!-- 关联 -->
            <span v-if="row.isUpdate" class="centerSpan">|</span>
            <a v-if="row.isUpdate" class="correcting" @click="handleRelation(row)">关联</a>
            <!-- 编辑保存 -->
            <el-button v-if="row.isUpdateSave && isRoleEdit" icon="el-icon-folder-checked" class="miniSuccess" :disabled="isokDisable" @click="updateData(row)" />
            <span v-if="row.isUpdateSave" 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="tab1.table.total>=0" :total="tab1.table.total" :page.sync="tab1.table.getdataListParm.offset" :limit.sync="tab1.table.getdataListParm.pagecount" @pagination="getList" />
    </div>
    <!-- 页面配置 -->
    <el-dialog :title="textMap[tab1.pageConfiguration.dialogStatus]" :visible.sync="tab1.pageConfiguration.dialogFormVisible" :close-on-click-modal="false" width="25%">
      <div style="height: 400px;overflow-y: auto;">
        <el-tree
          ref="tree"
          v-loading="tab1.pageConfiguration.listLoading"
          show-checkbox
          node-key="id"
          :default-expand-all="true"
          :data="tab1.pageConfiguration.data"
          :props="tab1.pageConfiguration.defaultProps"
          :default-checked-keys="tab1.pageConfiguration.defaultCheckedKeys"
          @check-change="handelCheckChange"
        />
      </div>
      <div slot="footer" class="dialog-footer" style="bottom:10px">
        <el-button class="cancelClose" @click="tab1.pageConfiguration.dialogFormVisible = false;getList(); ">取消</el-button>
        <el-button class="save" :disabled="isokDisable" @click="pageConfigurationData()">确认</el-button>
      </div>
    </el-dialog>
    <!-- 关联 -->
    <el-dialog :fullscreen="dialogFull" :visible.sync="tab1.relation.dialogFormVisible" :close-on-click-modal="false" width="70%">
      <template slot="title">
        <div class="avue-crud__dialog__header">
          <span class="el-dialog__title">
            <span style="display:inline-block;width:3px;height:20px;margin-right:5px; float: left;margin-top:2px" />
            {{ textMap[tab1.relation.dialogStatus] }}
          </span>
          <div class="avue-crud__dialog__menu" @click="dialogFull? dialogFull=false: dialogFull=true">
            <svg-icon v-if="dialogFull" icon-class="exit-fullscreen" />
            <svg-icon v-else icon-class="fullscreen" />
          </div>
        </div>
      </template>
      <div>
        <el-form ref="relationTemp" :rules="rules" label-position="right" :model="tab1.relation.temp" label-width="130px" style="width: 100%;">
          <el-row>
            <el-col :span="8">
              <el-form-item label="集团名称:" prop="groupsname">
                <el-input ref="groupsname" v-model="tab1.relation.temp.groupsname" disabled class="filter-item" placeholder="集团名称" type="text" style="width:100%;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="8">
              <el-form-item label="关联：" prop="radio">
                <el-radio-group v-model="tab1.relation.radio">
                  <el-radio-button label="集团" />
                  <el-radio-button label="牧场" />
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <div v-if="tab1.relation.radio =='集团'" class="group">
            <el-row>
              <el-col :span="16">
                <el-form-item label="选择关联集团：">
                  <el-autocomplete v-model="tab1.relation.group.groupsname" class="inline-input" :fetch-suggestions="groupSearch" placeholder="选择关联集团" style="width: 100%;" @select="handleGroupSelect">
                    <template slot-scope="{ item }">
                      <span class="addr"> 集团：{{ item.groupsname }}</span>
                    </template>
                  </el-autocomplete>
                </el-form-item>
              </el-col>
            </el-row>
            <div class="table">
              <el-table
                :key="tab1.relation.group.table.tableKey"
                v-loading="tab1.relation.group.table.listLoading"
                element-loading-text="给我一点时间"
                :data="tab1.relation.group.table.list"
                border
                fit
                highlight-current-row
                style="width: 100%;"
                :row-style="rowStyle"
                :cell-style="cellStyle"
                class="elTable table-fixed"
              >
                <el-table-column label="序号" align="center" type="index" width="50px" />
                <el-table-column label="集团ID" min-width="130px" align="center" prop="groupscode" />
                <el-table-column label="集团名称" min-width="130px" align="center" prop="groupsname" />
                <el-table-column label="管理员用户名" min-width="130px" align="center" prop="user" />
                <el-table-column label="状态" min-width="110px" align="center">
                  <template slot-scope="scope">
                    <el-switch v-model="scope.row.enable" disabled active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0" />
                  </template>
                </el-table-column>
                <el-table-column label="备注" min-width="130px" align="center" prop="note" />
                <el-table-column label="操作" align="center" width="50" class-name="small-padding fixed-width" fixed="right">
                  <template slot-scope="{row}">
                    <el-button class="miniDanger" icon="el-icon-delete" @click="handleGroupDelete(row)" />
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </div>
          <div v-if="tab1.relation.radio =='牧场'" class="pasture">
            <el-row>
              <el-col :span="16">
                <el-form-item label="选择关联牧场：">
                  <el-autocomplete v-model="tab1.relation.pasture.pastureName" class="inline-input" :fetch-suggestions="pastureSearch" placeholder="选择关联牧场" style="width: 100%;" @select="handlePastureSelect">
                    <template slot-scope="{ item }">
                      <span class="addr"> 牧场：{{ item.pasturename }}</span>
                    </template>
                  </el-autocomplete>
                </el-form-item>
              </el-col>
            </el-row>
            <div class="table">
              <el-table
                :key="tab1.relation.pasture.table.tableKey"
                v-loading="tab1.relation.pasture.table.listLoading"
                element-loading-text="给我一点时间"
                :data="tab1.relation.pasture.table.list"
                border
                fit
                highlight-current-row
                style="width: 100%;"
                :row-style="rowStyle"
                :cell-style="cellStyle"
                class="elTable table-fixed"
              >
                <el-table-column label="序号" align="center" type="index" width="50px" />
                <el-table-column label="牧场ID" min-width="130px" align="center" prop="pasturecode" />
                <el-table-column label="牧场名称" min-width="130px" align="center" prop="pasturename" />
                <el-table-column label="管理员用户名" min-width="130px" align="center" prop="user" />
                <el-table-column label="权限归属" min-width="130px" align="center" prop="groupsname" />
                <el-table-column label="状态" min-width="110px" align="center">
                  <template slot-scope="scope">
                    <el-switch v-model="scope.row.enable" disabled active-color="#13ce66" inactive-color="#ff4949" :active-value="1" :inactive-value="0" />
                  </template>
                </el-table-column>
                <el-table-column label="备注" min-width="130px" align="center" prop="note" />
                <el-table-column label="操作" align="center" width="50" class-name="small-padding fixed-width" fixed="right">
                  <template slot-scope="{row}">
                    <el-button class="miniDanger" icon="el-icon-delete" @click="handlePastureDelete(row)" />
                  </template>
                </el-table-column>
              </el-table>
            </div>
          </div>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer" style="bottom:10px;">
        <el-button class="cancelClose" @click="tab1.relation.dialogFormVisible = false;getList(); ">取消</el-button>
        <el-button v-if="tab1.relation.dialogStatus==='relation'" class="save" :disabled="isokDisable" @click="relationData()">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { GetDataByName, failproccess, transData, ExecDataByConfig, checkButtons } from '@/api/common'
import Cookies from 'js-cookie'
import { parseTime } from '@/utils/index.js'
import { validate11Digits } from '@/utils/validate.js'
import { MessageBox } from 'element-ui'
import Pagination from '@/components/Pagination'
export default {
  name: 'OperationManagement',
  components: { Pagination },
  data() {
    return {
      requestParams: [
        { name: 'getGroupsAllList', offset: 0, pagecount: 0, parammaps: {}}
      ],
      dialogFull: false,
      isRoleEdit: [],
      stateList: [{ id: 1, name: '启用' }, { id: 0, name: '禁止' }], // 状态
      tab1: {
        table: {
          getdataListParm: {
            name: 'getGroupsAllList',
            page: 1,
            offset: 1,
            pagecount: 10,
            returntype: 'Map',
            parammaps: {
              groupsname: '',
              enable: ''
            }
          },
          tableKey: 0,
          list: [],
          total: 0,
          listLoading: true,
          temp: {}
        },
        // 页面配置
        pageConfiguration: {
          dialogFormVisible: false,
          dialogStatus: '',
          temp: {},
          listLoading: true,
          getdataListParm: {
            name: 'getMenuandButtonlistJT'
          },
          parentType: [],
          defaultProps: {
            children: 'children',
            label: 'name'
          },
          defaultCheckedKeys: [],
          arrList: []
        },
        // 关联
        relation: {
          dialogFormVisible: false,
          dialogStatus: '',
          radio: '集团',
          temp: {},
          group: {
            requestParam: {
              name: 'getGroupsAllListV2',
              page: 1,
              offset: 1,
              pagecount: 10,
              returntype: 'Map',
              parammaps: {
                groupsname: ''
              }
            },
            searchList: [],
            table: {
              tableKey: 1,
              list: [],
              total: 0,
              listLoading: false,
              getdataListParm: {
                name: 'groupsrelationlistJT',
                page: 1,
                offset: 1,
                pagecount: 10,
                returntype: 'Map',
                parammaps: {
                  groupsname: ''
                }
              }
            }
          },
          pasture: {
            requestParam: {
              name: 'groupsrelationlistMCAll',
              page: 1,
              offset: 1,
              pagecount: 10,
              returntype: 'Map',
              parammaps: {
                pastureName: ''
              }
            },
            searchList: [],
            table: {
              tableKey: 2,
              list: [],
              total: 0,
              listLoading: false,
              getdataListParm: {
                name: 'groupsrelationlistMC',
                page: 1,
                offset: 1,
                pagecount: 10,
                returntype: 'Map',
                parammaps: {
                  pastureName: ''
                }
              }
            }
          }
        }
      },
      requestParam: {},
      rules: {},
      isokDisable: false,
      textMap: {
        pageConfiguration: '页面配置',
        relation: '关联'
      },
      rowStyle: { maxHeight: 30 + 'px', height: 30 + 'px' },
      cellStyle: { padding: 0 + 'px' }

    }
  },

  created() {
    this.getList()
    this.getButtons()
  },

  methods: {
    getButtons() {
      const Edit = 'Role'
      const isRoleEdit = checkButtons(JSON.parse(sessionStorage.getItem('buttons')), Edit)
      this.isRoleEdit = isRoleEdit
    },
    getList() {
      this.tab1.table.listLoading = true
      GetDataByName(this.tab1.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++) {
            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.tab1.table.list = response.data.list
          this.tab1.table.pageNum = response.data.pageNum
          this.tab1.table.pageSize = response.data.pageSize
          this.tab1.table.total = response.data.total
        } else {
          this.tab1.table.list = []
        }
        setTimeout(() => {
          this.tab1.table.listLoading = false
        }, 100)
      })
    },

    form_search() {
      console.log('点击了查询')
      this.tab1.table.getdataListParm.offset = 1
      this.getList()
    },
    handleRefresh() {
      console.log('点击了重置')
      this.tab1.table.getdataListParm.parammaps.groupsname = ''
      this.tab1.table.getdataListParm.parammaps.enable = ''
      this.tab1.table.getdataListParm.offset = 1
      this.getList()
    },

    handleCreate() {
      console.log('点击了新增')
      // 编辑true/不可编辑false
      // 新增操true,编辑false，编辑保存false
      for (let i = 0; i < this.tab1.table.list.length; i++) {
        if (this.tab1.table.list[i].Edit == true) {
          this.$message({ type: 'error', message: '当前内容未保存，请点击取消或保存继续进行操作', duration: 2000 })
          return false
        }
      }
      this.tab1.table.list.unshift({ 'myId': (new Date()).valueOf(), 'Edit': true, 'NoEdit': false, 'isCreate': true, 'isUpdate': false, 'isUpdateSave': false, 'enable': 1, 'groupscode': '', 'groupsname': '', 'emp': '', 'phone': '', 'user': '', 'note': '' })
    },
    createData(row) {
      // 检验集团ID/集团名称是否为空
      console.log(row)
      if (row.groupscode == '' && row.groupsname == '' && row.user == '') {
        this.$message({ type: 'error', message: '集团ID/集团名称/管理员用户名不能为空', duration: 2000 })
        return false
      } else if (row.groupsname == '' && row.user == '') {
        this.$message({ type: 'error', message: '集团名称/管理员用户名不能为空', duration: 2000 })
        return false
      } else if (row.groupscode == '') {
        this.$message({ type: 'error', message: '集团ID不能为空', duration: 2000 })
        return false
      } else if (row.groupsname == '') {
        this.$message({ type: 'error', message: '集团名称不能为空', duration: 2000 })
        return false
      } else if (row.user == '') {
        this.$message({ type: 'error', message: '管理员用户名不能为空', duration: 2000 })
        return false
      }
      if (row.phone !== undefined && row.phone !== '') {
        const phone = /^\d{11}$/
        if (!phone.test(row.phone)) {
          this.$message({ type: 'error', message: '请输入11位手机号', duration: 2000 })
          return false
        }
      }
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      this.requestParam = {}
      this.requestParam.common = { 'returnmap': '0' }
      this.requestParam.data = []
      this.requestParam.data[0] = { 'name': 'checkuser', 'type': 'v', 'parammaps': {
        'user': row.user
      }}
      this.requestParam.data[1] = { 'name': 'insertGroups', 'type': 'e', 'parammaps': {
        'groupsname': row.groupsname,
        'groupscode': row.groupscode,
        'emp': row.emp,
        'phone': row.phone,
        'enable': row.enable,
        'note': row.note,
        'user': row.user
      }}
      this.requestParam.data[2] = { 'name': 'createNewPasture', 'type': 'e', 'parammaps': {
        'user': row.user,
        'pastureid': '@insertGroups.LastInsertId',
        'type': '1'
      }}

      ExecDataByConfig(this.requestParam).then(response => {
        console.log('新增保存发送参数', this.requestParam)
        if (response.msg !== 'fail') {
          this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
          this.getList()
        } else {
          const tId = new RegExp("column 'id'")
          if (tId.test(response.data)) {
            this.$notify({ title: '保存失败', message: '集团已存在，不可重复生成', type: 'warning', duration: 2000 })
          } else {
            this.$notify({ title: '保存失败', message: response.data, type: 'warning', duration: 2000 })
          }
        }
      })
    },
    createCancel(row) {
      console.log('点击了集团新增取消')
      for (let i = 0; i < this.tab1.table.list.length; i++) {
        if (row.myId === this.tab1.table.list[i].myId) {
          var listIndex = this.tab1.table.list.indexOf(this.tab1.table.list[i])
        }
        if (listIndex > -1) {
          this.tab1.table.list.splice(listIndex, 1)
          return
        }
      }
    },
    handleUpdate(row) {
      console.log('点击了编辑')
      for (let i = 0; i < this.tab1.table.list.length; i++) {
        if (this.tab1.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) {
      if (row.pasturecode == '' && row.pasturename == '' && row.user == '') {
        this.$message({ type: 'error', message: '牧场ID/牧场名称/管理员用户名不能为空', duration: 2000 })
        return false
      } else if (row.pasturename == '' && row.user == '') {
        this.$message({ type: 'error', message: '牧场名称/管理员用户名不能为空', duration: 2000 })
        return false
      } else if (row.pasturecode == '') {
        this.$message({ type: 'error', message: '牧场ID不能为空', duration: 2000 })
        return false
      } else if (row.pasturename == '') {
        this.$message({ type: 'error', message: '牧场名称不能为空', duration: 2000 })
        return false
      } else if (row.user == '') {
        this.$message({ type: 'error', message: '管理员用户名不能为空', duration: 2000 })
        return false
      }
      if (row.phone !== undefined && row.phone !== '') {
        const phone = /^\d{11}$/
        if (!phone.test(row.phone)) {
          this.$message({ type: 'error', message: '请输入11位手机号', duration: 2000 })
          return false
        }
      }
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      this.requestParam = {}
      this.requestParam.common = { 'returnmap': '0' }
      this.requestParam.data = []
      this.requestParam.data[0] = { 'name': 'updateGroups', 'type': 'e', 'parammaps': {
        'id': row.id,
        'groupsname': row.groupsname,
        'groupscode': row.groupscode,
        'emp': row.emp,
        'phone': row.phone,
        'enable': row.enable,
        'note': row.note,
        'user': row.user
      }}
      this.requestParam.data[1] = { 'name': 'updateUserName', 'type': 'e', 'parammaps': {
        'id': row.id,
        'userold': row.userold,
        'user': row.user
      }}
      ExecDataByConfig(this.requestParam).then(response => {
        console.log('编辑保存发送参数', this.requestParam)
        if (response.msg !== 'fail') {
          this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
          this.getList()
        } else {
          const tId = new RegExp("column 'id'")
          if (tId.test(response.data)) {
            this.$notify({ title: '保存失败', message: '集团已存在，不可重复生成', type: 'warning', duration: 2000 })
          } 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()
    },
    handlePageConfiguration(row) {
      this.tab1.pageConfiguration.temp = Object.assign({}, row)
      console.log('点击了行内页面配置')
      this.tab1.pageConfiguration.dialogStatus = 'pageConfiguration'
      this.tab1.pageConfiguration.dialogFormVisible = true
      this.getPageConfigurationList()
    },
    getPageConfigurationList() {
      this.tab1.pageConfiguration.getdataListParm.parammaps = {}
      this.tab1.pageConfiguration.getdataListParm.parammaps.pastureid = this.tab1.pageConfiguration.temp.pastureid
      this.tab1.pageConfiguration.getdataListParm.parammaps.id = this.tab1.pageConfiguration.temp.id
      this.tab1.pageConfiguration.listLoading = true
      GetDataByName(this.tab1.pageConfiguration.getdataListParm).then(response => {
        if (response.data.list !== null) {
          for (let i = 0; i < response.data.list.length; i++) {
            if (parseFloat(response.data.list[i].seebutton) > 0 && response.data.list[i].pid !== -1) {
              this.tab1.pageConfiguration.defaultCheckedKeys.push(String(response.data.list[i].menu_id))
            }
            if (parseFloat(response.data.list[i].seebutton) > 0) {
              this.tab1.pageConfiguration.arrList.push(String(response.data.list[i].menu_id))
            }
          }
          this.tab1.pageConfiguration.data = transData(response.data.list, 'id', 'pid', 'children')
          console.log(this.tab1.pageConfiguration.data)
          console.log(response.data.list)
        } else {
          this.tab1.pageConfiguration.data = []
        }
        setTimeout(() => {
          this.tab1.pageConfiguration.listLoading = false
        }, 1000)
      })
    },
    handelCheckChange(id) {
      this.tab1.pageConfiguration.defaultCheckedKeys = this.$refs.tree.getCheckedKeys()
      var checkedKeys = this.$refs.tree.getCheckedKeys()
      var options = checkedKeys.map((item) => {
        var node = this.$refs.tree.getNode(item) // 所有被选中的节点对应的node
        const tmpMap = {}
        console.log(node, 'node')
        tmpMap.pid = node.parent.key
        tmpMap.value = node.key
        return tmpMap
      })
      var selectedData = options.map((item) => {
        console.log(item)
        return item
      })
      console.log(selectedData)
      var arr = []
      for (let i = 0; i < selectedData.length; i++) {
        if (selectedData[i].pid !== undefined) {
          arr.push(selectedData[i].pid, selectedData[i].value)
        } else {
          arr.push(selectedData[i].value)
        }
      }
      this.tab1.pageConfiguration.arrList = this.unique(arr)
    },
    unique(arr) {
      return Array.from(new Set(arr))
    },
    pageConfigurationData() {
      console.log('集团页面配置保存')
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      this.requestParam = {}
      this.requestParam.common = { 'returnmap': '0' }
      this.requestParam.data = []
      this.requestParam.data[0] = { 'name': 'deletemenuPastureJT', 'type': 'e', 'parammaps': {
        'id': this.tab1.pageConfiguration.temp.id
      }}
      var list = []
      for (let i = 0; i < this.tab1.pageConfiguration.arrList.length; i++) {
        list.push({ 'id': this.tab1.pageConfiguration.arrList[i] })
      }
      this.requestParam.data[1] = { 'name': 'insertSpotList', 'resultmaps': { 'list': list }}
      this.requestParam.data[1].children = []
      this.requestParam.data[1].children[0] = { 'name': 'insertRolepastureJT', 'type': 'e', 'parammaps': {
        menuid: '@insertSpotList.id',
        id: this.tab1.pageConfiguration.temp.id
      }}
      ExecDataByConfig(this.requestParam).then(response => {
        console.log('页面配置保存发送参数', this.requestParam)
        if (response.msg !== 'fail') {
          this.tab1.pageConfiguration.dialogFormVisible = false
          this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
          this.getList()
        } else {
          this.$notify({ title: '保存失败', message: response.data, type: 'warning', duration: 2000 })
        }
      })
    },
    handleRelation(row) {
      this.tab1.relation.temp = Object.assign({}, row)
      console.log('关联', this.tab1.relation.temp)
      this.dialogFull = false
      this.tab1.relation.dialogStatus = 'relation'
      this.tab1.relation.dialogFormVisible = true
      this.tab1.relation.radio = '集团'
      this.getGrouplist()
      this.getPasturelist()
    },
    getGrouplist() {
      this.tab1.relation.group.table.listLoading = true
      this.tab1.relation.group.table.getdataListParm.parammaps.id = this.tab1.relation.temp.id
      GetDataByName(this.tab1.relation.group.table.getdataListParm).then(response => {
        console.log('集团table数据', response.data.list)
        if (response.data.list !== null) {
          this.tab1.relation.group.table.list = response.data.list
        } else {
          this.tab1.relation.group.table.list = []
        }
        setTimeout(() => {
          this.tab1.relation.group.table.listLoading = false
        }, 100)
      })
    },
    getPasturelist() {
      this.tab1.relation.pasture.table.listLoading = true
      this.tab1.relation.pasture.table.getdataListParm.parammaps.id = this.tab1.relation.temp.id
      GetDataByName(this.tab1.relation.pasture.table.getdataListParm).then(response => {
        console.log('牧场table数据', response.data.list)
        if (response.data.list !== null) {
          this.tab1.relation.pasture.table.list = response.data.list
        } else {
          this.tab1.relation.pasture.table.list = []
        }
        setTimeout(() => {
          this.tab1.relation.pasture.table.listLoading = false
        }, 100)
      })
    },
    groupSearch(queryString, cb) {
      console.log(this.tab1.relation.temp, '====')
      this.tab1.relation.group.requestParam.parammaps.groupsname = queryString
      this.tab1.relation.group.requestParam.parammaps.id = this.tab1.relation.temp.id
      GetDataByName(this.tab1.relation.group.requestParam).then(response => {
        if (response.data.list !== null) {
          this.tab1.relation.group.searchList = response.data.list
          cb(this.tab1.relation.group.searchList)
        } else {
          cb([])
        }
      })
    },
    handleGroupSelect(item) {
      if (this.tab1.relation.group.table.list.length > 0) {
        if (this.tab1.relation.group.table.list.find(obj => obj.id === item.id)) {
          this.$message({ type: 'warning', message: '集团不可重复添加' })
        } else {
          this.tab1.relation.group.table.list.push(item)
        }
      } else {
        this.tab1.relation.group.table.list.push(item)
      }
    },
    handleGroupDelete(row) {
      for (var i = 0; i < this.tab1.relation.group.table.list.length; i++) {
        if (this.tab1.relation.group.table.list[i].id == row.id) {
          var listIndex = this.tab1.relation.group.table.list.indexOf(this.tab1.relation.group.table.list[i])
        }
        if (listIndex > -1) {
          this.tab1.relation.group.table.list.splice(listIndex, 1)
          return
        }
      }
    },
    pastureSearch(queryString, cb) {
      this.tab1.relation.pasture.requestParam.parammaps.pastureName = queryString
      GetDataByName(this.tab1.relation.pasture.requestParam).then(response => {
        if (response.data.list !== null) {
          this.tab1.relation.pasture.searchList = response.data.list
          cb(this.tab1.relation.pasture.searchList)
        } else {
          cb([])
        }
      })
    },
    handlePastureSelect(item) {
      if (this.tab1.relation.pasture.table.list.length > 0) {
        if (this.tab1.relation.pasture.table.list.find(obj => obj.id === item.id)) {
          this.$message({ type: 'warning', message: '集团不可重复添加' })
        } else {
          this.tab1.relation.pasture.table.list.push(item)
        }
      } else {
        this.tab1.relation.pasture.table.list.push(item)
      }
    },
    handlePastureDelete(row) {
      for (var i = 0; i < this.tab1.relation.pasture.table.list.length; i++) {
        if (this.tab1.relation.pasture.table.list[i].id == row.id) {
          var listIndex = this.tab1.relation.pasture.table.list.indexOf(this.tab1.relation.pasture.table.list[i])
        }
        if (listIndex > -1) {
          this.tab1.relation.pasture.table.list.splice(listIndex, 1)
          return
        }
      }
    },
    relationData() {
      console.log('关联保存')
      this.isokDisable = true
      setTimeout(() => {
        this.isokDisable = false
      }, 1000)
      this.requestParam = {}
      this.requestParam.common = { 'returnmap': '0' }
      this.requestParam.data = []
      this.requestParam.data[0] = { 'name': 'deletegroupsPasture', 'type': 'e', 'parammaps': {
        'id': this.tab1.relation.temp.id
      }}
      this.requestParam.data[1] = { 'name': 'insertSpotList', 'resultmaps': { 'list': this.tab1.relation.group.table.list }}
      this.requestParam.data[1].children = []
      this.requestParam.data[1].children[0] = { 'name': 'insertgroupsPasture', 'type': 'e', 'parammaps': {
        rid: '@insertSpotList.id',
        id: this.tab1.relation.temp.id,
        type: '1'
      }}
      this.requestParam.data[2] = { 'name': 'insertSpotList2', 'resultmaps': { 'list': this.tab1.relation.pasture.table.list }}
      this.requestParam.data[2].children = []
      this.requestParam.data[2].children[0] = { 'name': 'insertgroupsPasture', 'type': 'e', 'parammaps': {
        rid: '@insertSpotList2.id',
        id: this.tab1.relation.temp.id,
        type: '0'
      }}
      ExecDataByConfig(this.requestParam).then(response => {
        console.log('关联保存发送参数', this.requestParam)
        if (response.msg !== 'fail') {
          this.tab1.relation.dialogFormVisible = false
          this.$notify({ title: '成功', message: '保存成功', type: 'success', duration: 2000 })
          this.getList()
        } else {
          this.$notify({ title: '保存失败', message: response.data, type: 'warning', duration: 2000 })
        }
      })
    }

  }
}
</script>
<style lang="scss" scoped>
  
  .inside-base {
    position: relative;
  }

  .inside-base-main {
    width: 100%;
    background: #ffffff;
    box-shadow: 0px 0px 10px 0px rgba(231,232,238,1);
    border-radius: 4px;
    margin-bottom: 30px;
  }

  // 中间内容
  .inside-base-cont {
    margin: 0 20px;
  }

  .role-cont {
    position: relative;
    font-size: 16px;
    color: #333333;
    line-height: 40px;
    > span {
      width: 90px;
      height: 40px;
      text-align: right;
      float: left;
      margin-right: 10px;
      margin-bottom: 20px;
    }
    > div {
      position: relative;
      width: 100%;
      float: left;
      margin-bottom: 20px;
      >>> .el-input__inner {
        width: 320px;
      }
    }
  }
  .check-page-all {
    position: absolute;
    top: 13px;
    left: 11px;
    z-index: 99;
  }

  /* element-ui css */
  .inside-base {
    >>> .el-input__inner {
      font-size: 16px;
      color: #333333;
    }
  }
</style>
