<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="演练名称" prop="drillName">
        <el-input
          v-model="queryParams.drillName"
          placeholder="请输入演练名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="演练类型" prop="drillType">
        <el-select v-model="queryParams.drillType" placeholder="请选择演练类型" clearable size="small">
          <el-option
            v-for="dict in drillTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="演练形式" prop="drillForm">
        <el-select v-model="queryParams.drillForm" placeholder="请选择演练形式" clearable size="small">
          <el-option
            v-for="dict in drillFormOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </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"
          v-hasPermi="['system:drill:add']"
        >新增</el-button>
      </el-col>
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="success"-->
          <!--plain-->
          <!--icon="el-icon-edit"-->
          <!--size="mini"-->
          <!--:disabled="single"-->
          <!--@click="handleUpdate"-->
          <!--v-hasPermi="['system:drill:edit']"-->
        <!--&gt;修改</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="['system:drill:remove']"-->
        <!--&gt;删除</el-button>-->
      <!--</el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['system:drill:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="drillList" @selection-change="handleSelectionChange">
      <!--<el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="演练名称" align="center" prop="drillName" />
      <el-table-column label="演练类型" align="center" prop="drillType" :formatter="drillTypeFormat" />
      <el-table-column label="演练形式" align="center" prop="drillForm" :formatter="drillFormFormat" />
      <el-table-column label="演练地址" align="center" prop="drillAddress" />
      <el-table-column label="主办单位" align="center" prop="drillUnit" />
      <el-table-column label="演练时间" align="center" prop="drillTime" width="180"/>

      <!--<el-table-column label="演练目的" align="center" prop="drillObjective" />-->
      <!--<el-table-column label="参演人员" align="center" prop="drillPeople" />-->
      <!--<el-table-column label="演练内容" align="center" prop="drillContent" />-->
      <!--<el-table-column label="评估" align="center" prop="assessment" />-->
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document-copy"
            @click="handleDetail(scope.row)"
            v-hasPermi="['system:drill:edit']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:drill: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" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <div class="division">
          <div class="div-kuang" style="width: 40%;">
            <el-form-item label="演练名称" prop="drillName">
              <el-input v-model="form.drillName" placeholder="请输入演练名称" />
            </el-form-item>
            <el-form-item label="演练类型" prop="drillType">
              <el-select v-model="form.drillType" placeholder="请选择演练类型">
                <el-option
                  v-for="dict in drillTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="演练形式" prop="drillForm">
              <el-select v-model="form.drillForm" placeholder="请选择演练形式">
                <el-option
                  v-for="dict in drillFormOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="parseInt(dict.dictValue)"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="演练地址" prop="drillAddress">
              <el-input v-model="form.drillAddress" placeholder="请输入演练地址" />
            </el-form-item>
            <el-form-item label="演练时间" prop="drillTime" >
              <el-date-picker clearable size="small"
                              style="width: 100%"
                              v-model="form.drillTime"
                              type="datetime"
                              value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="选择演练时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="主办单位" prop="drillUnit">
              <el-input v-model="form.drillUnit" placeholder="请输入主办单位" />
            </el-form-item>
            <el-form-item label="演练目的" prop="drillObjective">
              <el-input type="textarea" v-model="form.drillObjective" placeholder="请输入演练目的" />
            </el-form-item>
          </div>
          <div class="div-kuang" style="width: 58%;margin-left: 2%">
            <el-form-item label="参演人员" prop="drillPeople">
              <el-input v-model="form.drillPeople" type="textarea" placeholder="请输入内容" />
            </el-form-item>
            <el-form-item label="演练内容">
              <editor v-model="form.drillContent" :min-height="240"/>
            </el-form-item>
          </div>
        </div>


        <!--<el-form-item label="评估" prop="assessment">-->
          <!--<el-input v-model="form.assessment" type="textarea" placeholder="请输入内容" />-->
        <!--</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-dialog title="演练详情" :visible.sync="open2" width="1200px" append-to-body>
      <el-form  label-width="90px" size="mini">
        <div class="division">
          <div class="div-kuang" style="width: 40%;">
            <el-form-item label="演练名称:" prop="drillName">
              <span>{{form.drillName}}</span>
            </el-form-item>
            <el-form-item label="演练类型:" prop="drillType">
              <span>{{form.drillType}}</span>
            </el-form-item>
            <el-form-item label="演练形式:" prop="drillForm">
              <span>{{form.drillForm}}</span>
            </el-form-item>
            <el-form-item label="演练地址:" prop="drillAddress">
              <span>{{form.drillAddress}}</span>
            </el-form-item>
            <el-form-item label="演练时间:" prop="drillTime" >
              <span>{{form.drillTime}}</span>
            </el-form-item>
            <el-form-item label="主办单位:" prop="drillUnit">
              <span>{{form.drillUnit}}</span>
            </el-form-item>
            <el-form-item label="演练目的:" prop="drillObjective">
              <span>{{form.drillObjective}}</span>
            </el-form-item>
          </div>
          <div class="div-kuang" style="width: 58%;margin-left: 2%">
            <el-form-item label="参演人员:" prop="drillPeople">
              <span>{{form.drillPeople}}</span>
            </el-form-item>
            <el-form-item label="演练内容:">
              <editor v-model="form.drillContent" :min-height="240" :readOnly="readOnly"/>
            </el-form-item>
          </div>
        </div>


        <!--<el-form-item label="评估" prop="assessment">-->
        <!--<el-input v-model="form.assessment" type="textarea" placeholder="请输入内容" />-->
        <!--</el-form-item>-->
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { listDrill, getDrill, delDrill, addDrill, updateDrill, exportDrill } from "@/api/system/drill";
import Editor from '@/components/Editor';

export default {
  name: "Drill",
  components: {
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      readOnly:true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应急演练表格数据
      drillList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      open2:false,
      // 演练类型字典
      drillTypeOptions: [],
      // 演练形式字典
      drillFormOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        drillName: null,
        drillType: null,
        drillForm: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        drillName: [
          { required: true, message: "演练名称不能为空", trigger: "blur" }
        ],
        drillType: [
          { required: true, message: "演练类型不能为空", trigger: "blur" }
        ],
        drillForm: [
          { required: true, message: "演练形式不能为空", trigger: "blur" }
        ],
        drillAddress: [
          { required: true, message: "演练地址不能为空", trigger: "blur" }
        ],
        drillTime: [
          { required: true, message: "演练时间不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("t_drill_type").then(response => {
      this.drillTypeOptions = response.data;
    });
    this.getDicts("t_drill_form").then(response => {
      this.drillFormOptions = response.data;
    });
  },
  methods: {
    /** 查询应急演练列表 */
    getList() {
      this.loading = true;
      listDrill(this.queryParams).then(response => {
        this.drillList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 演练类型字典翻译
    drillTypeFormat(row, column) {
      return this.selectDictLabel(this.drillTypeOptions, row.drillType);
    },
    // 演练形式字典翻译
    drillFormFormat(row, column) {
      return this.selectDictLabel(this.drillFormOptions, row.drillForm);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        drillId: null,
        drillName: null,
        drillAddress: null,
        drillUnit: null,
        drillTime: null,
        drillType: null,
        drillForm: null,
        drillObjective: null,
        drillPeople: null,
        drillContent: null,
        assessment: null,
        createTime: null,
        createBy: null,
        isDel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.drillId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加应急演练";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const drillId = row.drillId || this.ids
      getDrill(drillId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改应急演练";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.drillId != null) {
            updateDrill(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDrill(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 详情按钮操作 */
    handleDetail(row) {
      getDrill(row.drillId).then(response => {
        this.form = response.data;
        this.form.drillType = this.selectDictLabel(this.drillTypeOptions, row.drillType);
        this.form.drillForm = this.selectDictLabel(this.drillFormOptions, row.drillForm);
        this.open2 = true;
    });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      //const drillIds = row.drillId || this.ids;
      this.$confirm('是否确认删除数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return updateDrill({drillId:row.drillId,isDel:1});
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有应急演练数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportDrill(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
<style>
  .div-kuang{
    background: white;
    padding-top:20px ;
    padding-right: 20px;
    border-radius: 10px;
  }
  .division{
    display:flex;
    flex-direction:row;
    justify-content:flex-start;
  }
</style>
<style lang="scss" scoped>
  ::v-deep .el-select{
    width: 100%;
  }
  ::v-deep .el-dialog__header{
    border-bottom: 1px solid #ccc;
  }
  ::v-deep .el-dialog{
    background: #f7f7f7;
  }
</style>
