<template>
	<div class="hui-flex-box hui-flex hui-table">
		<div class="hui-content-insert">
			<el-button type="primary" size="small" @click="upload()">上传合同</el-button>
			<contract-upload ref="upload" v-show="false" :contractId="contractId" type="project" @reload="init">
			</contract-upload>
		</div>
		<div class="hui-flex-box">
			<el-table ref="elTable" :data="tableData" height="100%" v-loading="loading"
				@current-change="handleCurrentChange">
				<el-table-column width="35" v-if="type === 'filter'">
					<template slot-scope="scope">
						<el-radio class="radio" :label="scope.row" v-model="currentRow">{{""}}</el-radio>
					</template>
				</el-table-column>
				<el-table-column prop="name" label="合同名称">
				</el-table-column>
				<el-table-column prop="createDate" label="创建时间">
				</el-table-column>
				<el-table-column prop="updateDate" label="更新时间">
				</el-table-column>
				<el-table-column label="操作" width="180" align="center">
					<template slot-scope="scope">
						<div class="hui-table-operation">
							<span class="table-operation" @click="previewItem(scope.row)">预览</span>
							<span class="table-operation" @click="upload(scope.row.id)">更新</span>
							<span class="table-operation" @click="deleteItem(scope.row.id)">删除</span>
						</div>
					</template>
				</el-table-column>
				<template slot="empty">
					<el-empty description="暂无数据"></el-empty>
				</template>
			</el-table>
		</div>
		<el-dialog :close-on-click-modal="false" title="预览合同" :visible.sync="dialogVisible" width="900px"
			:append-to-body="true">
			<pdf-viewer v-if="dialogVisible" :list="contractList" type="preview">
			</pdf-viewer>
		</el-dialog>
	</div>
</template>

<script>
	import {
		getOrganizationContract,
		deleteOrganizationContract
	} from '@/api/finace'
	const contractUpload = () => import('@/components/work/finace/contract/contractUpload');
	const pdfViewer = () => import('@/components/common/pdfViewer');
	import config from '@/config';
	export default {
		props: ['type'],
		data() {
			return {
				tableData: [],
				loading: false,
				contractList: [],
				contractId: '',
				dialogVisible: false,
				currentRow: null
			}
		},
		mounted() {
			this.init();
		},
		methods: {
			init(type) {
				this.loading = true;
				getOrganizationContract(this.$store.getters.organization.id).then(res => {
					this.loading = false;
					if (res.state) {
						this.tableData = res.data;
						if (this.type === 'filter') this.currentRow = this.tableData[0];
					}
				})
			},
			handleCurrentChange(row) {
				this.currentRow = row;
			},
			upload(id) {
				this.contractId = id;
				this.$nextTick(() => {
					this.$refs.upload.reloadUpload();
				})
			},
			previewItem(item) {
				this.contractList = [{
					url: window.location.origin + '/' + config.baseURL + '/file/workarkContract/pdf/' + item.id
				}]
				this.dialogVisible = true;
			},
			deleteItem(id) {
				this.$confirm('确定要删除合同?', () => {
					deleteOrganizationContract(id).then(res => {
						if (res.state) {
							this.$message.success('操作成功');
							this.init();
						}
					})
				});
			}
		},
		components: {
			contractUpload,
			pdfViewer
		}
	}
</script>

<style lang="scss"></style>