<template>
  <div class="inline-16">
    <el-button size="small" type="text" @click="onShow">编 辑</el-button>
    <el-dialog
        :visible.sync="drawer"
        width="950px"
        @close="onClose"
    >
      <div>
        <el-row :gutter="20">
          <el-form :model="form" :rules="rules"  ref="form"  size="mini" label-position="left" label-width="90px">
            <el-col :span="20">
              <el-form-item prop="type"  label="档案名称:">
                <el-input v-model="form.type" placeholder="输入档案名称"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item  label="档案备注:">
                <el-input type="textarea" autosize v-model="form.remarks" placeholder="输入档案备注"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="20">
              <el-form-item  label="上传附件">
                <uploadFile ref="upload" style="margin-top:10px" :folderid="folderid" accept=".text,.doc,.xls" :bindData="{ownertable:'plm_technicalinfo',ownerid:form.plm_technicalinfoid,usetype:'default'}" @onSuccess="uploadSuccess"></uploadFile>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
      </div>
      <div>
        <file_table ref="list" :id="form.plm_technicalinfoid">
          <template v-slot:del="scope">
            <file_delete v-if="tool.checkAuth($route.name,'delete')" :data="scope.data" :type="'text'" @deleteSuccess="onSuccess"></file_delete>
          </template>
        </file_table>
      </div>

      <div class="dialog-footer">
        <el-button size="small" @click="drawer = false" class="normal-btn-width">取 消</el-button>
        <el-button size="small" type="primary" @click="onSubmit"   class="normal-btn-width btn-primary">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import uploadFile from '../../../components/upload/hw_obs_upload.vue'
import file_table from "../components/fileTable";
import file_delete from "../components/fileDel"
export default {
  props:['data'],
  name: "edit",
  data() {
    return {
      drawer:false,
      form:[],
      rules:{
        type: [
          { required: true, message: '档案名称不可为空', trigger: 'blur' },
        ],
      },
      folderid:JSON.parse(sessionStorage.getItem('folderid')).appfolderid,
      attachmentids:[]
    }
  },
  components:{
    uploadFile,
    file_table,
    file_delete
  },
  mounted() {

  },
  methods: {
    onShow(){
      this.drawer = true
      this.form = this.data
      console.log(this.form)
    },
    onClose(){
      this.drawer = false
    },
    async onSubmit(){
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "id": "20220926095703",
          "version":1,
          "content": {
            "plm_technicalinfoid":this.form.plm_technicalinfoid,
            "type":this.form.type,
            "remarks":this.form.remarks
          }
        })
        this.tool.showMessage(res,() => {
          this.drawer = false

          this.$emit('updateSuccess')
        })
      })
    },
    uploadSuccess(res){
      this.bindEnclosure(res.data.attachmentids)
      this.form.attinfos = JSON.parse(res.attinfos).data
      this.$refs['upload'].dialogUploadVisible = false

    },
    async bindEnclosure(attachmentids){
      const res = await this.$api.requested({
        "classname": "system.attachment.Attachment",
        "method": "createFileLink",
        "content": {
          "ownertable": "plm_technicalinfo",
          "ownerid": this.form.plm_technicalinfoid,
          "usetype": "default",
          "attachmentids": attachmentids
        }
      })
      this.tool.showMessage(res,() => {
        this.$refs.list.listData()
      })
    },
    onSuccess(){
      this.$refs.list.listData()
    }
  }
}
</script>

<style scoped>

</style>