<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务标题" prop="workTitle">
        <el-input
          v-model="queryParams.workTitle"
          placeholder="请输入任务标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务类型" prop="workType">
        <el-select v-model="queryParams.workType" placeholder="请选择任务类型" clearable size="small">
          <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 label="任务状态" prop="workStatus">
        <el-select v-model="queryParams.workStatus" placeholder="请选择任务状态" clearable size="small">
          <el-option label="派发中" value="1" />
          <el-option label="反馈" value="2" />
          <el-option label="归档" value="3" />
        </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:order: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:order: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="['system:order:remove']"
        >删除</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:order:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="任务标题" align="center" prop="workTitle" />
      <el-table-column label="任务类型" align="center" prop="workType" />
      <el-table-column label="任务内容" align="center" prop="workContent" />
      <el-table-column label="创建单位" align="center" prop="workCreateEnterpriseName" />
      <el-table-column label="指派单位" align="center" prop="workAssignEnterproseName" />
      <el-table-column label="指派人" align="center" prop="workAssignManId" />
      <el-table-column label="任务状态" align="center" prop="workStatus" />
      <el-table-column label="巡检时间" align="center" prop="inspectionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.inspectionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="巡检路线" align="center" prop="inspectionRoute" />
      <el-table-column label="问题描述" align="center" prop="problemDescription" />
      <el-table-column label="问题图片" align="center" prop="pictureUrl" style="text-align:center;">
        <template slot-scope="scope">
          <img :src="scope.row.iconUrl" style="width: 20%;vertical-align:middle;cursor:pointer;" @click="showPicture(scope.row)"/>
          <el-image :ref="'a'+scope.row.workId" :src="scope.row.iconUrl" v-show="false" :preview-src-list="[scope.row.iconUrl]" v-if="scope.row.iconUrl != '' && scope.row.iconUrl != null"></el-image>
        </template>
      </el-table-column>
      <!--<el-table-column label="图片路径" align="center" prop="iconUrl" />-->
      <el-table-column label="整改方案" align="center" prop="rectificationPlan" />
      <el-table-column label="整改结果" align="center" prop="rectificationResult" />
      <el-table-column label="责任单位" align="center" prop="responsibleUnit" />
      <el-table-column label="责任人员" align="center" prop="responsiblePerson" />
      <el-table-column label="截止日期" align="center" prop="expiryDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expiryDate, '{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 v-if="'admin'==$store.state.user.roles[0] && scope.row.workAssignMan == null && scope.row.workStatus == '1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:order:edit']"
          >任务下发</el-button>
          <el-button v-if="'admin'==$store.state.user.roles[0]  && scope.row.workStatus == '1'"
                     size="mini"
                     type="text"
                     icon="el-icon-edit"
                     @click="handleUpdate(scope.row)"
                     v-hasPermi="['system:order:edit']"
          >接单</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:order:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:order: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="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务标题" prop="workTitle">
              <el-input v-model="form.workTitle" placeholder="请输入任务标题" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="任务类型" prop="workType">
              <el-select v-model="form.workType" placeholder="请选择任务类型" style="width: 350px">
                <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-col>
        </el-row>
        <el-form-item label="任务内容">
          <editor v-model="form.workContent" :min-height="192"/>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="指派单位" prop="workAssignEnterproseName">
              <el-input v-model="form.workAssignEnterproseName" placeholder="请输入指派单位名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="指派人" prop="workAssignManId">
              <el-input v-model="form.workAssignManId" placeholder="请输入指派人" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="巡检时间" prop="inspectionDate">
          <el-date-picker clearable size="small"
            v-model="form.inspectionDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择巡检时间" style="width: 350px">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="巡检路线" prop="inspectionRoute">
          <el-input v-model="form.inspectionRoute" type="textarea" placeholder="请输入巡检路线" />
        </el-form-item>
        <el-form-item label="问题描述" prop="problemDescription">
          <el-input v-model="form.problemDescription" type="textarea" placeholder="请输入问题描述" />
        </el-form-item>
        <el-form-item label="问题图片" prop="iconUrl">
          <MyFileUpload
            listType="picture-card"
            @resFun="getFileInfo"
            @remove="listRemove"
            :fileArr="fileList"
          />
          <el-input v-show="false" disabled v-model="form.iconUrl"></el-input>
        </el-form-item>
        <el-form-item label="整改方案" prop="rectificationPlan">
          <el-input v-model="form.rectificationPlan" type="textarea" placeholder="请输入整改方案" />
        </el-form-item>
        <el-form-item label="整改结果" prop="rectificationResult">
          <el-input v-model="form.rectificationResult" type="textarea" placeholder="请输入整改结果" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="责任单位" prop="responsibleUnit">
              <el-input v-model="form.responsibleUnit" placeholder="请输入责任单位" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="责任人员" prop="responsiblePerson">
              <el-input v-model="form.responsiblePerson" placeholder="请输入责任人员" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="截止日期" prop="expiryDate">
          <el-date-picker clearable size="small"
            v-model="form.expiryDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择截止日期" style="width: 350px">
          </el-date-picker>
        </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 { listOrder, getOrder, delOrder, addOrder, updateOrder, exportOrder } from "@/api/operationMonitor/order";
import Editor from '@/components/Editor';
import MyFileUpload from '@/components/MyFileUpload';
export default {
  name: "Order",
  components: {
    Editor,
    MyFileUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 燃气任务表格数据
      orderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 上传文件列表
      fileList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workTitle: null,
        workType: null,
        workContent: null,
        workCreateEnterpriseName: null,
        workCreateEnterpriseId: null,
        workAssignEnterproseName: null,
        workAssignEnterproseId: null,
        workAssignManId: null,
        workAssignMan: null,
        workStatus: null,
        inspectionDate: null,
        inspectionRoute: null,
        problemDescription: null,
        iconUrl: null,
        rectificationPlan: null,
        rectificationResult: null,
        responsibleUnit: null,
        responsiblePerson: null,
        expiryDate: null,
        isDel: null,
        remarks: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询燃气任务列表 */
    getList() {
      this.loading = true;
      listOrder(this.queryParams).then(response => {
        this.orderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        workId: null,
        workTitle: null,
        workType: null,
        workContent: null,
        workCreateEnterpriseName: null,
        workCreateEnterpriseId: null,
        workAssignEnterproseName: null,
        workAssignEnterproseId: null,
        workAssignManId: null,
        workAssignMan: null,
        workStatus: "0",
        inspectionDate: null,
        inspectionRoute: null,
        problemDescription: null,
        iconUrl: null,
        rectificationPlan: null,
        rectificationResult: null,
        responsibleUnit: null,
        responsiblePerson: null,
        expiryDate: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        isDel: null,
        remarks: 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.workId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加燃气任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const workId = row.workId || this.ids
      getOrder(workId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改燃气任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.workId != null) {
            updateOrder(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrder(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const workIds = row.workId || this.ids;
      this.$confirm('是否确认删除燃气任务编号为"' + workIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delOrder(workIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有燃气任务数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportOrder(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    getFileInfo(res){
      this.form.iconUrl = res.url;
    },
    listRemove(e) {
      this.form.iconUrl = "";
      this.fileList = [];
    },
    showPicture(row){
      this.$refs['a'+row.workId].showViewer = true;
    }
  }
};
</script>
