<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <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="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"
          v-hasPermi="['safetyManagement:enterpriseSystem:add']"
        >新增</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="法律法规标题" align="center" prop="systemTitle" width="260px"/>
      <el-table-column label="颁布部门" align="center" prop="issueDept" >
        <template slot-scope="scope">
          <span v-if="scope.row.issueDept != null && scope.row.issueDept!=''">
            {{scope.row.issueDept}}
          </span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="文号" align="center" prop="referenceNum" >
        <template slot-scope="scope">
          <span v-if="scope.row.referenceNum != null && scope.row.referenceNum!=''">
            {{scope.row.referenceNum}}
          </span>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="层级" align="center" prop="hierarchy" :formatter="hierarchyFormat"/>
      <el-table-column label="有效性" align="center" prop="availability" :formatter="availabilityFormat"/>
      <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" width="120px" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="150px" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="210px" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['safetyManagement:enterpriseSystem:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <el-button
            v-if="scope.row.status == '0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePublish(scope.row)"
            v-hasPermi="['safetyManagement:enterpriseSystem:edit']"
          >发布</el-button>
          <el-button
            v-if="scope.row.status == '1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleApproval(scope.row)"
            v-hasPermi="['safetyManagement:enterpriseSystem:edit']"
          >审批</el-button>
          <el-button
            v-if="scope.row.status == '2'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleInvalid(scope.row)"
            v-hasPermi="['safetyManagement:enterpriseSystem:edit']"
          >作废</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['safetyManagement:enterpriseSystem:remove']"
          >删除</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="120px">
        <div class="division">
          <div style="width: 45%;">
            <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="hierarchy">
              <el-select v-model="form.hierarchy" placeholder="请选择层级" clearable size="small" :disabled="readOnly" style="width: 100%">
                <el-option
                  v-for = "dict in hierarchys"
                  :key = "dict.dictValue"
                  :label = "dict.dictLabel"
                  :value = "dict.dictValue"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="文号" prop="referenceNum">
              <el-input v-model="form.referenceNum" placeholder="请输入文号" :disabled="readOnly"/>
            </el-form-item>
            <el-form-item label="颁布部门" prop="issueDept">
              <el-input v-model="form.issueDept" placeholder="请输入颁布部门" :disabled="readOnly"/>
            </el-form-item>
            <el-form-item label="有效性" prop="availability">
              <el-select v-model="form.availability" placeholder="请选择有效性" clearable size="small" :disabled="readOnly" style="width: 100%">
                <el-option
                  v-for = "dict in availabilitys"
                  :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" v-show="!readOnly">
        <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/supervise/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: [],
      statusOptions: [],
      fileList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      isOpen:false,
      operate: false,
      readOnly: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        systemType: '2',
        systemTitle: null,
        content: null,
        isDel: '0',
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        systemTitle: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        hierarchy: [
          { required: true, message: "请选择层级", trigger: "change" }
        ],
      },
      hierarchys:[],
      availabilitys:[]
    };
  },
  created() {
    this.getList();
    this.getDicts("t_enterprise_system_status").then(response =>{
      this.statusOptions = response.data;
    })
    this.getDicts("t_hierarchy").then(response =>{
        this.hierarchys = response.data;
    })
    this.getDicts("t_availability").then(response =>{
        this.availabilitys = response.data;
    })

  },
  methods: {
    // 状态
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    /** 查询法律法规管理列表 */
    getList() {
      this.loading = true;
      var dateRange = this.dateRange;
      if (null != dateRange && '' != dateRange) {
        this.queryParams.beginTime = dateRange[0];
        this.queryParams.endTime = dateRange[1];
      }else{
        this.queryParams.beginTime = null;
        this.queryParams.endTime = null;
      }
      listEnterpriseSystem(this.queryParams).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: '2',
        content: null,
        fileUrl: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remark: null
      };
      this.resetForm("form");
      this.fileList = [];
      this.operate=false;
    },
    /** 搜索按钮操作 */
    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;
        this.open = true;
        this.title = "修改法律法规信息";
        if(this.form.fileUrl!=null||this.form.fileUrl==""){
          this.fileList = [{name: this.form.fileName, url: uploadfile}];
        }
      });
    },
    /** 详情按钮操作*/
    handleDetail(row) {
      this.readOnly=true;
      this.reset();
      const systemId = row.systemId || this.ids
      getEnterpriseSystem(systemId).then(response => {
        this.form = response.data;
        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;
        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;
                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');
    },
    hierarchyFormat(row,colum){
      return this.selectDictLabel(this.hierarchys, row.hierarchy);
    },
    availabilityFormat(row,colum){
      return this.selectDictLabel(this.availabilitys, row.availability);
    }
  }
};
</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>
