<template>
  <Modal v-model="showModalArea" :mask-closable="false" :closable="false" :title="modalTitle" :value="value" width="820"
         v-draggable="options">

    <i-form label-position="right" :label-width="100" ref="areaForm" :model="bean" :rules="rules" inline>
      <Row>
        <i-col :span="12">
          <Form-item label="父区域" class="ivu-form-item-search">
            <Input v-model="bean.parentName" style="width: 243px" disabled></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="区划代码" prop="areaCode" class="ivu-form-item-search">
            <Input v-model="bean.areaCode" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="区域名称" prop="areaName" class="ivu-form-item-search">
            <Input v-model="bean.areaName" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="居委会" prop="abName" class="ivu-form-item-search">
            <Input v-model="bean.abName" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="拼音" class="ivu-form-item-search">
            <Input v-model="bean.pinYin" style="width: 243px" placeholder="自动生成" disabled></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="首字母缩写" class="ivu-form-item-search">
            <Input v-model="bean.abpy" style="width: 243px" placeholder="自动生成" disabled></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="区域级别" prop="areaLevel" class="ivu-form-item-search">
            <Select v-model="bean.areaLevel" style="width: 243px" :disabled="disabled">
              <Option v-for="item in areaLevels" :value="item.value" :key="item.id" :tag="item.value" >{{ item.name }}</Option>
            </Select>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="备注" class="ivu-form-item-search">
            <Input v-model="bean.remark" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
      </Row>
    </i-form>
    <div slot="footer">
      <Button @click="cancel">关闭</Button>
      <Button type="primary" :loading="saveLoading" @click="ok('areaForm')" v-if="showSaveBtn">保存</Button>
    </div>
  </Modal>
</template>

<script>
  import { addArea, editArea } from '../../../../api/base/area-api'

  let $this
  export default {
    name: 'areaModal',
    components: {},
    props: {
      show: Boolean,
      value: Object
    },
    data () {
      return {
        areaLevels: [],
        showModalArea: false,
        bean: {},
        initBean: {},
        flag: '',
        saveLoading: false,
        modalTitle: '',
        disabled: false,
        showSaveBtn: true,
        rules: {
          areaCode: [
            { required: true, message: '区划代码不能为空!', trigger: 'blur' },
          ],
          areaName: [
            { required: true, message: '区域名称不能为空!', trigger: 'blur' },
          ],
          abName: [
            { required: true, message: '居委会不能为空!', trigger: 'blur' },
          ],
          areaLevel: [
            { required: true, message: '区域级别不能为空!', trigger: 'blur' },
          ],
        },
        options: {
          trigger: '.ivu-modal-header',
          body: '.ivu-modal',
          recover: true
        }
      }
    }, methods: {
      ok (name) {
        this.$refs[name].validate((valid) => {
          if (!valid) {
            return
          }//表单校验失败则返回
          this.saveLoading = true
          //this.showModalArea = false;
          if (this.flag == 'edit') {
            let params = this.bean
            delete params['createTime']
            delete params['modifyTime']
            //执行修改操作
            editArea(params).then(data => {
              if (!data.status) {
                this.$Message.error(data.error.message)
                this.saveLoading = false
                console.log(data)
                return false
              }
              this.$Message.success('操作成功！')
              this.closeAndRefresh()
            }).catch(err => {
              this.$Message.error('操作失败！')
              this.saveLoading = false
              console.log(err)
            })
          } else if (this.flag == 'add') {
            delete this.bean['createTime']
            delete this.bean['modifyTime']
            //执行添加操作
            addArea(this.bean).then(data => {
              if (!data.status) {
                this.$Message.error(data.error.message)
                this.saveLoading = false
                console.log(data)
                return false
              }
              this.$Message.success('操作成功！')
              this.closeAndRefresh()
            }).catch(err => {
              this.$Message.error('操作失败！')
              this.saveLoading = false
              console.log(err)
            })
          }
        })

      },
      cancel () {
        this.showModalArea = false
        this.$emit('update:show', false)
        //重置校验结果
        this.$refs['areaForm'].resetFields()
      },
      closeAndRefresh () {
        this.saveLoading = false
        this.cancel()
        this.$emit('reload-data')
      },
    },
    mounted () {
      $this = this
      this.bean = this.initBean
      //初始化区划级别字典集合
      this.$dictUtil.getDict('area_level',(data)=>{
        this.areaLevels = data;
      });
    },
    watch: {
      show: {
        immediate: true,
        handler: function () {
          this.showModalArea = this.show
        }
      },
      value: {
        immediate: true,
        handler: function () {
          //赋值
          this.bean = this.value.bean == null ? Object.assign({}, this.initBean) : JSON.parse(JSON.stringify(this.value.bean))
          this.flag = this.value.flag
          this.modalTitle = this.value.title
          //如果时查看的功能，要将所有input框只读，并且去掉保存按钮
          if (this.flag == 'view') {
            this.disabled = true
            this.showSaveBtn = false
          } else {
            this.disabled = false
            this.showSaveBtn = true
          }
        }
      }
    }

  }
</script>

<style scoped>
  .ivu-form-item-search {
    margin-bottom: 21px;
  }
</style>
