<template>
  <div>
    <div>
      <div>
        <el-form ref="ref_form"
                 :inline="true"
                 :model="formData"
                 :rules="dataRule"
                 :disabled="formType === 'detail'"
                 class="form">

          <el-form-item label="场所类型"
                        label-width="150px"
                        prop="ninePlaceVal">
            <el-select class="item_width_1"
                       v-model="formData.ninePlaceVal"
                       placeholder="请选择"
                       clearable>
              <el-option v-for="item in placeTypeList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>

          </el-form-item>

          <el-form-item label="场所区域"
                        prop="gridId"
                        label-width="150px"
                        style="display: block">
            <el-select class="item_width_1"
                       v-model="formData.gridId"
                       placeholder="全部"
                       clearable>
              <el-option v-for="item in gridList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="场所名称"
                        label-width="150px"
                        prop="placeOrgName">
            <el-input v-model="formData.placeOrgName"
                      class="item_width_1"
                      clearable
                      placeholder="请输入九小场所名称">
            </el-input>

          </el-form-item>
          <el-form-item label="场所地址"
                        label-width="150px"
                        prop="address">
            <el-input v-model="formData.address"
                      class="item_width_1"
                      clearable
                      placeholder="请输入场所地址">
            </el-input>

          </el-form-item>

          <el-form-item label="规模"
                        label-width="150px"
                        prop="scale">
            <el-select class="item_width_1"
                       v-model="formData.scale"
                       placeholder="请选择"
                       clearable>
              <el-option v-for="item in scaleList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>

          </el-form-item>
          <el-form-item label="负责人"
                        label-width="150px"
                        prop="personInCharge">
            <el-input v-model="formData.personInCharge"
                      class="item_width_1"
                      clearable
                      placeholder="请输入负责人姓名">
            </el-input>

          </el-form-item>
          <el-form-item label="联系电话"
                        label-width="150px"
                        prop="mobile">
            <el-input v-model="formData.mobile"
                      class="item_width_1"
                      type='number'
                      clearable
                      placeholder="请输入电话">
            </el-input>

          </el-form-item>
        </el-form>
      </div>

    </div>
    <div class="div_btn">
      <el-button @click="handleCancle">取 消</el-button>
      <el-button v-if="formType != 'detail'"
                 type="primary"
                 :disabled="btnDisable"
                 @click="handleComfirm">确 定</el-button>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { Loading } from 'element-ui' // 引入Loading服务
import { requestPost } from '@/js/dai/request'

let loading // 加载动画
export default {
  data () {
    return {
      formType: 'add', //表单操作类型 add新增，edit编辑，detail详情

      btnDisable: false,

      placeOrgId: '',
      formData: {
        gridId: '',
        ninePlaceVal: '',
        placeOrgName: '',
        address: '',
        scale: '',
        personInCharge: '',
        mobile: ''
      },

    }
  },
  components: {},
  mounted () {

  },

  methods: {

    async initForm (type, placeOrgId) {
      this.$refs.ref_form.resetFields();

      this.formType = type
      if (placeOrgId) {
        this.placeOrgId = placeOrgId
        this.formData.placeOrgId = placeOrgId
        await this.loadFormData()
      }

    },

    //加载组织
    async loadFormData () {
      const url = '/gov/org/placeorg/detail'
      // const url = 'http://yapi.elinkservice.cn/mock/245/gov/org/placeorg/detail'
      let params = {
        placeOrgId: this.placeOrgId
      }

      const { data, code, msg } = await requestPost(url, params)

      if (code === 0) {
        this.formData = data

      } else {
        this.$message.error(msg)
      }
    },



    async handleComfirm () {

      this.btnDisable = true
      setTimeout(() => {
        this.btnDisable = false
      }, 10000)
      this.$refs['ref_form'].validate((valid, messageObj) => {
        if (!valid) {
          app.util.validateRule(messageObj)
          this.btnDisable = false
        } else {
          this.addPlace()
        }

      })


    },
    async addPlace () {
      // const regPhone = /^1(3|4|5|6|7|8|9)\d{9}$/; //手机号码
      // if (regPhone.test(this.formData.mobile) === false) {
      //   this.btnDisable = false
      //   this.$message({
      //     type: 'warning',
      //     message: '请输入正确的手机号码'
      //   })
      //   return false;
      // }

      let url = ''

      if (this.formType === 'edit') {
        url = '/gov/org/placeorg/edit'
        // url = 'http://yapi.elinkservice.cn/mock/245/gov/org/placeorg/edit'

      } else {
        url = "/gov/org/placeorg/add"
        // url = "http://yapi.elinkservice.cn/mock/245/gov/org/placeorg/add"
      }

      const { data, code, msg } = await requestPost(url, this.formData)

      if (code === 0) {
        this.$message({
          type: 'success',
          message: '操作成功'
        })
        this.resetData()
        this.$emit('dialogOk')
        this.btnDisable = false
      } else {
        this.btnDisable = false
        this.$message.error(msg)
      }

    },

    handleCancle () {

      this.resetData()
      this.$emit('dialogCancle')

    },

    resetData () {
      this.placeOrgId = ''
      this.formData = {
        gridId: '',
        ninePlaceVal: '',
        placeOrgName: '',
        address: '',
        scale: '',
        personInCharge: '',
        mobile: ''
      }
    },
    // 开启加载动画
    startLoading () {
      loading = Loading.service({
        lock: true, // 是否锁定
        text: '正在加载……', // 加载中需要显示的文字
        background: 'rgba(0,0,0,.7)' // 背景颜色
      })
    },
    // 结束加载动画
    endLoading () {
      // clearTimeout(timer);
      if (loading) {
        loading.close()
      }
    }
  },
  computed: {
    dataRule () {
      return {
        gridId: [
          { required: true, message: '所属网格不能为空', trigger: 'blur' }
        ],
        ninePlaceVal: [
          { required: true, message: '场所类型不能为空', trigger: 'blur' },
        ],
        placeOrgName: [
          { required: true, message: '场所名称不能为空', trigger: 'blur' }
        ],

        address: [
          { required: true, message: '场所地址不能为空', trigger: 'blur' }
        ],
        scale: [
          { required: true, message: '场所规模不能为空', trigger: 'blur' }
        ],
        personInCharge: [
          { required: true, message: '负责人不能为空', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '联系电话不能为空', trigger: 'blur' }
        ]
      }
    },

  },
  props: {

    gridList: {
      type: Array,
      default: []
    },
    scaleList: {
      type: Array,
      default: []
    },
    placeTypeList: {
      type: Array,
      default: []
    },
  }
}
</script>
<style scoped>
.item_width_1 {
  width: 500px;
}
.item_width_2 {
  width: 400px;
}
.item_width_3 {
  margin-left: 10px;
  width: 200px;
}
.item_width_4 {
  width: 200px;
}

.div_map {
  position: relative;
}
.div_searchmap {
  z-index: 5000;
  position: absolute;
  top: 5px;
  left: 5px;
}

.div_btn {
  display: flex;
  justify-content: flex-end;
}
.el-tabs {
  margin: 0 20px;
}
.el-upload__tip {
  color: rgb(155, 155, 155);
  margin: 0;
}
.form {
  margin-top: 30px;
}
</style>

<style lang=scss>
.el-dialog__body {
  padding: 0 10px 20px !important;
}
</style>
