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

    <i-form label-position="right" :label-width="100" ref="attachmentForm" :model="bean" :rules="rules" inline>
      <Row>
        <!--<i-col :span="12">
          <Form-item label="主键ID" prop="id" class="ivu-form-item-search">
            <Input v-model="bean.id" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="外键id" prop="fid" class="ivu-form-item-search">
            <Input v-model="bean.fid" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>-->
        <i-col :span="12">
          <Form-item label="模块名称" prop="moduleName" class="ivu-form-item-search">
            <Input v-model="bean.moduleName" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="业务名称" prop="busName" class="ivu-form-item-search">
            <Input v-model="bean.busName" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="原文件名" prop="oldName" class="ivu-form-item-search">
            <Input v-model="bean.oldName" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="新文件名" prop="newName" class="ivu-form-item-search">
            <Input v-model="bean.newName" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="文件类型 字典或枚举" prop="fileType" class="ivu-form-item-search">
            <Input v-model="bean.fileType" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="储存类型 local--本地存储 distributed--分布式" prop="saveType" class="ivu-form-item-search">
            <Input v-model="bean.saveType" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="后缀" prop="suffix" class="ivu-form-item-search">
            <Input v-model="bean.suffix" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="文件大小" prop="fileSize" class="ivu-form-item-search">
            <Input v-model="bean.fileSize" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="组----只在分布式文件存储时有效" prop="groupName" class="ivu-form-item-search">
            <Input v-model="bean.groupName" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="路径" prop="path" class="ivu-form-item-search">
            <Input v-model="bean.path" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="全路径" prop="fullPath" class="ivu-form-item-search">
            <Input v-model="bean.fullPath" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="能否下载 0-否 1-能" prop="canDown" class="ivu-form-item-search">
            <Input v-model="bean.canDown" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="上传时间" prop="uploadTime" class="ivu-form-item-search">
            <Input v-model="bean.uploadTime" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="来源地址" prop="sourceAddress" class="ivu-form-item-search">
            <Input v-model="bean.sourceAddress" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="状态  1--正常 0--无效" prop="state" class="ivu-form-item-search">
            <Input v-model="bean.state" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="备注" prop="remark" class="ivu-form-item-search">
            <Input v-model="bean.remark" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="是否删除 0-否 1-是" prop="deleteTag" class="ivu-form-item-search">
            <Input v-model="bean.deleteTag" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="创建人" prop="createBy" class="ivu-form-item-search">
            <Input v-model="bean.createBy" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="创建人姓名" prop="createByName" class="ivu-form-item-search">
            <Input v-model="bean.createByName" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="创建时间" prop="createTime" class="ivu-form-item-search">
            <Input v-model="bean.createTime" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="修改人" prop="modifyBy" class="ivu-form-item-search">
            <Input v-model="bean.modifyBy" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="修改人姓名" prop="modifyByName" class="ivu-form-item-search">
            <Input v-model="bean.modifyByName" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
        <i-col :span="12">
          <Form-item label="修改时间" prop="modifyTime" class="ivu-form-item-search">
            <Input v-model="bean.modifyTime" style="width: 243px" :disabled="disabled"></Input>
          </Form-item>
        </i-col>
      </Row>
    </i-form>
    <template #footer>
    <div>
      <Button @click="cancel">关闭</Button>
      <Button type="primary" :loading="saveLoading" @click="ok('attachmentForm')" v-if="showSaveBtn">保存</Button>
    </div>
    </template>
  </Modal>
</template>

<script>
  import { addAttachment, editAttachment } from '../../../../api/base/attachment-api'

  let $this
  export default {
    name: 'attachmentModal',
    components: {},
    props: {
      show: Boolean,
      value: Object
    },
    data () {
      return {
        showModalAttachment: false,
        bean: {},
        initBean: {},
        flag: '',
        saveLoading: false,
        modalTitle: '',
        disabled: false,
        showSaveBtn: true,
        rules: {
          id: [
            { required: true, message: '主键ID不能为空!', trigger: 'blur' },
          ],
          fid: [
            { required: true, message: '外键id不能为空!', trigger: 'blur' },
          ],
          moduleName: [
            { required: true, message: '模块名称不能为空!', trigger: 'blur' },
          ],
          busName: [
            { required: true, message: '业务名称不能为空!', trigger: 'blur' },
          ],
          oldName: [
            { required: true, message: '原文件名不能为空!', trigger: 'blur' },
          ],
          newName: [
            { required: true, message: '新文件名不能为空!', trigger: 'blur' },
          ],
          fileType: [
            { required: true, message: '文件类型 字典或枚举不能为空!', trigger: 'blur' },
          ],
          saveType: [
            { required: true, message: '储存类型 local--本地存储 distributed--分布式不能为空!', trigger: 'blur' },
          ],
          suffix: [
            { required: true, message: '后缀不能为空!', trigger: 'blur' },
          ],
          fileSize: [
            { required: true, message: '文件大小不能为空!', trigger: 'blur' },
          ],
          groupName: [
            { required: true, message: '组----只在分布式文件存储时有效不能为空!', trigger: 'blur' },
          ],
          path: [
            { required: true, message: '路径不能为空!', trigger: 'blur' },
          ],
          fullPath: [
            { required: true, message: '全路径不能为空!', trigger: 'blur' },
          ],
          canDown: [
            { required: true, message: '能否下载 0-否 1-能不能为空!', trigger: 'blur' },
          ],
          uploadTime: [
            { required: true, message: '上传时间不能为空!', trigger: 'blur' },
          ],
          sourceAddress: [
            { required: true, message: '来源地址不能为空!', trigger: 'blur' },
          ],
          state: [
            { required: true, message: '状态  1--正常 0--无效不能为空!', trigger: 'blur' },
          ],
          remark: [
            { required: true, message: '备注不能为空!', trigger: 'blur' },
          ],
          deleteTag: [
            { required: true, message: '是否删除 0-否 1-是不能为空!', trigger: 'blur' },
          ],
          createBy: [
            { required: true, message: '创建人不能为空!', trigger: 'blur' },
          ],
          createByName: [
            { required: true, message: '创建人姓名不能为空!', trigger: 'blur' },
          ],
          createTime: [
            { required: true, message: '创建时间不能为空!', trigger: 'blur' },
          ],
          modifyBy: [
            { required: true, message: '修改人不能为空!', trigger: 'blur' },
          ],
          modifyByName: [
            { required: true, message: '修改人姓名不能为空!', trigger: 'blur' },
          ],
          modifyTime: [
            { 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.showModalAttachment = false;
          if (this.flag == 'edit') {
            let parmas = this.bean
            delete parmas['createTime']
            delete parmas['modifyTime']
            //执行修改操作
            editAttachment(parmas).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']
            //执行添加操作
            addAttachment(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.showModalAttachment = false
        this.$emit('update:show', false)
        //重置校验结果
        this.$refs['attachmentForm'].resetFields()
      },
      closeAndRefresh () {
        this.saveLoading = false
        this.cancel()
        this.$emit('reload-data')
      },
    },
    mounted () {
      $this = this
      this.bean = this.initBean
    },
    watch: {
      show: {
        immediate: true,
        handler: function () {
          this.showModalAttachment = 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>
