<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="制度标题" prop="systemTitle">
        <el-input
          v-model="queryParams.systemTitle"
          placeholder="请输入制度标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="内容分类" prop="contentType">
        <el-select v-model="queryParams.contentType" placeholder="请选择内容分类" clearable size="small">
          <el-option
            v-for = "dict in contentTypeOptions"
            :key = "dict.dictValue"
            :label = "dict.dictLabel"
            :value = "dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for = "dict in statusOptions"
            :key = "dict.dictValue"
            :label = "dict.dictLabel"
            :value = "dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
        ></el-date-picker>
      </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>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="enterpriseSystemList" >
<!--      <el-table-column label="制度id" align="center" prop="systemId" />-->
      <el-table-column label="制度标题" align="center" prop="systemTitle" />
      <el-table-column label="内容分类" align="center" prop="contentType" :formatter="contentTypeFormat" />
      <el-table-column label="附件" align="center" prop="fileUrl" width="260px">
        <template slot-scope="scope">
          <span
            class="dbtn"
            @click="checkFile(scope.row.fileUrl)"
            v-if="scope.row.fileUrl != null && scope.row.fileUrl!=''"
          >
            <i class="el-icon el-icon-view"></i>{{scope.row.fileName}}
          </span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-if="scope.row.status == '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePublish(scope.row)"
          >发布</el-button>
          <el-button
            v-if="scope.row.status == '1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleApproval(scope.row)"
          >审批</el-button>
          <el-button
            v-if="scope.row.status == '2'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleInvalid(scope.row)"
          >作废</el-button>
          <el-button
            v-if="scope.row.status == '0'"
            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 :title="title" :visible.sync="open" width="1200px" @closed="dialogClose" @open="dialogOpen" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <div class="division">
          <div style="width: 42%;">
            <div class="dialogTitle">制度基本信息</div>
            <el-form-item label="制度标题" prop="systemTitle">
              <el-input v-model="form.systemTitle" placeholder="请输入制度标题" :disabled="readOnly"/>
            </el-form-item>
            <el-form-item label="内容分类" prop="contentType">
              <el-select v-model="form.contentType" placeholder="请选择内容分类" filterable clearable :disabled="readOnly" style="width: 100%">
                <el-option
                  v-for = "dict in contentTypeOptions"
                  :key = "dict.dictValue"
                  :label = "dict.dictLabel"
                  :value = "dict.dictValue"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" :disabled="readOnly" />
            </el-form-item>
            <el-form-item label="附件" v-if="!readOnly" prop="fileUrl">
              <FileUpload
                listType="picture"
                @resFun="getFileInfo"
                @remove="listRemove"
                :fileArr="fileList"
              />
              <el-input v-show="false" disabled v-model="form.fileUrl"></el-input>
            </el-form-item>
            <el-form-item label="附件" v-if="readOnly" prop="fileUrl" >
              <span
                class="dbtn"
                @click="checkFile(form.fileUrl)"
                v-if="form.fileUrl!=null && form.fileUrl != ''"
              >
                <i class="el-icon el-icon-view"></i>{{form.fileName}}
              </span>
              <span v-else>-</span>
            </el-form-item>
            <el-form-item v-show="operate" label="审批" prop="status">
              <el-radio v-model="form.status" label="2">通过</el-radio>
              <el-radio v-model="form.status" label="0">驳回</el-radio>
            </el-form-item>
          </div>
          <div style="width: 58%;margin-left: 2%">
            <div class="dialogTitle">制度内容</div>
            <editor id="editor" v-if="isOpen" :readOnly="readOnly" v-model="form.content" :min-height="300"/>
          </div>
        </div>
      </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>
  </div>
</template>

<script>
import { listEnterpriseSystem, getEnterpriseSystem, delEnterpriseSystem, addEnterpriseSystem, updateEnterpriseSystem, exportEnterpriseSystem } from "@/api/safetyManagement/enterpriseSystem";
import Editor from '@/components/Editor';
let uploadfile = require("@/assets/uploadfile.png");
import FileUpload from '@/components/FileUpload';

