<template>
  <div class="app-container">
    <el-table border v-loading="loading" :data="postList">
      <el-table-column label="序号" align="center" type="index" width="80" />
      <el-table-column label="岗检报告编号" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.reportNo || "--" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="岗检报告名称" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.reportName || "--" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完整性问题" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.reportName || "--" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="适宜性问题" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.reportName || "--" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="有效性问题"
        align="center"
        prop="problemAttribute"
      >
        <template slot-scope="{ row }">
          <span>{{ row.reportName || "--" }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="{ row }">
          <span :style="{ color: statusColor[row.status] }">{{
            status[row.status]
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生成时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.createTime || "--" }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            v-if="scope.row.status === 1"
            @click="handleCheck('edit', '审核', scope.row)"
            v-hasPermi="['system:post:edit']"
            >审核</el-button
          >
          <el-button
            type="text"
            @click="handleCheck('check', '查看', 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="open"
      :fullscreen="true"
      class="reset-dialog"
      @close="cancel"
    >
      <div class="shengc">
        <el-form ref="form" :model="form" disabled label-width="100px">
          <el-row :gutter="20">
            <el-col :span="12">
              <el-form-item label="标题：">
                <el-input
                  v-model="form.reportName"
                  placeholder="请输入"
                /> </el-form-item
            ></el-col>
            <el-col :span="12">
              <el-form-item label="前言：" prop="status">
                <el-input
                  type="textarea"
                  :autosize="{ minRows: 2, maxRows: 2 }"
                  placeholder="请输入"
                  v-model="form.reportPreface"
                >
                </el-input> </el-form-item
            ></el-col>
          </el-row>
          <el-row :gutter="20" class="row_header">
            <el-col :span="12">
              <el-form-item label="时间：">
                <el-date-picker
                  style="width: 100%"
                  v-model="form.dateRange"
                  type="daterange"
                  range-separator="至"
                  value-format="yyyy-MM-dd"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                >
                </el-date-picker> </el-form-item
            ></el-col>
            <el-col :span="12">
              <el-form-item label="统计类型：">
                <el-select
                  v-model="form.type"
                  placeholder="请选择"
                  style="width: 100%"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select> </el-form-item
            ></el-col>
          </el-row>
        </el-form>
        <div>
          <h2>一、岗位责任制检查总体情况</h2>
          <h3>(一)组织情况</h3>
          <Editor :height="300" v-model="form.htmlOne" :readOnly="true" />
          <h3>(二)岗检的时间、范围和开展形式</h3>
          <Editor :height="300" v-model="form.htmlTwo" :readOnly="true" />
          <h3>(三)岗检发现的主要问题</h3>
          <el-table border :data="problemList">
            <el-table-column label="问题属性" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.problemAttributeView || "--" }}</span>
              </template>
            </el-table-column>
            <el-table-column label="工作标准问题" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.problemCategoryThree || "--" }}</span>
              </template>
            </el-table-column>
            <el-table-column label="工作内容问题" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.problemCategoryTwo || "--" }}</span>
              </template>
            </el-table-column>
            <el-table-column label="考核奖励问题" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.problemCategoryFour || "--" }}</span>
              </template>
            </el-table-column>
            <el-table-column label="任职资格问题" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.problemCategoryFive || "--" }}</span>
              </template>
            </el-table-column>
            <el-table-column label="职责权限问题" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.problemCategoryOne || "--" }}</span>
              </template>
            </el-table-column>
            <el-table-column label="总计" align="center">
              <template slot-scope="{ row }">
                <span>{{ row.problemTotal || "--" }}</span>
              </template>
            </el-table-column>
          </el-table>
          <div style="height: 10px"></div>
          <Editor :height="300" v-model="form.htmlThree" :readOnly="true" />
          <h3>(四)原因分析</h3>
          <Editor :height="300" v-model="form.htmlFour" :readOnly="true" />
          <h3>(五)取得成效</h3>
          <Editor :height="300" v-model="form.htmlFive" :readOnly="true" />
          <h2>二、岗位问题整改情况</h2>
          <h3>(一)“立行立改”问题整改情况</h3>
          <Editor :height="300" v-model="form.htmlSix" :readOnly="true" />
          <h3>(二)“非立行立改”问题整改情况</h3>
          <Editor :height="300" v-model="form.htmlSeven" :readOnly="true" />
          <h2>三、下一步岗位责任提升计划</h2>
          <Editor :height="300" v-model="form.htmlEight" :readOnly="true" />
          <h2>四、总体结论</h2>
          <Editor :height="300" v-model="form.htmlNine" :readOnly="true" />
        </div>
        <div v-if="reason || type === 'edit'">
          <h3>不通过理由</h3>
          <el-input
            style="width: 500px"
            v-model="reason"
            placeholder="请输入"
            type="textarea"
            :autosize="{ minRows: 4, maxRows: 6 }"
            :disabled="type === 'check'"
          />
        </div>
        <div class="footer-button" v-if="type !== 'check'">
          <el-button type="danger" @click="submitForm(4)">不通过</el-button>
          <el-button type="primary" @click="submitForm(3)">通过</el-button>
          <el-button @click="cancel">取消</el-button>
        </div>
      </div>
    </el-dialog>

    <el-dialog
      title="查看"
      :visible.sync="checkDialog"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="提交部门：" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="方案名称：" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="文件名称：" prop="remark">
          <el-link type="primary">主要链接</el-link>
        </el-form-item>
      </el-form>
    </el-dialog>
    <el-dialog
      title="创建"
      :visible.sync="establishDialog"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="方案名称：" prop="postName">
          <el-input v-model="form.postName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="上传附件：" prop="postCode">
          <el-upload
            ref="upload"
            :limit="1"
            accept=".xlsx, .xls"
            :headers="upload.headers"
            :action="upload.url + '?updateSupport=' + upload.updateSupport"
            :disabled="upload.isUploading"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
            :auto-upload="false"
            drag
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">
              将文件拖到此处，或<em>点击上传</em>
            </div>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">创建</el-button>
        <el-button @click="cancelEstablish">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="预览附件"
      :visible.sync="previewAnnex"
      :fullscreen="true"
      :close-on-click-modal="false"
      :destroy-on-close="true"
    >
      <preview-file
        :preview-url="previewUrl"
        :preview-type="previewType"
        @handleDownload="handleDownload"
      />

      <div slot="footer" class="batch-dialog-footer">
        <el-button type="primary" @click="previewAnnex = false">
          关闭
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listCheckedReport,
  getCheckedReport,
  statusApprove,
  problemCategoryByAttribute,
} from "@/api/postImplementation/reportManagement.js";
import { getToken } from "@/utils/auth";
import  { getBaseUrl } from '@/utils/baseUrl.js'

