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

    <i-form label-position="right" :label-width="100" ref="dictGroupForm" :model="bean" :rules="rules" inline>
      <Row>
        <i-col :span="12" v-show="false" >
          <Form-item label="上级id" class="ivu-form-item-search">
            <Input v-model="bean.parentId" style="width: 243px" disabled></Input>
          </Form-item>
        </i-col>
        <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="code" class="ivu-form-item-search">
            <Input v-model="bean.code" style="width: 243px" :disabled="flag=='edit'"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="类型名称" prop="name" class="ivu-form-item-search">
            <Input v-model="bean.name" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="排序" prop="sort" class="ivu-form-item-search">
            <InputNumber v-model="bean.sort" style="width: 243px" :disabled="disabled" ></InputNumber>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="状态" prop="state" class="ivu-form-item-search">
            <Select v-model="bean.state" style="width: 243px" :disabled="disabled">
              <Option :value="1" :key="1">正常</Option>
              <Option :value="0" :key="0">禁用</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('dictGroupForm')" v-if="showSaveBtn">保存</Button>
    </div>
  </Modal>
</template>

<script>
  import { addDictGroup, editDictGroup } from '../../../../api/base/dictGroup-api'

  let $this
  export default {
    name: 'dictGroupModal',
    components: {},
    props: {
      show: Boolean,
      value: Object
    },
    data () {
      return {
        showModalDictGroup: false,
        bean: {},
        initBean: {state: 1, sort: 1 },
        flag: '',
        saveLoading: false,
        modalTitle: '',
        disabled: false,
        showSaveBtn: true,
        rules: {
          parentId: [
            { required: true, message: '上级id不能为空!', trigger: 'blur' },
          ],
          parentName: [
            { required: true, message: '上级名称不能为空!', trigger: 'blur' },
          ],
          fullPath: [
            { required: true, message: '全路径ID不能为空!', trigger: 'blur' },
          ],
          fullPathName: [
            { required: true, message: '全路径名称不能为空!', trigger: 'blur' },
          ],
          code: [
            { required: true, message: '类型编码不能为空!', trigger: 'blur' },
          ],
          name: [
            { required: true, message: '类型名称不能为空!', trigger: 'blur' },
          ],
          sort: [
            { type: 'number', required: true, message: '排序不能为空!', trigger: 'blur' },
          ],
          state: [
            { type: 'number', required: true, message: '状态不能为空!', trigger: 'change' },
          ],
          remark: [
            { 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.showModalDictGroup = false
          if (this.flag == 'edit') {
            let params = this.bean
            delete params['createTime']
            delete params['modifyTime']
            //执行修改操作
            editDictGroup(params).then(data => {
              if (!data.status) {
                this.$Message.error(data.error.message)
                this.saveLoading = false
                console.log(data)
                return false
              }
              this.$Message.success('操作成功！')
              this.saveLoading = false
              this.$emit('update:show', false)
              this.$emit('reload-data')
            }).catch(err => {
              this.$Message.error('操作失败！')
              this.saveLoading = false
              console.log(err)
            })
          } else if (this.flag == 'add') {
            delete this.bean['createTime']
            delete this.bean['modifyTime']
            //执行添加操作
            addDictGroup(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.saveLoading = false
              this.$emit('update:show', false)
              this.$emit('reload-data')
            }).catch(err => {
              this.$Message.error('操作失败！')
              this.saveLoading = false
              console.log(err)
            })
          }
        })

      },
      cancel () {
        this.showModalDictGroup = false
        this.$emit('update:show', false)
      },
    },
    mounted () {
      $this = this
      this.bean = this.initBean
    },
    watch: {
      show: {
        immediate: true,
        handler: function () {
          this.showModalDictGroup = 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>
