<template>
  <div>
    <xy-dialog ref="dialog" :width='70' :is-show.sync="isShow" type="form" :title="type==='add'?'新增防汛队伍':'编辑防汛队伍'"
      :form="form" :rules="rules" @submit="submit">
      <template v-slot:mingcheng>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;">*</span>队伍名称：
          </div>
          <div class="xy-table-item-content">
            <el-input style="width:300px" v-model="form.mingcheng"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:gongsi>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;"></span>公司：
          </div>
          <div class="xy-table-item-content">
            <el-input style="width:300px" v-model="form.gongsi"></el-input>

          </div>
        </div>
      </template>
      <template v-slot:fuzeren>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;"></span>负责人：
          </div>
          <div class="xy-table-item-content">
            <el-select  style="width:300px"  v-model="form.fuzeren" @change="changeFuzeren" placeholder="请选择">
              <el-option v-for="item in bookList" :key="item.id" :label="item.xingming" :value="item.xingming">
              </el-option>
            </el-select>
          </div>
        </div>
      </template>
      <template v-slot:duizhang>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;"></span>队长：
          </div>
          <div class="xy-table-item-content">
            <el-select style="width:300px"  v-model="form.duizhang"placeholder="请选择">
              <el-option v-for="item in bookList" :key="item.id" :label="item.xingming" :value="item.xingming">
              </el-option>
            </el-select>
          </div>
        </div>
      </template>
      <template v-slot:lianxidianhua>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;"></span>联系电话：
          </div>
          <div class="xy-table-item-content">
            <el-input style="width:300px" v-model="form.lianxidianhua"></el-input>

          </div>
        </div>
      </template>
      <template v-slot:renshu>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;"></span>人数：
          </div>
          <div class="xy-table-item-content">
            <el-input style="width:300px" v-model="form.renshu"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:quyu>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;"></span>区域：
          </div>
          <div class="xy-table-item-content">
            <el-input style="width: 300px;" v-model="form.quyu"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:dizhi>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;"></span>地址：
          </div>
          <div class="xy-table-item-content">
            <avue-input-map v-model="mapform" :params="mapparams" style="width:300px"
              placeholder="请选择地图"></avue-input-map>
          </div>
        </div>
      </template>
      <template v-slot:jingdu>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;"></span>经度：
          </div>
          <div class="xy-table-item-content">
            <el-input style="width: 300px;" disabled v-model="form.jingdu"></el-input>
          </div>
        </div>
      </template>
      <template v-slot:weidu>
        <div class="xy-table-item">
          <div class="xy-table-item-label">
            <span style="color: red;font-weight: 600;padding-right: 4px;"></span>纬度：
          </div>
          <div class="xy-table-item-content">
            <el-input style="width: 300px;" disabled v-model="form.weidu"></el-input>
          </div>
        </div>
      </template>
    </xy-dialog>

  </div>
</template>

<script>
  import {
    save,
    show,
    index
  } from "@/api/system/baseForm.js"
  import {
    Message
  } from 'element-ui'
  export default {

    data() {
      return {
        isShow: false,
        type: 'add',
        id: '',
        tableName: 'teams',
        mapparams: {
          zoom: 11,
        },
        mapform: [],
        form: {
          mingcheng: '',
          gongsi: '',
          fuzeren: '',
          duizhang: '',
          lianxidianhua: '',
          renshu: '',
          quyu: '',
          dizhi: '',
          jingdu: '',
          weidu: '',
          duiyuan: []
        },
        bookList: [],
        rules: {
          mingcheng: [{
            required: true,
            message: '请填写队伍名称'
          }]
        },

      }
    },
    created() {
      this.getBooks()
    },
    methods: {
      async getBooks() {
        const res = await index({
          table_name: 'books',
          page: 1,
          page_size: 999
        })
        this.bookList = res.data
      },
      changeFuzeren(e){
        this.bookList.map(item=>{
          if(e==item.xingming){
            this.form.lianxidianhua = item.lianxidianhua
          }
        })
      },
      async getDetail() {
        const res = await show({
          id: this.id,
          table_name: this.tableName
        })
        this.$integrateData(this.form, res)
        this.mapform = [res.jingdu, res.weidu, res.dizhi]
      },

      submit() {
        if (this.type === 'add') {
          save({
            table_name: this.tableName,
            ...this.form
          }).then(res => {
            Message({
              type: 'success',
              message: '新增成功'
            })
            this.$emit('refresh')
            this.isShow = false

          })
          return
        }
        if (this.type === 'editor') {
          save({
            id: this.id,
            table_name: this.tableName,
            ...this.form
          }).then(res => {
            Message({
              type: 'success',
              message: '编辑成功'
            })
            this.$emit('refresh')
            this.isShow = false
          })
        }
      },

    },
    watch: {
      isShow(newVal) {
        if (newVal) {
          if (this.type === 'editor') {
            this.getDetail()
          }
        } else {
          this.id = ''
          this.type = ''
          this.mapform = []
          this.$refs['dialog'].reset()
        }
      },
      mapform(newVal, oldVal) {
        console.log(newVal)
        this.form.jingdu = newVal[0];
        this.form.weidu = newVal[1];
        this.form.dizhi = newVal[2];
      },
    }

  }
</script>

<style scoped lang="scss">
  ::v-deep .xy-table-item-label {
    min-width: 160px !important
  }
</style>
