<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select v-model="table.getdataListParm.parammaps.pastureName" placeholder="牧场" style="width: 140px;" class="filter-item">
        <el-option v-for="item in findAllPasture" :key="item.id" :label="item.name" :value="item.name" />
      </el-select>
      <el-button class="filter-item" type="primary" icon="el-icon-search" @click="form_search">搜索</el-button>
    </div>
    <div class="app-operation" style="height:50px">
      <el-button class="filter-item" style="margin-left: 10px;" type="primary" icon="el-icon-edit" @click="form_add">新增</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"
      >
        <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="150px" align="center" prop="areaName" />
        <el-table-column label="牧场" min-width="150px" align="center" prop="pastureName" />
        <el-table-column label="牧场全称" min-width="150px" align="center" prop="companyName" />
        <el-table-column label="牧场编号" min-width="150px" align="center" prop="pastureNumber" />
        <el-table-column label="中心" min-width="150px" align="center" prop="center" />
        <el-table-column label="地图经度" min-width="150px" align="center" prop="longitude" />
        <el-table-column label="地图纬度" min-width="150px" align="center" prop="latitude" />
        <el-table-column label="备注" min-width="150px" align="center" prop="note" />
        <el-table-column label="顺序" min-width="150px" align="center" prop="sort" />
        <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width" fixed="right">
          <template slot-scope="{row}">
            <el-button type="primary" size="mini" @click="form_see(row)">查看</el-button>
            <el-button type="success" size="mini" @click="form_edit(row)">编辑</el-button>
          </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 class="create">
      <el-dialog :title="textMap[create.dialogStatus]" :visible.sync="create.dialogFormVisible" :close-on-click-modal="false" width="40%">
        <el-form ref="createTemp" :rules="create.rules" :model="create.temp" label-position="right" label-width="110px" style="width: 80%;margin: 0 auto;">
          <el-row>
            <el-col :span="24">
              <el-form-item label="所属区域：" prop="areaId" v-if="create.dialogStatus =='create'">
                <el-select  v-model="create.temp.areaId" placeholder="所属区域" style="width: 100%;">
                  <el-option v-for="item in regionList" :key="item.id" :label="item.name" :value="item.id" />
                </el-select>
              </el-form-item>
              <el-form-item label="所属区域："  v-else>
                <!-- <el-input ref="areaName" v-model="create.temp.areaName" placeholder="所属区域"  style="width: 100%;" /> -->
                <el-select  v-model="create.temp.areaid" placeholder="所属区域" style="width: 100%;" :disabled="create.dialogStatus =='see'" >
                  <el-option  v-for="item in regionList" :key="item.id" :label="item.name" :value="parseInt(item.id)" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="牧场：" prop="pastureName">
                <el-input ref="pastureName" v-model="create.temp.pastureName" placeholder="牧场" style="width: 100%;" :disabled="create.dialogStatus =='see'"  /><!-- :disabled="create.dialogStatus =='see'"  -->
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="牧场全称：" prop="companyName">
                <el-input ref="companyName" v-model="create.temp.companyName" placeholder="牧场全称"  style="width: 100%;" :disabled="create.dialogStatus =='see'"  />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="牧场编号：" prop="pastureNumber">
                <el-input ref="pastureNumber" v-model="create.temp.pastureNumber" placeholder="牧场编号" style="width: 100%;" :disabled="create.dialogStatus =='see'"  />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="中心: " prop="center">
                <el-input ref="center" v-model="create.temp.center" placeholder="中心" style="width: 100%;" :disabled="create.dialogStatus =='see'"  />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="地图经度：" prop="longitude">
                <el-input ref="longitude" v-model="create.temp.longitude" type="number" placeholder="请输入数字" :disabled="create.dialogStatus =='see'" style="width: 100%;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="地图纬度：" prop="latitude">
                <el-input ref="latitude" v-model="create.temp.latitude" type="number" placeholder="请输入数字" :disabled="create.dialogStatus =='see'" style="width: 100%;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注：" prop="note">
                <el-input ref="note" v-model="create.temp.note" placeholder="备注" :disabled="create.dialogStatus =='see'" style="width: 100%;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row v-if="create.dialogStatus !=='create'">
            <el-col :span="24">
              <el-form-item label="顺序：" prop="sort">
                <el-input ref="sort" v-model="create.temp.sort" type="number" placeholder="顺序" :disabled="create.dialogStatus =='see'" style="width: 100%;" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button v-if="create.dialogStatus !== 'see'" :disabled="isokDisable" type="primary" @click="create.dialogStatus =='create'?add_dialog_save():edit_dialog_save()">保存并关闭</el-button>
          <el-button @click="create.dialogFormVisible = false;getList()">关闭</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { GetDataByName, GetDataByNames, ExecDataByConfig } from '@/api/common'
