<template>
  <div class="inline-24">
    <el-button size="small" type="text" @click="onShow">编 辑</el-button>
    <el-drawer
      title="编辑资料"
      :visible.sync="drawer"
      size="30%"
      direction="rtl"
      append-to-body>
      <div class="drawer__panel">
        <el-row :gutter="20">
          <el-form :model="form" :rules="rules"  ref="form"  size="mini" label-position="right" label-width="90px">
            <el-col :span="24">
              <el-form-item prop="type"  label="资料类别:">
                <el-select v-model="form.type" placeholder="请选择资料类别" style="width: 100%">
                  <el-option
                      v-for="item in options"
                      :key="item.value"
                      :label="item.value"
                      :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="资料说明:">
                <el-input type="textarea" rows="5" v-model="form.remarks" placeholder="输入资料说明"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item  label="上传附件:">
<!--                <uploadFile ref="upload"  :folderid="folderid" accept=".text,.doc,.xls" :bindData="{ownertable:'plm_technicalinfo',ownerid:form.plm_technicalinfoid,usetype:'default'}" @onSuccess="uploadSuccess"></uploadFile>-->
                <uploadFile ref="upload" :folderid="folderid"
                        :bindData="{ ownertable: 'plm_technicalinfo', ownerid: form.plm_technicalinfoid, usetype: 'default' }"
                        @onSuccess="uploadSuccess"></uploadFile>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
        <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="fixed__btn__panel">
          <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>
      </div>
    </el-drawer>
  </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:[],
      options: [],
      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)
      this.typeDetails()
    },
    onClose(){
      this.drawer = false
    },
    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')
        })
      })
    },
    /*资料类别明细*/
    async typeDetails(){
      const siteid = JSON.parse(sessionStorage.getItem('active_account')).siteid
      const res = await this.$api.requested({
        "classname": "sysmanage.develop.optiontype.optiontype",
        "method": "optiontypeselect",
        "content": {
          "pageNumber": 1,
          "pageSize": 20,
          "typename": "technicalinfotype",
          "parameter": {
            "siteid": siteid
          }
        }
      })
      console.log(res)
      this.options = res.data
    },
    uploadSuccess(res){
      console.log("输出")
      console.log(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>