<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="tenantId">
        <el-select
          v-model="queryParams.param.tenantId"
          placeholder="请选择"
          clearable
          filterable
        >
          <el-option
            v-for="(item, index) in tenantsListData"
            :label="item.name"
            :value="item.id"
            :key="index"
          />
        </el-select>
      </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"
          >新增</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col> -->
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="listDat"
      @selection-change="handleSelectionChange"
      max-height="600"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column
        label="二维码"
        align="center"
        prop="name"
        min-width="120"
      >
        <template slot-scope="scope">
          <img
            :src="qzUrl + scope.row.url"
            alt=""
            width="100"
            height="100"
            @click="Original(scope.row.url)"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="组织"
        align="center"
        prop="tenantName"
        min-width="100"
      />

      <el-table-column label="创建人/创建时间" align="center" min-width="140">
        <template slot-scope="scope">
          <div>{{ scope.row.createBy }}</div>
          <span>{{
            parseTime(scope.row.createTime, "{y}-{m}-{d} {h}:{i}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" align="center" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-download"
            @click="handleExport(scope.row)"
            >导出
          </el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(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
      class="popup"
      :title="title"
      :visible.sync="open"
      width="780px"
      append-to-body
    >
      <el-form
        class="formStep"
        ref="form"
        :model="form"
        :rules="rules"
        label-width="60px"
      >
        <el-form-item label="组织" prop="tenantId">
          <el-select
            v-model="form.tenantId"
            placeholder="请选择"
            clearable
            filterable
          >
            <el-option
              v-for="(item, index) in tenantsListData"
              :label="item.name"
              :value="item.id"
              :key="index"
            />
          </el-select>
        </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-image
      :preview-src-list="imgUrl"
      ref="preview"
      style="display: none"
    ></el-image>
  </div>
</template>

<script>
import {
  queryScreenList,
  addScreen,
  deleteScreen,
  exportQr,
} from "@/api/screening.js";
import { tenantsList } from "@/api/member";
export default {
  name: "Notice",
  dicts: ["sys_notice_status", "sys_notice_type"],
  data() {
    return {
      qzUrl: process.env.VUE_APP_API_QZURL, // 二维码路径
      imgUrl: [],
      tenantsListData: [], // 组织列表
      loading: false, // 遮罩层
      ids: [], // 选中数组
      single: true, // 非单个禁用
      multiple: true, // 非多个禁用
      showSearch: true, // 显示搜索条件
      total: 0, // 总条数
      listDat: [], // 公告表格数据
      title: "", // 弹出层标题
      open: false, // 是否显示弹出层
      importOpen: false, // 导入弹窗
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        param: {
          tenantId: "", //建档组织
        },
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tenantId: [
          {
            required: true,
            message: "组织不能为空",
            trigger: "blur",
          },
        ],
      },
    };
  },
  created() {
    this.getList(); // 列表
    this.getTenantsList(); // 组织列表
  },
  methods: {
    Original(_url) {
      this.imgUrl = [];
      if (_url) {
        this.imgUrl.push(this.qzUrl + _url);
      }
      this.$refs.preview.clickHandler();
    },
    // 组织列表
    getTenantsList() {
      tenantsList({
        pageNum: -1,
        param: {},
      }).then((res) => {
        this.tenantsListData = res.data.list;
      });
    },

    /** 查询公告列表 */
    getList() {
      this.loading = true;
      queryScreenList(this.queryParams).then((res) => {
        this.listDat = res.data.list;
        this.total = res.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.param = {
        tenantId: "", //关键字
      };
      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 = "新增筛查二维码";
    },

    /** 提交按钮 */
    submitForm: function () {
      let path = process.env.VUE_APP_H5_URL;
      this.$refs["form"].validate((valid) => {
        if (valid) {
          let data = JSON.parse(JSON.stringify(this.form));
          let tenantName =
            this.tenantsListData.filter((item) => item.id == data.tenantId)[0]
              ?.name || "";
          data.path = `${path}?tenantId=${data.tenantId}&tenantName=${tenantName}`;
          if (data.id != undefined) {
            addScreen(data).then((response) => {
              this.$modal.msgSuccess("二维码已重新生成，请及时更换");
              this.open = false;
              this.getList();
            });
          } else {
            addScreen(data).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const idList = row.id ? [row.id] : this.ids;
      this.$modal
        .confirm("是否确认删除当前选择的数据？")
        .then(function () {
          return deleteScreen({
            idList: idList,
          });
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport(row) {
      let data = {
        tenantId: row.tenantId,
        url: row.url,
      };
      exportQr(data).then((res) => {
        window.open(`${process.env.VUE_APP_API_QZURL}${res.data}`);
      });
    },
  },
};
</script>
<style scoped src="@/assets/styles/common.css"></style>
<style scoped>
.form-item-age {
  display: flex;
  align-items: center;
}

.form-item-age span {
  margin: 0 10px;
}

.form-item-age >>> .el-input {
  width: 100px;
}
</style>
<!-- >>> .el-input__inner {
  padding: 0 15px !important;
} -->