import Pagination from '@/components/Pagination'
import { MessageBox } from 'element-ui'
import Cookies from 'js-cookie'
export default {
  name: 'RanchManagement',
  components: { Pagination },
  data() {
    return {
      requestParams: [
        { name: 'findAllPasture', offset: 0, pagecount: 0, returntype: 'Map', parammaps: { 'id': Cookies.get('pastureid') }},
        { name: 'getareaList', offset: 0, pagecount: 0, returntype: 'Map', parammaps: {}}
      ],
      findAllPasture: [],
      regionList: [], // 所属区域
      areaIds:[],
      table: {
        tableKey: 0,
        list: null,
        total: 0,
        listLoading: true,
        getdataListParm: {
          name: 'getPastureInforList',
          page: 1,
          offset: 1,
          pagecount: 10,
          returntype: 'Map',
          parammaps: {}
        }
      },
      create: {
        dialogFormVisible: false,
        dialogStatus: '',
        rules: {
          areaId: [{ required: true, message: '必填', trigger: 'blur' }],
          pastureName: [{ required: true, message: '必填', trigger: 'blur' }],
          companyName: [{ required: true, message: '必填', trigger: 'blur' }],
          pastureNumber: [{ required: true, message: '必填', trigger: 'blur' }]
        },
        temp: {
          areaId: '', pastureName: '', pastureNumber: '', longitude: '', latitude: '', note: '', sort: '',center:''
        }
      },
      postDataParam: {},
      textMap: { create: '新增', update: '编辑', see: '查看' },
      isokDisable: false,
      rowStyle: { maxHeight: 50 + 'px', height: 45 + 'px' },
      cellStyle: { padding: 0 + 'px' }
    }
  },
  created() {
    this.get_select_list()
    this.getList()
  },
  methods: {
    get_select_list() {
      GetDataByNames(this.requestParams).then(response => {
        this.findAllPasture = response.data.findAllPasture.list
        this.regionList = response.data.getareaList.list
        this.regionList.forEach(row => {
          this.areaIds[row.id]= row.name
        });
      })
    },
    getList() {
      this.table.listLoading = true
      GetDataByName(this.table.getdataListParm).then(response => {
        this.table.list = response.data.list
        this.table.pageNum = response.data.pageNum
        this.table.pageSize = response.data.pageSize
        if (response.data.total) {
          this.table.total = response.data.total
        }
        setTimeout(() => {
          this.table.listLoading = false
        }, 100)
      })
    },
    form_search() {
      this.table.getdataListParm.offset = 1
      this.getList()
    },
    resetCreateTemp() {
      this.create.temp.areaId = ''
      this.create.temp.pastureName = ''
      this.create.temp.pastureNumber = ''
      this.create.temp.longitude = ''
      this.create.temp.latitude = ''
      this.create.temp.note = ''
      this.create.temp.sort = ''
      this.create.temp.center = ''
    },
    form_add() {
      this.resetCreateTemp()
      this.create.dialogStatus = 'create'
      this.create.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['createTemp'].clearValidate()
      })
    },
    add_dialog_save() {
      this.$refs['createTemp'].validate(valid => {
        if (valid) {
          this.isokDisable = true
          setTimeout(() => {
            this.isokDisable = false
          }, 1000)
          this.postDataParam.common = { 'returnmap': '0' }
          this.postDataParam.data = []
          this.postDataParam.data[0] = { 'name': 'insertPasture', 'type': 'e', 'parammaps': {
            pastureName: this.create.temp.pastureName,
            pastureNumber: this.create.temp.pastureNumber,
            longitude: this.create.temp.longitude,
            latitude: this.create.temp.latitude,
            note: this.create.temp.note,
            center:this.create.temp.center,
            companyName: this.create.temp.companyName
          }}
          this.postDataParam.data[1] = { 'name': 'insertPastureDept', 'type': 'e', 'parammaps': {
            pastureName: this.create.temp.pastureName,
            pastureId: '@insertPasture.LastInsertId',
            areaId: this.create.temp.areaId
          }}
          ExecDataByConfig(this.postDataParam).then(response => {
            console.log('新增保存发送参数', this.postDataParam)
            if (response.msg === 'fail') {
              this.$notify({ title: '保存失败', message: response.data, type: 'warning', duration: 2000 })
            } else {
              this.create.dialogFormVisible = false
              this.$notify({ title: '', message: '保存成功', type: 'success', duration: 2000 })
              this.getList()
            }
          })
        }
      })
    },
    form_edit(row) {
      console.log(row)
      this.create.temp = Object.assign({}, row)
      this.create.dialogStatus = 'update'
      this.create.dialogFormVisible = true
    },
    edit_dialog_save() {
      this.$refs['createTemp'].validate(valid => {
        if (valid) {
          this.isokDisable = true
          setTimeout(() => {
            this.isokDisable = false
          }, 1000)
          this.postDataParam.common = { 'returnmap': '0' }
          this.postDataParam.data = []
          this.postDataParam.data[0] = { 'name': 'updatePastureDept', 'type': 'e', 'parammaps': {
            name:this.create.temp.pastureName,
            companyName:this.create.temp.companyName,
            pastureNumber:this.create.temp.pastureNumber,
            center:this.create.temp.center,
            region:this.areaIds[this.create.temp.areaid],
            longitude: this.create.temp.longitude,
            latitude: this.create.temp.latitude,
            note: this.create.temp.note,
            id: this.create.temp.id,
            sort: this.create.temp.sort
          }}
          this.postDataParam.data[1] = { 'name': 'updateDepartment', 'type': 'e', 'parammaps': {
            dname: this.create.temp.pastureName,
            pastureId: this.create.temp.areaid,
            id: this.create.temp.id
          }}
          ExecDataByConfig(this.postDataParam).then(response => {
            console.log('编辑保存发送参数', this.postDataParam)
            if (response.msg === 'fail') {
              this.$notify({ title: '保存失败', message: response.data, type: 'warning', duration: 2000 })
            } else {
              this.create.dialogFormVisible = false
              this.$notify({ title: '', message: '保存成功', type: 'success', duration: 2000 })
              this.getList()
            }
          })
        }
      })
    },
    form_see(row) {
      this.create.dialogStatus = 'see'
      this.create.dialogFormVisible = true
      this.create.temp = Object.assign({}, row)
    }
  }
}
</script>
<style>
  input::-webkit-outer-spin-button,
      input::-webkit-inner-spin-button {
          -webkit-appearance: none;
      }
      input[type="number"]{
          -moz-appearance: textfield;
      }

</style>
