<!--
  @description: 系统共享文档管理新增/编辑
  @Modifier: zhuyanchao
  @routerPath:
  @date: 2020-7-11-15:23
-->
<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="dialogData.dialog"
    :close-on-click-modal="false"
    :title="title"
    width="700px"
    custom-class="el-dialog_table"
  >
    <section class="main">
      <!-- 表单 -->
      <el-form ref="formData" :model="formData" label-width="96px" class="demo-ruleForm">
        <el-row>
          <el-col :span="24">
            <!-- 文档类型 -->
            <el-form-item label="文档类型" prop="docType">
              <el-select v-model="formData.docType" style="width: 100%;" placeholder="请选择">
                <el-option
                        v-for="(item,index) in docTypeList"
                        :key="index"
                        :label="item.typename"
                        :value="item.typecode"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <!-- 角色类型 -->
            <el-form-item label="角色类型" prop="roleType">
              <el-select v-model="formData.roleType" style="width: 100%;" placeholder="请选择">
                <el-option
                  v-for="(item,index) in docRoleTypeList"
                  :key="index"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <!-- 文档描述 -->
            <el-form-item label="文档描述" prop="memo">
              <el-input
                v-model="formData.memo"
                :autosize="{ minRows: 4, maxRows: 4 }"
                type="textarea"
                maxlength="500"
                show-word-limit
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="共享文档" prop="file">
            <upload
                    ref="upload"
                    :data="upLoadData"
                    :on-change="systemZipDocChange"
                    :auto-delete="true"
                    :auto-upload="true"
                    title=""
                    file-ref="fileRef"
                    :tip-text="'注：请上传共享文档zip包'"
            />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </section>
    <!--底部操作按钮-->
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="ensure('formData')">提 交</el-button>
      <el-button @click="dialogData.dialog = false">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* 接口*/
import { saveOrUpdateSystemDocZip, getSystemconfigById } from '@/api/common/systemConfigCenter'// 系统配置
import { systemZipDocMixin } from './mixins/systemZipDocMixin'
import Cookies from 'js-cookie'
import upload from '@comp/common/updownload/upload'
export default {
  name: 'SystemZipDocAddDialog',
  mixins: [systemZipDocMixin],
  props: {
    // 弹框展示状态
    dialogData: {
      type: Object,
      default: function() {
      }
    }
  },
  components: {
    upload
  },
  data() {
    return {
      importLoading: false,
      action: `/bidprocurement/common-tools/attachmentDoc/uploadSystemDocZip?fileId=`,
      importHeaders: {}, // cookit
      upLoadData: {
        fileType: 'system_doc_zip_attachment', // 文件类型
        bizId: '', // 业务主键Id
        fileId: ''
      },
      title: '新增共享文档', // 弹框标题
      formData: {
        id: '',
        fileId: '',
        createUserName: '',
        memo: '', // 备注
        roleType: ''
      }
    }
  },
  created() {
    this.importHeaders['X-AUTH-TOKEN'] = Cookies.get('X-AUTH-TOKEN')
  },
  mounted() {
    this.watchDialog()
  },
  methods: {
    // 监听dialog
    watchDialog() {
      if (this.dialogData.dialog) {
        // 弹框已打开
        if (this.dialogData.id !== null) {
          this.getSystemconfigById(this.dialogData.id)
          this.title = '更新共享文档'
        } else {
          this.title = '新增共享文档'
        }
      }
    },
    // 获取详情
    getSystemconfigById(id) {
      getSystemconfigById(id).then(res => {
        if (res.header.ret === '0') {
          this.formData = { ...res.data }
        }
      })
    },
    systemZipDocChange(file, fileList) {
      if (fileList.length > 0 && file.fileData != null) { // 上传了
        this.formData.fileId = file.fileData.fileId// 01 是
      }
    },
    // 提交
    ensure(formName) {
      if (this.importLoading === true) {
        this.$message.error('未完成上传')
        return false
      }
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveOrUpdateSystemDocZip(this.formData).then(res => {
            if (res.header.ret === '0') {
              this.dialogData.dialog = false
              this.$emit('watchChild', true)
            }
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
