<template>
  <div>
    <el-button size="small" type="primary" @click="onShow" >新增产品技术资料</el-button>
    <el-drawer
      title="新增资料"
      :visible.sync="drawer"
      size="30%"
      direction="rtl"
      append-to-body
      @close="onCancel"
      >
      <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:plm_technicalinfoid,usetype:'default'}" @onSuccess="uploadSuccess"></uploadFile>
              </el-form-item>
            </el-col>
          </el-form>
        </el-row>
        <div>
          <file_table ref="list" :id="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="onCancel" 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 {
  name: "add",
  data() {
    return {
      drawer:false,
      plm_technicalinfoid:'',
      form:{
        type:'',
        remarks:'',
        attinfos:[]
      },
      options: [],
      rules:{
        type: [
          { required: true, message: '资料类别不可为空', trigger: 'change' },
        ],
      },
      folderid:JSON.parse(sessionStorage.getItem('folderid')).appfolderid,
      attachmentids:[],
      linksids:[]
    }
  },
  components:{
    uploadFile,
    file_table,
    file_delete
  },
  methods:{
    onShow() {
      this.drawer = true
      this.typeDetails();
      this.plm_technicalinfoid = '0'
    },
    onClose() {
      if (this.linksids.length > 0){
        this.batchDeletion()
      }
      /*this.$refs.form.resetFields()*/
      /*this.plm_technicalinfoid = ''
      this.form = {
        type:'',
        remarks:'',
        attinfos:[]
      }*/
    },
    onSubmit(){
      this.$refs['form'].validate(async (valid) => {
        if (!valid) return false
        const res = await this.$api.requested({
          "id": "20220926095703",
          "version":1,
          "content": {
            "plm_technicalinfoid":0,
            "type":this.form.type,
            "remarks":this.form.remarks
          }
        })
        this.tool.showMessage(res,() => {
          console.log("输出创建id")
          console.log(res)
          this.batchBinding(res.data)
          this.drawer = false
          this.$refs.form.resetFields()
          this.plm_technicalinfoid = ''
          this.form = {
            type:'',
            remarks:'',
            attinfos:[]
          }
          this.$emit('onSuccess')
        })
      })
    },
    onCancel(){
      this.drawer = false
      this.$refs.form.resetFields()
      this.plm_technicalinfoid = ''
      this.form = {
        type:'',
        remarks:'',
        attinfos:[]
      }
    },
    /*资料类别明细*/
    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("输出")
      if(typeof res.attinfos == 'string') {
         this.$refs.list.listData()
      } else {
        console.log(res)
        this.bindEnclosure(res.data.attachmentids)
        this.form.attinfos = this.form.attinfos.push(res.data.attachmentids)
        this.$refs['upload'].dialogUploadVisible = false
      }
      
    },
    async bindEnclosure(attachmentids){
      console.log(attachmentids)
      console.log(this.attachmentids)
      const res = await this.$api.requested({
        "classname": "system.attachment.Attachment",
        "method": "createFileLink",
        "content": {
          "ownertable": "plm_technicalinfo",
          "ownerid": 0,
          "usetype": "default",
          "attachmentids": attachmentids
        }
      })
      this.tool.showMessage(res,() => {
        console.log("附件绑定")
        console.log(res)
        const attachmentid = []
        const linksid=[]
        attachmentid[0] = res.data[0].attachmentid
        linksid[0] = res.data[0].linksid
        if (this.attachmentids.length === 0){
          this.attachmentids[0]= res.data[0].attachmentid
          this.linksids[0] = res.data[0].linksid
        }else {
          this.attachmentids.push(...attachmentid)
          this.linksids.push(...linksid)
        }
        console.log(this.attachmentids)
        console.log(this.linksids)
        this.$refs.list.listData()
        console.log('触发');
        
      })
    },
    /*批量绑定*/
    async batchBinding(id){
      const res = await this.$api.requested({
        "classname": "system.attachment.Attachment",
        "method": "createFileLink",
        "content": {
          "ownertable": "plm_technicalinfo",
          "ownerid": id,
          "usetype": "default",
          "attachmentids": this.attachmentids
        }
      })
      this.batchDeletion()
    },
    /*批量删除*/
    async batchDeletion(){
      const res = await this.$api.requested({
        "classname": "system.attachment.Attachment",
        "method": "deleteFileLink",
        "content": {
          "linksids": this.linksids
        }
      })
      console.log("删除")
      console.log(res)
      this.$refs.form.resetFields()
      this.plm_technicalinfoid = ''
      this.form = {
        type:'',
        remarks:'',
        attinfos:[]
      }
      this.$refs.list.listData()
      this.$emit('onSuccess')
    },
    onSuccess(){
      this.$refs.list.listData()
    }
  }

}
</script>

<style scoped>

</style>