export default {
  name: "EnterpriseSystem",
  components: {
    Editor,
    FileUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 日期范围
      dateRange: [],
      // 企业制度管理表格数据
      enterpriseSystemList: [],
      contentTypeOptions: [],
      statusOptions: [],
      fileList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isOpen:false,
      operate: false,
      readOnly: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        systemType: '1',
        systemTitle: null,
        contentType: null,
        content: null,
        isDel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        systemTitle: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        contentType: [
          { required: true, message: "内容类型不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("t_content_type").then(response =>{
      this.contentTypeOptions = response.data;
    })
    this.getDicts("t_enterprise_system_status").then(response =>{
      this.statusOptions = response.data;
    })
  },
  methods: {
    // 内容分类
    contentTypeFormat(row, column) {
      return this.selectDictLabel(this.contentTypeOptions, row.contentType);
    },
    // 内容分类
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    /** 查询企业制度管理列表 */
    getList() {
      this.loading = true;
      listEnterpriseSystem(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.enterpriseSystemList = response.rows;
        console.log("enterpriseSystemList", this.enterpriseSystemList)
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.fileList = [];
    },
    // 表单重置
    reset() {
      this.form = {
        systemId: null,
        systemTitle: null,
        systemType: '1',
        contentType: null,
        content: null,
        fileUrl: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remark: null
      };
      this.resetForm("form");
      this.fileList = [];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.readOnly=false;
      this.reset();
      this.open = true;
      this.title = "添加企业制度信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.readOnly=false;
      this.reset();
      const systemId = row.systemId || this.ids
      getEnterpriseSystem(systemId).then(response => {
        this.form = response.data;
        console.log("审批驳回修改", this.readOnly)
        this.open = true;
        this.title = "修改企业制度信息";
        if(this.form.fileUrl!=null||this.form.fileUrl==""){
          this.fileList = [{name: this.form.fileName, url: uploadfile}];
        }
      });
    },
    /** 审批按钮操作 */
    handleApproval(row) {
      this.readOnly = true;
      this.reset();
      const systemId = row.systemId || this.ids
      getEnterpriseSystem(systemId).then(response => {
        this.form = response.data;
        this.form.status = '2';
        this.operate = true;
        console.log("审批开始", this.readOnly);
        this.open = true;
        this.title = "审批企业制度信息";
        if(this.form.fileUrl!=null||this.form.fileUrl==""){
          this.fileList = [{name: this.form.fileName, url: uploadfile}];
        }
      });
    },
    dialogClose(){
      this.isOpen=false;
    },
    dialogOpen(){
      this.isOpen=true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.systemId != null) {
            if(this.operate){
              updateEnterpriseSystem(this.form).then(response => {
                this.msgSuccess("审批成功");
                this.open = false;
                this.operate = false;
                this.readOnly = false;
                console.log("审批完成", this.readOnly);
                this.getList();
              });
            } else {
              updateEnterpriseSystem(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            }
          } else {
            addEnterpriseSystem(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 发布按钮操作 */
    handlePublish(row) {
      this.$confirm('确认要发布 "' + row.systemTitle + '" 的制度信息?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        row.status = '1';
        return updateEnterpriseSystem(row);
      }).then(() => {
        this.getList();
        this.msgSuccess("发布成功");
      }).catch(() => {});
    },
    /** 作废按钮操作 */
    handleInvalid(row) {
      this.$confirm('确认要作废 "' + row.systemTitle + '" 的制度信息?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        row.status = '3';
        return updateEnterpriseSystem(row);
      }).then(() => {
        this.getList();
        this.msgSuccess("作废成功");
      }).catch(() => {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('是否确认删除 "' + row.systemTitle + '" 的制度信息?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          row.isDel = '1';
          return updateEnterpriseSystem(row);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有企业制度管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportEnterpriseSystem(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    //上传
    getFileInfo(res){
      this.form.fileUrl = res.url;
      this.form.fileName = res.fileName;
      this.fileList.push({
        name: res.fileName,
        url: uploadfile,
      });
    },
    listRemove(e) {
      this.fileList = [];
      this.form.fileUrl = null;
      this.form.fileName = null;
    },
    checkFile(url) {
      window.open(url,'_blank');
    },
  }
};
</script>
<style>
  .division{
    display:flex;
    flex-direction:row;
    justify-content:flex-start;
  }

  .dbtn {
    display: inline-block;
    line-height: normal;
    padding-left: 2px;
    padding-right: 2px;
    cursor: pointer;
    border-radius: 3px;
    border-style: solid;
    border-width: 0;
    color: rgb(48, 180, 107);
  }

  .dialogTitle {
    background: #1c84c6;
    color: white;
    height: 20px;
    line-height: 20px;
    text-align: center;
    margin-bottom: 10px;
  }
</style>
