<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="68px">
            <el-form-item label="项目名称" prop="projectName">
                <el-input v-model="queryParams.projectName" placeholder="请输入项目名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>

            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>
        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['abuwx:project:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                    v-hasPermi="['abuwx:project:edit']">修改</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"
                    @click="handleDelete" v-hasPermi="['abuwx:project:remove']">删除</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                    v-hasPermi="['abuwx:project:export']">导出</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table v-loading="loading" :data="projectList" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column label="项目名称" align="center" prop="projectName" />
            <el-table-column label="项目概况" align="center" prop="projectOverview" />
            <el-table-column label="附件上传" align="center" prop="remark" >
                <template slot-scope="scope">
                    <!-- <el-button size="mini" type="primary" v-if="scope.row.fileUrl"  @click="handleDownLoadFilecod(scope.row)" >下载</el-button> -->
                    <el-button    @click="$download.resource(scope.row.fileUrl,false)" type="text" v-if="scope.row.fileUrl">下载</el-button>
                    <span v-else>无附件</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['abuwx:project:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['abuwx:project:remove']">删除</el-button>
                    <el-button size="mini" type="text" @click="openerweima(scope.row)">生成二维码</el-button>
                </template>
            </el-table-column>
        </el-table>

        <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize" @pagination="getList" />
        <!-- 添加或修改微信用户对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="80px">
                <el-form-item label="项目名称" prop="projectName">
                    <el-input v-model="form.projectName" placeholder="请输入项目名称" />
                </el-form-item>
                <el-form-item label="项目概况" prop="projectOverview">
                    <el-input type="textarea" v-model="form.projectOverview" placeholder="请输入项目概况" />
                </el-form-item>
                <el-form-item label="附件" prop="avatar">
                    <!-- <image-upload v-model="form.avatar"/> -->
                    <upload v-model="form.fileUrl" :fileShow="true"></upload>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
        <!-- 二维码 -->
        <el-dialog :visible.sync="codeOpen" width="250px" append-to-body>
          <div id="printdata">
            <div>项目名称：<span>{{ this.titles }}</span></div>
            <div>
              <vue-qr ref="qrCode" :text="texturl" :logoSrc="logoSrc" :color-dark="randomColor"></vue-qr>
            </div>
          </div>

            <div slot="footer">
                <el-button type="primary" @click="() => { codeOpen = false }">确定</el-button>
              <el-button type="text" @click="handlePrint" v-print="'#printdata'" >打印二维码</el-button>

            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listProject, getProject, delProject, addProject, updateProject } from "@/api/abuwx/project";

import upload from '@/components/FileUpload/index.vue'
import VueQr from 'vue-qr'
// import { Base64 } from "js-base64";
export default {
    name: "Project",
    dicts: ['sys_user_sex'],
    components: {
        upload,
        VueQr,
    },
    data() {
        return {
          printContent: {
            id: "printdata", // 打印的区域
            preview: false, // 预览工具是否启用
            previewTitle: "", // 预览页面的标题
            popTitle: "", // 打印页面的页眉
            // extraCss: "", //css 尽量不要在此设置样式
            previewBeforeOpenCallback(vue) {
              console.log("正在加载预览窗口");
            },
            previewOpenCallback(vue) {
              console.log("已经加载完预览窗口");
            },
            clickMounted: (vue) => {
              console.log("触发点击打印回调");

              vue.isShowPrint = true; //弹框显示条码
            },
            beforeOpenCallback(vue) {
              console.log("打开之前", vue.barcodeNum);
            },
            openCallback(vue) {
              console.log(vue);
              vue.isShowPrint = false; // 关闭条码显示弹框
              console.log("执行了打印", vue.barcodeNum);
            },
          },
          fontSizeText: "16px",
          lineHeightText: "16px",

            downLoadUrl :process.env.VUE_APP_BASE_API,
            // 遮罩层
            loading: true,
            // 二维码内容
            codeOpen: false,
            titles: '',
            logoSrc: '',
            randomColor: 'black',
            texturl: ``,
            qrCodeUrl: '', // 存放生成的二维码url
            // 选中数组
            ids: [],
            // 非单个禁用
            single: true,
            // 非多个禁用
            multiple: true,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 微信用户表格数据
            projectList: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                projectName: null,
                projectOverview: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
            }
        };
    },
    created() {
        this.getList();
    },
    methods: {
        /** 查询微信用户列表 */
        getList() {
            this.loading = true;
            listProject(this.queryParams).then(response => {
                this.projectList = response.rows;
                this.total = response.total;
                this.loading = false;
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        //二维码
        openerweima(row) {
            this.titles=row.projectName
            this.texturl = `https://xiaofangweibao.jsxinyuansu.com/pages/index?id=${row.id}`
            this.codeOpen = true
        },

        // 表单重置
        reset() {
            this.form = {
                id: null,
                projectOverview: null,
                avatar: null,
                projectName: null,
                gender: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null,
                remark: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.id)
            this.single = selection.length !== 1
            this.multiple = !selection.length
        },
        /** 新增按钮操作 */
        handleAdd() {
            this.reset();
            this.open = true;
            this.title = "添加项目";
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            const id = row.id || this.ids
            getProject(id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改项目";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateProject(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addProject(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            const ids = row.id || this.ids;
            this.$modal.confirm('是否确认删除数据项？').then(function () {
                return delProject(ids);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        },
        /** 导出按钮操作 */
        handleExport() {
            this.download('abuwx/project/export', {
                ...this.queryParams
            }, `project_${new Date().getTime()}.xlsx`)
        },
      handlePrint(qrCode,name) {
        // refName ref名称   name下载文件名称
          const iconUrl = this.$refs[qrCode].$el.src //currentSrc
        console.log(this.$refs[qrCode],'=====================')
          const a = document.createElement('a')
          const event = new MouseEvent('click')
          a.download = name
          a.href = iconUrl
          a.dispatchEvent(event)

      },
    }
};
</script>
<style scoped lang="scss">
#printdata {
  text-align: center;
  font-weight: 700;
  font-size: 20px;
  .text {
    margin-bottom: 20px;
  }
}
</style>
