<!--
  @description: 新增范本/编辑范本
  @Modifier: sunxinman
  @routerPath:
  @date: 2020/04/10-15:50
-->
<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="dialogData.dialog"
    :close-on-click-modal="false"
    :title="title"
    width="800px"
    custom-class="el-dialog__body-p0"
    top="calc((100vh - 611px) / 2)"
  >
    <section class="main">
      <!-- 表单 -->
      <el-form ref="formData" :model="formData" :rules="rules" label-width="160px" class="demo-ruleForm">
        <el-row>
          <el-col :span="24">
            <!-- 文件名称 -->
            <el-form-item label="文件名称" prop="templateName">
              <el-input
                v-model="formData.templateName"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <!-- 模板类型 -->
            <el-form-item label="模板类型" prop="templateType">
              <el-select v-model="formData.templateType" placeholder="请选择">
                <el-option
                  v-for="(item,index) in templateTypeList"
                  :key="index"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 专业类型 -->
            <el-form-item label="专业类型" prop="industryType">
              <el-select v-model="formData.industryType" placeholder="请选择">
                <el-option
                  v-for="(item,index) in projectClassificationList"
                  :key="index"
                  :label="item.typename"
                  :value="item.typecode"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <!-- 版本号 -->
            <el-form-item label="版本号" prop="versionNumber">
              <el-input
                v-model="formData.versionNumber"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 是否显示投标文件结构 -->
            <el-form-item label="是否显示投标文件结构" prop="isShowBiddingDocumentStructure">
              <el-radio-group v-model="formData.isShowBiddingDocumentStructure">
                <el-radio v-for="(item,index) in showDocList" :key="index" :label="item.typecode">
                  {{ item.typename }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <!-- 说明 -->
            <el-form-item label="说明" prop="remark">
              <el-input
                v-model="formData.remark"
                :autosize="{ minRows: 4, maxRows: 4 }"
                type="textarea"
                maxlength="1000"
                show-word-limit
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <!-- 附件 -->
            <el-form-item label="附件" prop="templateStatus">
              <upload
                ref="upload"
                :data="upLoadData"
                :file-list="fileList"
                :auto-delete="true"
                :auto-upload="true"
                :limit="1"
                :on-change="onChange"
                title=""
                file-ref="fileRef"
              />
            </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>
/* 公共参数mixin*/
import { templateLibrary } from './mixins/templateLibraryMixin'
import { templateGroupMixin } from './mixins/templateGroupMixin'
/* 常用方法 */
import { randomData } from '@/assets/js/utils/index'
/* 组件*/
import upload from '@comp/common/updownload/upload'// 文件上传
/* 接口 */
import { addOrUpdateTmplateLibrary, getTemplateLibraryById } from '@/api/common/tools.js'

export default {
  name: 'TemplateAddDialog',
  components: {
    upload
  },
  mixins: [templateLibrary, templateGroupMixin],
  props: {
    // 弹框展示状态
    dialogData: {
      type: Object,
      default: function() {

      }
    }
  },
  data() {
    return {
      title: '新增范本', // 弹框标题
      formData: {
        id: '',
        templateName: '', // 文件名称
        industryType: '01', // 专业类型
        isShowBiddingDocumentStructure: '00', // 是否显示投标文件结构 00否；01是
        templateType: '00', // 模板类型
        remark: '', // 说明
        versionNumber: '', // 版本号
        templateStatus: '00' // 模板状态 00未上传 01已上传
      },
      rules: {
        templateName: [{ required: true, message: '不能为空', trigger: 'blur' }], // 文件名称
        industryType: [{ required: true }] // 专业类型
      }
    }
  },
  mounted() {
    this.watchDialog()
  },
  methods: {
    // 获取详情
    getTemplateLibraryById(id) {
      getTemplateLibraryById(id).then(res => {
        this.formData = { ...res.data }
      })
    },
    // 提交
    ensure(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addOrUpdateTmplateLibrary(this.formData).then(() => {
            this.dialogData.dialog = false
            this.$emit('watchChild', true)
          })
        } else {
          return false
        }
      })
    },
    // 文件改变
    onChange(file, fileList) {
      if (fileList.length) {
        this.formData.templateStatus = '01' // 模板状态 01已上传
      } else {
        this.formData.templateStatus = '00' // 模板状态 00未上传
      }
    },
    // 监听dialog
    watchDialog() {
      if (this.dialogData.dialog) {
        // 弹框已打开
        if (this.dialogData.type === '01') { // 01编辑；00新增
          this.title = '编辑范本'
          this.getTemplateLibraryById(this.dialogData.id)
          this.upLoadData.bizId = this.dialogData.id
        } else {
          this.title = '新增范本'
          // if (this.formData.templateStatus === '01') { // 模板状态 01 已上传；00 未上传
          this.upLoadData.bizId = randomData()
          // } else {
          //   this.upLoadData.bizId = ''
          // }
          this.formData.id = this.upLoadData.bizId
        }
      }
    }
  }
}
</script>

<style scoped lang="less">

</style>
