<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="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入题目"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      
      <el-form-item label="制定单位" prop="publishDept">
        <!-- <el-select v-model="queryParams.publishDept" placeholder="制定单位" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select> -->
        <el-input
          v-model="queryParams.publishDept"
          placeholder="请输入制定单位"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布时间" prop="configKey">
        
        <el-date-picker
          v-model="dateRange"
          style="width: 340px"
          format="yyyy-MM-dd HH:mm:ss"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
       
      </el-form-item>
      <el-form-item label="文号">
        <el-input
          v-model="queryParams.docId"
          placeholder="请输入文号"
          clearable
          style="width: 240px"
          @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="['business:regulations:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdateChk"
          v-hasPermi="['business:regulations: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="['business:regulations: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="['system:config:export']"
        >导出</el-button>
      </el-col> -->
      
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="configList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="题目" align="center" prop="title" width="220"/>
      <el-table-column label="制定单位" align="center" prop="publishDept" width="180"/>
      <el-table-column label="文号" align="center" prop="docId" width="120" />
      
      <el-table-column label="内容" align="left" prop="content">
        <template slot-scope="scope">
          <el-popover placement="top-start" title="" width="100%" trigger="hover" popper-class="workorder-problem-desc" v-if="deleteTag(scope.row.content).length>20">
            <div>{{ deleteTag(scope.row.content).substr(0,500)+ '...' }}</div>
            <span slot="reference">
              {{ deleteTag(scope.row.content).length > 20 ? deleteTag(scope.row.content).substr(0,20) + '...':deleteTag(scope.row.content) }}
            </span>
          </el-popover>
          <span v-else>{{deleteTag(scope.row.content)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.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
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:regulations:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:regulations:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageIndex"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文号" prop="docId">
          <el-input v-model="form.docId" placeholder="请输入文号" />
        </el-form-item>
        <el-form-item label="题目" prop="title">
          <el-input v-model="form.title" placeholder="请输入题目" />
        </el-form-item>
        <el-form-item label="制定单位" prop="publishDept">
          <!-- <el-select v-model="form.publishDept" placeholder="制定单位" clearable>
            <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select> -->
          <el-input v-model="form.publishDept" placeholder="请输入制定单位" />
        </el-form-item>
        <!-- <el-form-item label="发布时间" prop="createTime">
          <el-date-picker
          type="datetime"
            v-model="form.createTime"
            style="width: 240px"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择发布时间"
          ></el-date-picker>
        
        </el-form-item> -->
        
        
        
        <el-form-item label="内容" prop="content">
          
          <editor v-model="form.content" :min-height="192"/>
        </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>
  </div>
</template>

<script>
import { list, getConfig, delConfig, add, update, refreshCache } from "@/api/business/regulations";

export default {
  name: "Config",
  dicts: ['sys_yes_no'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 参数表格数据
      configList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        title: undefined,
        publishDept: undefined,
        docId: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "题目不能为空", trigger: "blur" }
        ],
        docId: [
          { required: true, message: "文号不能为空", trigger: "blur" }
        ],
        publishDept: [
          { required: true, message: "制定单位不能为空", trigger: "blur" }
        ]
        ,
        content: [
          { required: true, message: "内容不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    deleteTag(content)
    {
        var regx = /<[^>]*>|<\/[^>]*>/gm;
        var result = content.replace(regx,"");
        return result;

    },
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      this.queryParams.queryFlag = 0
      if(this.dateRange.length > 0){
        this.queryParams['startTime'] = this.dateRange[0]
        this.queryParams['endTime'] = this.dateRange[1]
      }
      list(this.queryParams).then(response => {
          this.configList = response.data.records;
          this.total = response.data.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        docId: undefined,
        title: undefined,
        publishDept: undefined,
        content: undefined
      };
      // this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.docId = null
      this.queryParams.startTime = null;
      this.queryParams.endTime = null;
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加制度";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdateChk() {
      this.reset();
      const id = this.ids[0];
      var arr = this.configList.filter(item => id == item.id)
      // getConfig(configId).then(response => {
      this.form = arr[0];
      this.open = true;
      this.title = "修改制度";
      // });
    },
    handleUpdate(row) {
      this.reset();
      // getConfig(configId).then(response => {
      this.form = JSON.parse(JSON.stringify(row));
      this.open = true;
      this.title = "修改制度";
      // });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            delete this.form.id;
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const configIds = row.configId || this.ids;
      this.$modal.confirm('是否确认删除该条记录？').then(function() {
          return delConfig(configIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/config/export', {
        ...this.queryParams
      }, `config_${new Date().getTime()}.xlsx`)
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$modal.msgSuccess("刷新成功");
      });
    }
  }
};
</script>