export default {
  name: "Post",
  dicts: ["sys_normal_disable"],
  data() {
    return {
      problemList: [],
      reason: "",
      row: {},
      type: "",
      checkDialog: false,
      templateManagementDialog: false,
      textarea2: "",
      // 文件预览
      previewAnnex: false,
      previewUrl: "",
      previewType: "",
      filename: "",
      active: 0,
      establishDialog: false,
      options: [
        {
          value: 1,
          label: "综合岗检工作组",
        },
        {
          value: 2,
          label: "生产岗检工作组",
        },
        {
          value: 3,
          label: "联合办公室",
        },
      ],
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      status: {
        1: "待审核",
        2: "审核中",
        3: "通过",
        4: "驳回",
      },
      statusColor: {
        1: "#cdd152",
        2: "#61dd83",
        3: "#61dd83",
        4: "red",
      },
      title: "",
      // 是否显示弹出层
      open: false,
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的用户数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: getBaseUrl() + "/system/user/importData",
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        postCode: undefined,
        postName: undefined,
        status: undefined,
      },
      // 表单参数
      form: {
        htmlEight: "",
        htmlFive: "",
        htmlFour: "",
        htmlNine: "",
        htmlOne: "",
        htmlSeven: "",
        htmlSix: "",
        htmlThree: "",
        htmlTwo: "",
        reportName: "",
        reportPreface: "",
        type: "",
        dateRange: [],
      },
      // 表单校验
      rules: {
        postName: [
          { required: true, message: "岗位名称不能为空", trigger: "blur" },
        ],
        postCode: [
          { required: true, message: "岗位编码不能为空", trigger: "blur" },
        ],
        postSort: [
          { required: true, message: "岗位顺序不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
    problemCategoryByAttribute().then((res) => {
      this.problemList = res.data;
    });
  },
  methods: {
    handleTemplateManagement() {
      this.templateManagementDialog = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(
        "<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" +
          response.msg +
          "</div>",
        "导入结果",
        { dangerouslyUseHTMLString: true }
      );
      this.getList();
    },
    /** 查询岗位列表 */
    getList() {
      this.loading = true;
      listCheckedReport(this.queryParams).then((response) => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelEstablish() {
      this.establishDialog = false;
    },
    // 表单重置
    reset() {
      this.form = this.$options.data().form;
      this.reason = "";
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.postId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.establishDialog = true;
    },
    /** 查看 */
    handleCheck(type, title, row) {
      this.type = type;
      this.title = title;
      getCheckedReport(row.id).then((response) => {
        const dateRange = [response.data.startTime, response.data.endTime];
        this.form = { ...response.data, dateRange };
        this.reason = response.data.reason;
        this.open = true;
        this.row = row;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const postId = row.postId || this.ids;
      getPost(postId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm: function (type) {
      if (type === 4 && !this.reason) {
        this.$modal.msgWarning("请输入不通过理由！！！");
        return;
      }
      const name = type === 4 ? "已驳回" : "已通过";
      const params = {
        id: this.row.id,
        status: type,
        reason: this.reason,
      };
      statusApprove(params).then(() => {
        this.$modal.msgSuccess(name);
        this.open = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const postIds = row.postId || this.ids;
      this.$modal
        .confirm('是否确认删除岗位编号为"' + postIds + '"的数据项？')
        .then(function () {
          return delPost(postIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/post/export",
        {
          ...this.queryParams,
        },
        `post_${new Date().getTime()}.xlsx`
      );
    },

    handleDownload() {
      // 生成地址
      var myUrl = URL.createObjectURL(this.previewUrl);

      // 下载文件
      this.downloadFile(myUrl, this.filename);
    },
    downloadFile(url, name) {
      const a = document.createElement("a");
      a.setAttribute("href", url);
      a.setAttribute("download", name);
      a.setAttribute("target", "_blank");
      const clickEvent = document.createEvent("MouseEvents");
      clickEvent.initEvent("click", true, true);
      a.dispatchEvent(clickEvent);
    },
  },
};
</script>
<style lang="scss" scoped>
.resetStyle {
  width: 100%;
  ::v-deep .el-upload {
    width: 100%;
    .el-upload-dragger {
      width: 100%;
    }
  }
}
.reset-dialog {
  overflow: hidden;
  ::v-deep .el-dialog.is-fullscreen {
    overflow: hidden !important;
  }
  ::v-deep .el-dialog__body {
    height: calc(100vh - 50px);
    overflow: auto;
  }
}
.shengc {
  margin: 0 400px;
  .footer-button {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>
