<style lang="scss">
    
</style>
<template>
    <section>
        <p>资料管理  >  清单列表</p>
        <div class="filter">
            <el-form label-width="70px" :inline="true" size="small" label-position="left">
                <el-form-item label="名称">
                        <el-input clearable placeholder="请输入名称" v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item>
                       <el-button @click="form.page=1,getData()" type="primary" icon="el-icon-search">搜索</el-button>
                       <el-button icon="el-icon-plus" @click="dialogVisible=true"  type="primary">新增清单</el-button>
                </el-form-item>
            </el-form>
        </div>


        <el-table
                class="table"
                :data="list" height="64vh"
                border  v-loading="loading"
                style="width: 100%">
                <el-table-column
                type="selection" fixed="left"
                width="40">
                </el-table-column>
                <el-table-column
                prop="name" 
                label="名称">
                </el-table-column>
                <el-table-column
                prop="enterprise_name" 
                label="公司">
                </el-table-column>
                <el-table-column
                prop="ctime"  width="200"
                label="时间">
                </el-table-column>
                <el-table-column fixed="right"
                prop="zip"  width="170"
                label="操作">
                <template slot-scope="scope">
                    <el-button icon="el-icon-download" @click="download(scope.row.fileurl)" size="mini" type="warning">下载</el-button>
                    <el-button icon="el-icon-delete" @click="del(scope.row.id)" size="mini"  type="danger">删除</el-button>
                </template>
                </el-table-column>
        </el-table>
        <Page  ref="pageButton"  :total='total' @pageChange='gopage'/>
        <el-dialog
            title="新增资料" center
            :visible.sync="dialogVisible"
            width="500px">
            <el-form label-width="60px">
                <el-form-item label="标题">
                    <el-input clearable   v-model="doc.name"></el-input>
                </el-form-item>
                <el-form-item label="公司">
                            <el-select clearable filterable v-model="doc.enterprise_id" placeholder="请选择">
                                <el-option v-for="(item,index) in company" :key="index" :label="item.name" :value="item.id"></el-option>
                            </el-select>
                </el-form-item>
                <el-form-item label="附件">
                     <input @change="upload('fileurl')" id='fileurl' type="file">
                </el-form-item>
            </el-form>
        
            <span slot="footer" class="dialog-footer">
                <el-button size="small" @click="dialogVisible = false">取 消</el-button>
                <el-button size="small" type="primary" @click="add">确 定</el-button>
            </span>
        </el-dialog>
    </section>
</template>
<script>
import Page from '../../components/Page';
export default {
  components:{
        Page
  },
  data(){
    return{
      form:{name:'',page:1,page_size:20},
      activeName:"1",
      total:1,
      list:[{name:'2333'}],
      loading:false,
      dialogVisible:false,
      doc:{},
      company:[]
    }
  },
  methods:{
        gopage(size){
            if(size){
                this.form.page_size=size
            }
            this.form.page=this.$refs.pageButton.page
            this.getData()
        },
        getData(){
            var parm=this.form;
            this.loading=true
            this.$api.getBillList(parm).then(res=>{
                this.list=res.data.data.list
                this.total=res.data.data.total
                this.loading=false
            })
        },
        del(id){
                this.$confirm('确定删除吗', '提示', {
                        type: 'warning'
                    }).then(() => {
                    this.$api.deleteBill({id:id}).then((res)=>{
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            })
                            this.getData()
                    })
                })
        },
        upload(type){
            var file=document.getElementById(type).files;
            var data=new FormData();
                data.append("file",file[0])
            this.$api.uploadFile(data).then(res=>{
                if(res.data.code==0){
                        let form=this.doc;
                        this.$set(form,type,res.data.data.url)
                        this.$message({message: '上传成功！',type: 'success'});
                    }else{
                        this.$message.error(res.data.message);
                    }
            })
        },
        download(url){
            window.open(url);
        },
        add(){
            let parm=this.doc;
            let list=this.company;
            parm.enterprise_name=list.filter(item=>item.id==parm.enterprise_id)[0].name
            this.$api.saveBill(parm).then(res=>{
                    if(res.data.code==0){
                        this.$message({message: '添加成功！',type: 'success'});
                        this.getData();
                        this.dialogVisible=false;
                    }else{
                        this.$message.error(res.data.message);
                    }
            })
            
        },
  },
  created(){
      this.getData()
      this.$api.getEnterpriseAll().then(res=>{
            this.company=res.data.data;
      })
  }
}
</script>
