<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">

      <el-form-item label="作业证编号" prop="documentNum">
        <el-input
          v-model="queryParams.documentNum"
          placeholder="请输入作业证编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="作业证类型" prop="documentType">
        <el-select v-model="queryParams.documentType" placeholder="请选择作业证类型" clearable size="small">
          <el-option
            v-for="item in specialPermitType"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option label="待分析" value="0" />
          <el-option label="已过期" value="1" />
          <el-option label="待签字" value="2" />
          <el-option label="待验收" value="3"/>
          <el-option label="已完成" value="4" />
        </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="['workPermit:document: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="['workPermit:document: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"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		      :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="documentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="作业证编号" align="center" prop="documentNum" />
      <el-table-column label="作业证类型" align="center" prop="documentType" :formatter="documentTypeFormatter"/>
      <el-table-column label="申请部门" align="center" prop="applyDeptName" />
      <el-table-column label="申请人" align="center" prop="applyName" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span v-if="scope.row.status == 0" style="color: red">待分析</span>
          <span v-if="scope.row.status == 1" style="color: purple">已过期</span>
          <span v-if="scope.row.status == 2" style="color: red">待签字</span>
          <span v-if="scope.row.status == 3" style="color: blue">待验收</span>
          <span v-if="scope.row.status == 4" style="color: green">已完成</span>
          <span v-if="scope.row.suspend == 1" style="color: red">(暂停中)</span>
        </template>
      </el-table-column>
      <el-table-column label="开始时间" align="center" prop="startTime" >
        <!--<template slot-scope="scope">-->
          <!--<span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>-->
        <!--</template>-->
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" >
        <!--<template slot-scope="scope">-->
          <!--<span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>-->
        <!--</template>-->
      </el-table-column>
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['workPermit:document:edit']"
          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
          >详情</el-button>

          <el-button
            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="500px" append-to-body destroy-on-close :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="申请部门id" prop="deptId">
          <el-input v-model="form.deptId" placeholder="请输入申请部门id" />
        </el-form-item>
        <el-form-item label="申请人id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入申请人id" />
        </el-form-item>
        <el-form-item label="1动火作业证 2动土 3断路 4高处 5临时用电 6盲板 7受限空间 8吊装" prop="documentType">
          <el-select v-model="form.documentType" placeholder="请选择1动火作业证 2动土 3断路 4高处 5临时用电 6盲板 7受限空间 8吊装">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="作业证编号" prop="documentNum">
          <el-input v-model="form.documentNum" placeholder="请输入作业证编号" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable size="small"
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable size="small"
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="基础信息json" prop="baseJson">
          <el-input v-model="form.baseJson" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="分析json" prop="analysisJson">
          <el-input v-model="form.analysisJson" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="分析时间" prop="analysisTime">
          <el-date-picker clearable size="small"
            v-model="form.analysisTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择分析时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="措施json" prop="contentJson">
          <el-input v-model="form.contentJson" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="0待分析 1已过期 2待签字 3已完成">
          <el-radio-group v-model="form.status">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </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>

    <!-- 详情 -->
    <DetailInfo ref="detailInfo"/>
  </div>
</template>

<script>
import { listDocument, getDocument, delDocument, addDocument, updateDocument, exportDocument } from "@/api/specialworkpermit/document";
import DetailInfo from "./components/DetailInfo";
export default {
  name: "Document",
  components: {
    DetailInfo
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 特殊作业单表格数据
      documentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        userId: null,
        documentType: null,
        documentNum: null,
        startTime: null,
        endTime: null,
        baseJson: null,
        analysisJson: null,
        analysisTime: null,
        contentJson: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      specialPermitType: [],
      documentNums: [],
    };
  },
  created() {
    this.getList();
    this.getSpecialPermitType();
  },
  methods: {
    /** 查询特殊作业单列表 */
    getList() {
      this.loading = true;
      listDocument(this.queryParams).then(response => {
        this.documentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deptId: null,
        userId: null,
        documentType: null,
        documentNum: null,
        startTime: null,
        endTime: null,
        baseJson: null,
        analysisJson: null,
        analysisTime: null,
        contentJson: null,
        status: 0,
        createTime: 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.id);
      this.documentNums = selection.map(item => item.documentNum);
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加特殊作业单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDocument(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改特殊作业单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDocument(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDocument(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const documentNums = row.documentNum || this.documentNums;
      this.$confirm('是否确认删除特殊作业单编号为"' + documentNums + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delDocument(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有特殊作业单数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportDocument(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    //特殊作业证类型
    getSpecialPermitType(){
      this.getDicts("t_special_permit_type").then(res =>{
        this.specialPermitType = res.data;
      })
    },
    documentTypeFormatter(row){
      return this.selectDictLabel(this.specialPermitType,row.documentType);
    },
    //详情
    handleDetail(row){
      getDocument(row.id).then(res => {
        if(res.code == 200 && res.data){
          if(!res.data.baseJson) return this.$message.error("表单数据未填写！");
          this.$refs.detailInfo.dataInfo = res.data;
          this.$refs.detailInfo.type = res.data.documentType;
          this.$refs.detailInfo.open = true;
        }
      });
    }
  }
};
</script>
