<template>
  <div v-loading="loading">
    <manager-ui
      :query-model="queryParams.fileMonitorMap"
      :query-items="queryItems"
      :static-data="staticData"
      :column-items="columnItems"
      :table-data="tableData"
      :pagination="page"
      :spread-threshold="1"
      :show-add-btn="showAddBtn"
      show-table-tools
      @create="openAddNewDialog"
      @selection-change="handleSelectionChange"
      @searchClick="searchClick"
      @search="search"
      @resetForm="resetForm"
      @expandChange="handleLogInfoExpand"
    >
      <template #transmissionWay="{scope}">
        {{ 
            scope.row.transmissionWay
          | toCase(staticData.transmissionWayList, "id", "label")
        }}
      </template>
      <template #transmissionType="{scope}">
        {{ scope.row.transmissionType| toCase(
          staticData.transmissionTypeList,
          "id",
          "label"
        ) }}
      </template>
      <template #isDeleteFile="{scope}">
        {{ scope.row.isDeleteFile| toCase(
          staticData.isDeleteFileList,
          "id",
          "label"
        ) }}
      </template>
      <template #isStart="{scope}">
        {{ scope.row.isStart| toCase(
          staticData.isStartList,
          "id",
          "label"
        ) }}
      </template>
      <template #expand="{scope}">
        <div style="padding:20px 50px">
          <el-table tooltip-effect="dark"
                          :data="scope.row.list"
                          stripe
                          border
                          v-loading="logLoading" 
                         >
                  <el-table-column prop="isSuccess" label="是否成功" show-overflow-tooltip>
                    <template
                      slot-scope="scope"
                    >{{scope.row.isSuccess | toCase( staticData.isSuccessList,'id', 'label')}}</template>
                  </el-table-column>
                  <el-table-column prop="fileActualNumber" label="文件获取数量" show-overflow-tooltip></el-table-column>
                  <el-table-column prop="fileUploadNumber" label="文件成功数量" show-overflow-tooltip></el-table-column>
                  <el-table-column prop="failReason" label="失败原因" show-overflow-tooltip>
                    <template
                    slot-scope="scope"
                  >{{scope.row.failReason | toCase( staticData.failReasonList,'id', 'label')}}</template>
                  </el-table-column>
                  <el-table-column prop="failLog" label="失败日志" show-overflow-tooltip></el-table-column>
                  <el-table-column prop="createTime" label="日志时间" show-overflow-tooltip></el-table-column>
                  <el-table-column label="操作" align="center" width="120">
                    <template slot-scope="scope">
                      <el-context-menu :menus="menus2" :scope="scope" :default="defaultVInner"
                                       @oper="clickRow2"
                                       ></el-context-menu>
                    </template>
                  </el-table-column>
                </el-table>
              <el-pagination
                layout="total,prev, pager, next,jumper"
                @current-change="(()=>{return handleInnerCurrentChange.bind(this, scope.row)})()"
                :page-size="scope.row.innerPage.size"
                :current-page.sync="scope.row.innerPage.current"
                :total="scope.row.innerPage.total"
              ></el-pagination>
        </div>
       </template> 

      <template slot="tableTools">
        <el-button type="button" icon="el-icon-delete-solid">
          <span @click="batchdel">
            删除
          </span>
        </el-button>
      </template>

      <template #action="{scope}">
        <el-context-menu :menus="menus" :scope="scope" :visibles="[scope.row.isStart=='0',scope.row.isStart=='1']" :default="4" @oper="clickRow" />
      </template>

    </manager-ui>
    
    <manager-dialog 
    :title="titleText"
    width="52%"
    :visible.sync="addnewDialog" 
    @closed="closeAdd">
      <el-form
        label-width="150px"
        :model="dialogForm"
        :label-position="labelPosition"
        ref="dialogForm"
      >
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="使用场景名称："
              prop="fileMonitor.userSceneName"
              :rules="[{ required: true, message: '使用场景不能为空', trigger: 'blur' }]">
              <el-input v-model="dialogForm.fileMonitor.userSceneName" :disabled="type == 3"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="执行时间："
              prop="fileMonitor.execitionTime"
              :rules="[{ required: true, validator: cronCheck, trigger: 'blur' }]">
              <el-tooltip class="item" effect="dark" style="size: 16px" content="请输入cron表达式。例：0 0/5 * * * ? 每5分钟执行一次" placement="top-start">
                <el-input v-model="dialogForm.fileMonitor.execitionTime"  placeholder="请输入cron表达式。例：0 0/5 * * * ? " :disabled="type == 3"></el-input>
              </el-tooltip>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="下次执行时间："
              prop="">
              <el-button icon="el-icon-view" @click="nextExecitionTime()"><span>{{nextTime}}</span></el-button>
            </el-form-item>
            </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="传输方式：" prop="fileMonitor.transmissionWay">
              <el-select v-model="dialogForm.fileMonitor.transmissionWay">
                <el-option
                  v-for="item in staticData.transmissionWayList"
                  :label="item.label"
                  :key="item.id"
                  :value="item.id"
                  :disabled="type == 3"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="传输类型：" prop="fileMonitor.transmissionType">
              <el-select v-model="dialogForm.fileMonitor.transmissionType">
                <el-option
                  v-for="item in staticData.transmissionTypeList"
                  :label="item.label"
                  :key="item.id"
                  :value="item.id"
                  :disabled="type == 3"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="匹配规则类型：" prop="fileMonitor.matchRuleType">
              <el-select v-model="dialogForm.fileMonitor.matchRuleType">
                <el-option
                  v-for="item in staticData.matchRuleTypeList"
                  :label="item.label"
                  :key="item.id"
                  :value="item.id"
                  :disabled="type == 3"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="16" v-if="this.dialogForm.fileMonitor.matchRuleType == '2'">
            <el-form-item
              label="文件匹配规则："
              prop="fileMonitor.fileRule"
              :rules="[{ required: true, message: '文件匹配规则不能为空', trigger: 'blur' }]">
              <el-tooltip class="item" effect="dark" style="size: 16px" content="文件过滤条件多个用英文分号‘ ; ’分隔(表示即符合又符合的正则表达式)。" placement="top-start">
                <el-input v-model="dialogForm.fileMonitor.fileRule"  placeholder="请输入文件过滤条件多个用英文分号‘;’分隔(表示即符合又符合的正则表达式)。" :disabled="type == 3"></el-input>
              </el-tooltip>
            </el-form-item>
          </el-col>
          <el-col :span="16" v-else>
            <el-form-item
              label="文件匹配规则："
              prop="fileMonitor.fileRule"
              :rules="[{ required: true, message: '文件匹配规则不能为空', trigger: 'blur' }]">
              <el-tooltip class="item" effect="dark" style="size: 16px" content="文件过滤条件多个用英文分号‘ ; ’分隔(表示即包含又包含,字符前加英文感叹号‘ ! ’表示不包含)。例如：KH;!QT;.bin 表示包含KH和.bin且不包含QT的文件，若包含时间可用'#{}'如#{yesterday}表示文件名包含昨天的时间.yesterday-昨天;today:当天;month:当月;lastmonth:上个月" placement="top-start">
                <el-input v-model="dialogForm.fileMonitor.fileRule"  placeholder="请输入文件过滤条件多个用英文分号‘;’分隔(表示即包含又包含,字符前加英文‘!’表示不包含)。例如：KH;!QT;.bin" :disabled="type == 3"></el-input>
              </el-tooltip>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="是否删除源文件："
              prop="fileMonitor.isDeleteFile">
              <el-select v-model="dialogForm.fileMonitor.isDeleteFile">
                <el-option
                  v-for="item in staticData.isDeleteFileList"
                  :label="item.label"
                  :key="item.id"
                  :value="item.id"
                  :disabled="type == 3"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="4">
              <el-form-item
              label="来源配置信息"
              prop=""><el-button icon="el-icon-search" v-loading="loadingCheckSrc" @click="connectClickSrc('src')">连通性检测</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="20">
              <el-form-item
              label=""
              prop=""><hr>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="认证方式:" prop="configMessageSrc.authType">
              <el-select v-model="dialogForm.configMessageSrc.authType" @change="authTypeSrcChange()">
                <el-option
                  v-for="item in staticData.authTypeList"
                  :label="item.label"
                  :key="item.id"
                  :value="item.id"
                  :disabled="type == 3"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="来源路径："
              prop="configMessageSrc.sourcePath"
              :rules="[{ required: true, validator:pathCheck, trigger: 'blur' }]">
              <el-tooltip class="item" effect="dark" style="size: 16px" content="路径以'/'开头，若包含时间可用'#{}'如#{yesterday}表示文件名包含昨天的时间.yesterday-昨天;today:当天;month:当月;lastmonth:上个月" placement="top-start">
                <el-input v-model="dialogForm.configMessageSrc.sourcePath"  placeholder="路径以'/'开头，若包含时间可用'#{}'如#{yesterday}表示文件名包含昨天的时间.yesterday-昨天;today:当天;month:当月;lastmonth:上个月" :disabled="type == 3"></el-input>
              </el-tooltip>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="IP地址："
              prop="configMessageSrc.host"
              :rules="[{ required: true, validator: ipCheck, trigger: 'blur' }]">
              <el-input v-model="dialogForm.configMessageSrc.host" :disabled="type == 3"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="端口："
              prop="configMessageSrc.port"
              :rules="[{ required: true, validator: portCheck, trigger: 'blur' }]">
              <el-input v-model="dialogForm.configMessageSrc.port" :disabled="type == 3"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="用户名："
              prop="configMessageSrc.userName"
              :rules="[{ required: true, message: '用户名不能为空', trigger: 'blur' }]">
              <el-input v-model="dialogForm.configMessageSrc.userName" :disabled="type == 3"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="dialogForm.configMessageSrc.authType == 1">
            <el-form-item
              label="密码/钥："
              prop="configMessageSrc.password"
              :rules="[{ required: true, message: '密码/钥不能为空', trigger: 'blur' }]">
              <el-input v-model="dialogForm.configMessageSrc.password" :disabled="type == 3"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="dialogForm.configMessageSrc.authType == 2">
            <el-form-item
              label="密码/钥："
              prop="configMessageSrc.privateKey"
              :rules="[{ required: true, message: '密码/钥不能为空', trigger: 'blur' }]">
              <el-input v-model="dialogForm.configMessageSrc.privateKey" :disabled="type == 3"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
            <el-col :span="4">
                <el-form-item
                label="目的配置信息"
                prop=""><el-button icon="el-icon-search" v-loading="loadingCheckDst" @click="connectClickDst('dst')">连通性检测</el-button>
              </el-form-item>
            </el-col>
            <el-col :span="20">
                <el-form-item
                label=""
                prop=""><hr>
              </el-form-item>
            </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="认证方式:" prop="configMessageDst.authType">
              <el-select v-model="dialogForm.configMessageDst.authType" @change="authTypeDstChange()">
                <el-option
                  v-for="item in staticData.authTypeList"
                  :label="item.label"
                  :key="item.id"
                  :value="item.id"
                  :disabled="type == 3"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="目的路径："
              prop="configMessageDst.targetPath"
              :rules="[{ required: true, validator:pathCheck, trigger: 'blur' }]">
              <el-tooltip class="item" effect="dark" style="size: 16px" content="路径以'/'开头，若包含时间可用'#{}'如#{yesterday}表示文件名包含昨天的时间.yesterday-昨天;today:当天;month:当月;lastmonth:上个月" placement="top-start">
                <el-input v-model="dialogForm.configMessageDst.targetPath"  placeholder="路径以'/'开头，若包含时间可用'#{}'如#{yesterday}表示文件名包含昨天的时间.yesterday-昨天;today:当天;month:当月;lastmonth:上个月" :disabled="type == 3"></el-input>
              </el-tooltip>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="IP地址："
              prop="configMessageDst.host"
              :rules="[{ required: true,validator: ipCheck, trigger: 'blur' }]">
              <el-input v-model="dialogForm.configMessageDst.host" :disabled="type == 3"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="端口："
              prop="configMessageDst.port"
              :rules="[{ required: true,validator: portCheck, trigger: 'blur' }]">
              <el-input v-model="dialogForm.configMessageDst.port" :disabled="type == 3"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="用户名："
              prop="configMessageDst.userName"
              :rules="[{ required: true, message: '用户名不能为空', trigger: 'blur' }]">
              <el-input v-model="dialogForm.configMessageDst.userName" :disabled="type == 3"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="dialogForm.configMessageDst.authType == 1">
            <el-form-item
              label="密码/钥："
              prop="configMessageDst.password"
              :rules="[{ required: true, message: '密码/钥不能为空', trigger: 'blur' }]">
              <el-input v-model="dialogForm.configMessageDst.password" :disabled="type == 3"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="dialogForm.configMessageDst.authType == 2">
            <el-form-item
              label="密码/钥："
              prop="configMessageDst.privateKey"
              :rules="[{ required: true, message: '密码/钥不能为空', trigger: 'blur' }]">
              <el-input v-model="dialogForm.configMessageDst.privateKey" :disabled="type == 3"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeAdd" v-show="type==1 || type==2">取消</el-button>
        <el-button @click="closeAdd" v-show="type==3">关闭</el-button>
        <el-button type="primary" @click="submitDialogForm" v-loading="loadingSubmit" v-show="type==1">提交</el-button>
        <el-button type="primary" @click="modifyDialogForm" v-loading="loadingSubmit" v-show="type==2">修改</el-button>
      </span>

    </manager-dialog>

    <manager-dialog 
    :title="'文件记录日志'"
    :visible.sync="reportFileRecordLogDialog">
      <el-row>
        <el-form :model="recordMap.reportFileRecordMap"
                label-width="150px"
                :label-position="labelPosition">
          <div class="searchHeader">
            <span>查询条件</span>
            <div class="searchBox">
              <el-row>
                   <div style="margin-left:70%;margin-bottom:10px">
                       <el-rule-button>
                        <el-button icon="el-icon-search" @click="searchReportFileRecord">查询</el-button>
                      </el-rule-button>
                       <el-button @click="resetForm2">
                        <icon name="reply"></icon>重置
                      </el-button>
                      
                      <el-rule-button>
                        <el-button icon="el-icon-folder" v-loading="backPassLoading" @click="backPass">补传</el-button>
                      </el-rule-button>
                    </div>
              </el-row>
            </div>

          </div>
          <div class="searchBox">
          <el-row class="searchBar">
            <el-col :span="12">
              <el-form-item label="文件名：" prop="fileName">
                <el-input
                  v-model="recordMap.reportFileRecordMap.fileName"
                  placeholder="请输入"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="是否上传：" prop="uploadFlag">
                <el-select v-model="recordMap.reportFileRecordMap.uploadFlag" clearable>
                  <el-option
                    v-for="item in staticData.uploadFlagList"
                    :label="item.label"
                    :key="item.id"
                    :value="item.id"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row class="searchBar">
            <el-col :span="24">
              <el-form-item label="日志时间：" prop="createTime">
                <el-date-picker
                  v-model="recordMap.reportFileRecordMap.createTime"
                  type="datetimerange"
                  format="yyyy-MM-dd HH:mm:ss"
                  style="width:100%"
                  @change="datetimerange2" clearable
                ></el-date-picker>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>
      <el-row>
        <el-table :data="recordTableData"  v-loading="recordLoading">
          <el-table-column prop="fileName" label="文件名" show-overflow-tooltip></el-table-column>
          <el-table-column prop="filePath" label="文件路径" show-overflow-tooltip></el-table-column>
          <el-table-column prop="uploadFlag" label="是否上传" show-overflow-tooltip>
            <template
              slot-scope="scope"
            >{{scope.row.uploadFlag | toCase( staticData.uploadFlagList,'id', 'label')}}</template>
          </el-table-column>
          <el-table-column prop="createTime" label="日志时间" show-overflow-tooltip></el-table-column>
        </el-table>
     
        <el-pagination
            @current-change="handleCurrentChildChange"
            :current-page.sync="recordMap.page.current"
            :page-sizes="[5, 10, 20]"
            :page-size="recordMap.page.size"
            :total="recordMap.page.total"
            layout="total,sizes, prev, pager, next,jumper"
        ></el-pagination>
      </el-row>
      </el-row>
      <span slot="footer" class="dialog-footer" style="margin-top: 10px;">
        <el-button @click="closeRecord">关闭</el-button>
      </span>
    </manager-dialog>
  </div>
</template>
<script>
import { title } from '@/settings'
import {  
  getFileMonitorManagementQuery,
  getFileMonitorManagementAdd,
  fileMonitorManagementDel,
  getFileMonitorManagementUpdate,
  getFileMonitorManagementQueryById,
  getFileMonitorIsStart,
  getFileMonitorConnectCheck,
  getNextExecitionTime,
  getFileMonitorLogList,
  getReportFileRecordLogList,
  fileMonitorBackPass } from './api/file_monitor_manage'
export default {
  data() {
    return {
      loading:false,
      loadingCheckSrc:false,
      loadingCheckDst:false,
      loadingSubmit: false,
      page: {
          current: 1,
          size: 10,
          total: 0
        },
      // 查询参数
      queryParams: {
        fileMonitorMap:{
          userSceneName:"",
          transmissionWay:"",
          transmissionType:"",
          isStart:"",
          isSuccess:"",
          failReason:"",
          createTime: null,
          startTime: null,
          endTime: null
        },
        page: {
          current: 1,
          size: 10,
          total: 0
        }
      },
      type: 1,
      nextTime: "",
      defaultV: 4,
      defaultVInner:2,
      labelPosition: "right",
      multipleSelection:[],
      dialogForm:{
        fileMonitor:{
          userSceneName:"",
          executionCycle:"",
          execitionTime:"",
          transmissionWay:"1",
          transmissionType:"1",
          // filePath:"",
          fileRule:"",
          isDeleteFile:"1",
          // isDeleteTempFile:"1",
          // isStart:"0",
          matchRuleType:"1",
        },
        configMessageSrc:{
          authType:"1",
          host:"",
          password:"",
          userName:"",
          password:"",
          privateKey:"",
          sourcePath:"",
          targetPath:"",
        },
        configMessageDst:{
          authType:"1",
          host:"",
          password:"",
          userName:"",
          password:"",
          privateKey:"",
          sourcePath:"",
          targetPath:"",
        }
      },
      addnewDialog: false,
      reportFileRecordLogDialog: false,
      tableData:[],
      expandedRow: null,
      deleteData: {
        ids: []
      },
      titleText: '',
      logLoading: false,
      recordLoading:false,
      backPassLoading:false,
      recordTableData:[],
      recordMap:{
        reportFileRecordMap:{
          logId: null,
          uploadFlag: "",
          fileName: null,
          createTime: null,
        },
        page:{
          size:5,
          current:1,
          total:0
        }
      },
      startMap:{
        id: null,
        isStart: ""
      },
      menus: [
        {
          text: "查看",
          command: "detail",
          icon: 'el-icon-s-order'
        },
        {
          text: "修改",
          command: "modify",
          icon: 'el-icon-edit'
        },
        {
          text: "启动",
          command: "enable",
          icon: 'el-icon-check',
          visible: true
        },
        {
          text: "停止",
          command: "stop",
          icon: 'el-icon-close',
          visible: true
        },
        {
          text: "删除",
          command: "delete",
          icon: 'el-icon-delete',
        },
      ],
      menus2: [
        {
          text: "日志",
          command: "detail_log",
          icon: "el-icon-s-cooperation"
        },
      ],
      // 查询条件
      queryItems: [
        {
          type: 'input',
          key: 'userSceneName',
          span: 12,
          label: '使用场景名称'
        },
        {
          type: 'select',
          span: 12,
          key: 'transmissionWay',
          label: '传输方式',
          option: 'transmissionWayList',
          optionLabel: 'label',
          optionValue: 'id'
        },
        {
          type: 'select',
          span: 12,
          key: 'transmissionType',
          label: '传输方式',
          option: 'transmissionTypeList',
          optionLabel: 'label',
          optionValue: 'id'
        },
        {
          type: 'select',
          span: 12,
          key: 'isStart',
          label: '是否开启',
          option: 'isStartList',
          optionLabel: 'label',
          optionValue: 'id'
        },
        {
          type: 'select',
          span: 12,
          key: 'isSuccess',
          label: '是否成功',
          option: 'isSuccessList',
          optionLabel: 'label',
          optionValue: 'id'
        },
        {
          type: 'select',
          span: 12,
          key: 'failReason',
          label: '是否成功',
          option: 'failReasonList',
          optionLabel: 'label',
          optionValue: 'id'
        },         
        {
          dataType: 'datetimerange',
          type: 'datetime',
          key: 'createTime',
          span: 12,
          label: '创建时间',
          format: 'yyyy-MM-dd HH:mm:ss',
          valueFormat: 'yyyy-MM-dd HH:mm:ss'
        },
      ],
      //列
      columnItems: [
        {
          type: 'selection',
          width: 50
        },
        {
          _key: 'expand',
          type:'expand',
          render: true
        },
        {
          label: '使用场景名称',
          prop: 'userSceneName'
        },
        {
          label: '执行时间',
          prop: 'execitionTime'
        },
        {
          _key: 'transmissionWay',
          label: '传输方式',
          prop: 'transmissionWay',
          render: true,
        },
        {
          _key: 'transmissionType',
          label: '传输类型',
          prop: 'transmissionType',
          render: true
        },
        {
          label: '文件匹配规则',
          prop: 'fileRule'
        },
        {
          _key: 'isDeleteFile',
          label: '是否删除源文件',
          prop: 'isDeleteFile',
          render: true
        },
        {
          _key: 'isStart',
          label: '是否启动',
          prop: 'isStart',
          render: true
        },
        {
          label: '创建时间',
          prop: 'createTime'
        },
        {
          _key: 'action',
          prop: 'action',
          fixed: 'right',
          label: '操作',
          render: true,
          width: 150
        }
      ],
      tableData: [],
      command: '',
      row: null, // 行内数据
      reportId: null,
      deleteData: {
        ids: []
      }
    }
  },
  watch: {
    type: {
      handler(newVal, oldVal) {
          if(this.type == 1){
            this.titleText = "创建任务"
          }else if(this.type == 2){
            this.titleText = "修改任务"
          }else{
            this.titleText = "查看任务"
          }
      },
      immediate: true
    }

  },
  computed: {
    showAddBtn() {
      // auditReportConfig_del 删除权限
      // return checkRuleCodeExit('auditReportConfig_add')
      return true;
    },
    staticData() {
      return {
        executionCycleList:[
          {
            id: "0",
            label: "按年"
          },
          {
            id: "1",
            label: "按月"
          },
          {
            id: "2",
            label: "按周"
          },
          {
            id: "3",
            label: "按日"
          },
          {
            id: "4",
            label: "小时"
          },
          {
            id: "5",
            label: "分钟"
          }
        ],
        transmissionWayList:[
          {
            id: "1",
            label: "sftp"
          },
          {
            id: "2",
            label: "ftp"
          },
        ],
        authTypeList:[
          {
            id: "1",
            label: "密码认证"
          },
          {
            id: "2",
            label: "秘钥认证"
          },
        ],
        transmissionTypeList:[
          {
            id: "1",
            label: "文件上传"
          },
          {
            id: "2",
            label: "文件下载"
          },
        ],
        isDeleteFileList:[
          {
            id: "1",
            label: "是"
          },
          {
            id: "0",
            label: "否"
          },
        ],
        isStartList:[
          {
            id: "1",
            label: "是"
          },
          {
            id: "0",
            label: "否"
          },
        ],
        matchRuleTypeList:[
          {
            id: "1",
            label: "字符串匹配"
          },
          // {
          //   id: "2",
          //   label: "正则匹配"
          // },
        ],
        isSuccessList:[
          {
            id: "1",
            label: "失败"
          },
          {
            id: "2",
            label: "成功"
          },
          {
            id: "3",
            label: "其他"
          },
        ],
        uploadFlagList:[
          {
            id: "0",
            label: "sftp-待上传"
          },
          {
            id: "1",
            label: "sftp-上传中"
          },
          {
            id: "2",
            label: "sftp-成功"
          },
          {
            id: "3",
            label: "ftp-待上传"
          },
          {
            id: "4",
            label: "ftp-上传中"
          },
          {
            id: "5",
            label: "ftp-成功"
          },
        ],
        failReasonList:[
          {
            id: "1",
            label: "sftp登录异常"
          },
          {
            id: "2",
            label: "ftp登录异常"
          },
          {
            id: "3",
            label: "文件上传数量为0"
          },
          {
            id: "4",
            label: "文件上传缺失，上传数量小于实际数量"
          },
          {
            id: "5",
            label: "无符合规则的文件"
          },
        ],
      }
    },
  },
  created() {
    this.search()
  },
  methods: {
    handleExpand(row, expandes) {
      console.log(row, expandes)
    },
    ipCheck: function (rule, value, callback) {
      if (!value) {
        callback(new Error("IP地址不能为空"));
      } else {
        var regIpv4 = /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
        if (!regIpv4.test(value)) {
          callback("IP地址格式错误");
        } else {
          callback();
        }
      }
    },
    portCheck: function (rule, value, callback) {
      if (!value) {
        callback(new Error("端口不能为空"));
      }
      var reg = /^([0-9]|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{4}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$/;
      if (!reg.test(value)) {
        callback("端口格式错误");
      } else {
        callback();
      }
    },
    pathCheck: function (rule, value, callback) {
      if (!value) {
        callback(new Error("路径不能为空"));
      }
      if(value.length>0){
        var str = value.substring(0,1);
        if(str!="/"){
          callback("路径必须以'/'开头");
        } else {
          callback();
        }
      }
    },
    cronCheck: function (rule, value, callback) {
      if(!value){
        callback("Cron表达式不能为空！");
      }
      let parser=require('cron-parser');
      try{
        let interval=parser.parseExpression(value);
      } catch (e) {
        callback("非Cron表达式格式，请检查！"+e.message);
      }
      callback();
    },
    clickRow(command, scope) {
      //点击上下菜单
      if (command === "delete") {
        let index = scope.row.monitorId;
        let list = [];
        list.push(index);
        this.deleteData.ids = list;
        this.confirmDel();
      } else if (command === "modify") {
        this.type = 2;
        this.backFill(scope.row);
      } else if (command === "detail") {
        this.type = 3;
        this.backFill(scope.row);
      } else if (command === "enable") {
        this.startMap.id = scope.row.monitorId;
        this.startMap.isStart = "1";
        this.getEnableOrStop();
      } else if (command === "stop") {
        this.startMap.id = scope.row.monitorId;
        this.startMap.isStart = "0";
        this.getEnableOrStop();
      }
    },
    clickRow2(command, scope, index, rows) {
      //点击上下菜单
      if (command === "detail_log") {
        index = scope.row.logId;
        this.tempLogId = index;
        this.getReportFileRecordLog(index);
        this.reportFileRecordLogDialog = true;
      }
    },
    addPage(command, row) {
      this.command = 'add'
      this.addnewDialog = true;
      // this.$refs.dialog.dialogVisible = true
    },
    handleDelete(ids) {
      this.deleteData.ids = ids
      reportDel(this.deleteData).then((resp) => {
        if (resp.data.returnCode == '0') {
          this.page.current = 1
          this.$message.success('删除成功')
          this.search()
        }
      })
    },
    searchClick() {
      this.search()
    },
    reset() {
      Object.assign(this.$data, this.$options.data())
      this.search()
    },
    search() {
      this.queryParams.fileMonitorMap.startTime = this.queryParams.fileMonitorMap.createTime ? this.queryParams.fileMonitorMap.createTime[0] : this.queryParams.fileMonitorMap.createTime
      this.queryParams.fileMonitorMap.endTime = this.queryParams.fileMonitorMap.createTime ? this.queryParams.fileMonitorMap.createTime[1] : this.queryParams.fileMonitorMap.createTime
      var params = JSON.parse(JSON.stringify(this.queryParams));
      this.loading = true;
      getFileMonitorManagementQuery(params).then(
        resp => {
          if (resp.data.returnCode == "0") {
            
          this.tableData = resp.data.data.list.map(item=> {
              item.list = []
              return item
            })
          const { current, size, total } = resp.data.data.page
          this.page.current = current
          this.page.size = size
          this.page.total = total
          }
          this.loading = false;
        },
        error => {
          this.$message.error(error.message);
          this.loading = false;
        }
      );

    },
    //回填数据
    backFill: function(row) {
      getFileMonitorManagementQueryById(row.monitorId).then(
        resp => {
          if (resp.data.returnCode == "0") {
            this.dialogForm = JSON.parse(JSON.stringify(resp.data.data));
          }
        },
        error => {
          this.$message.error(error.message);
        }
      );
      this.nextTime = "",
      this.addnewDialog = true;
    },
     //重置查询条件
    resetForm() {
      this.queryParams.fileMonitorMap.userSceneName = "";
      this.queryParams.fileMonitorMap.transmissionWay = "";
      this.queryParams.fileMonitorMap.transmissionType = "";
      this.queryParams.fileMonitorMap.isStart = "";
      this.queryParams.fileMonitorMap.isSuccess = "";
      this.queryParams.fileMonitorMap.failReason = "";
      this.queryParams.fileMonitorMap.createTime = "";
      this.queryParams.fileMonitorMap.startTime = null;
      this.queryParams.fileMonitorMap.endTime = null;
      this.queryParams.page.current = 1;
      this.search();
    },
    resetForm2() {
      this.recordMap.reportFileRecordMap.fileName = "";
      this.recordMap.reportFileRecordMap.uploadFlag = "";
      this.recordMap.reportFileRecordMap.createTime = "";
      this.recordMap.reportFileRecordMap.startTime = null;
      this.recordMap.reportFileRecordMap.endTime = null;
      this.recordMap.page.current = 1;
      this.searchReportFileRecord();
    },
    datetimerange: function(value) {
      if(value){
        var dataArr = value.split("-"),
        before = dataArr.splice(0, 3),after = dataArr;
        this.queryParams.fileMonitorMap.startTime = before.join("-").trim();
        this.queryParams.fileMonitorMap.endTime = after.join("-").trim();
      }else{
        this.queryParams.fileMonitorMap.startTime = null;
        this.queryParams.fileMonitorMap.endTime =  null;
      }
    },
    datetimerange2: function(value) {
      if(value){
        var dataArr = value.split("-"),
        before = dataArr.splice(0, 3),after = dataArr;
        this.recordMap.reportFileRecordMap.startTime = before.join("-").trim();
        this.recordMap.reportFileRecordMap.endTime = after.join("-").trim();
      }else{
        this.recordMap.reportFileRecordMap.startTime = null;
        this.recordMap.reportFileRecordMap.endTime =  null;
      }
    },
    authTypeSrcChange(){
      if(this.dialogForm.configMessageSrc.authType=='1'){
        this.dialogForm.configMessageSrc.privateKey = "";
      }else{
        this.dialogForm.configMessageSrc.password = "";
      }
    },
    authTypeDstChange(){
      if(this.dialogForm.configMessageDst.authType=='1'){
        this.dialogForm.configMessageDst.privateKey = "";
      }else{
        this.dialogForm.configMessageDst.password = "";
      }
    },
    //打开新增窗口
    openAddNewDialog: function(command, row) {
      this.type = 1;
      this.nextTime = "",
      this.addnewDialog = true;
      this.resetDialogForm();
    },
    //新增
    submitDialogForm() {
      // this.loadingSubmit = true;
      this.$refs["dialogForm"].validate(valid => {
        if (valid) {
          var form = JSON.parse(JSON.stringify(this.dialogForm));          
          getFileMonitorManagementAdd(form).then(
            resp => {
              if (resp.data.returnCode == "0") {
                this.addnewDialog = false;
                this.$message({
                  message: "新增成功!",
                  type: "success"
                });
                this.search();
              }
              this.loadingSubmit = false;
            },
            error => {
              this.loadingSubmit = false;
              this.$message.error(error.message);
            }
          );
        } else {
          this.loadingSubmit = false;
          return false;
        }
      });
    },
    //修改
    modifyDialogForm: function() {
      this.loadingSubmit = true;
      this.$refs["dialogForm"].validate(valid => {
        if (valid) {
          var form = JSON.parse(JSON.stringify(this.dialogForm));
          getFileMonitorManagementUpdate(this.dialogForm).then(
            resp => {
              if (resp.data.returnCode == "0") {
                this.addnewDialog = false;
                this.search();
                this.$message({
                  message: "修改成功!",
                  type: "success"
                });
              }
              this.loadingSubmit = false;
            },
            error => {
              this.loadingSubmit = false;
              this.$message.error(error.message);
            }
          );
        }else{
          this.loadingSubmit = false;
        }
      });
    },
    connectClickSrc(source){
      this.loadingCheckSrc = true;
      this.connectCheck(source);
    },
    connectClickDst(source){
      this.loadingCheckDst = true;
      this.connectCheck(source);
    },
    connectCheck(source){
      let param;
      if(source == "src"){
        param = {
          transmissionWay:this.dialogForm.fileMonitor.transmissionWay,
          configMessage:this.dialogForm.configMessageSrc
        };
      }else{
        param = {
          transmissionWay:this.dialogForm.fileMonitor.transmissionWay,
          configMessage:this.dialogForm.configMessageDst
        };
      }
      getFileMonitorConnectCheck(param).then(
        resp => {
          if (resp.data.returnCode == "0") {
            if(source == "src"){
              this.$message({
                message: "来源配置连接成功!",
                type: "success"
              });
              this.loadingCheckSrc = false;
            }else{
              this.$message({
                message: "目的配置连接成功!",
                type: "success"
              });
              this.loadingCheckDst = false;
            }
          }else{
            if(source == "src"){
              this.loadingCheckSrc = false;
            }else{
              this.loadingCheckDst = false;
            }
            this.$message.error(resp.data.message);
          }
        },
        error => {
          if(source == "src"){
            this.loadingCheckSrc = false;
          }else{
            this.loadingCheckDst = false;
          }
          this.$message.error(error.message);
        }
      );
    },
    nextExecitionTime(){
      this.nextTime = "";
      let time = this.dialogForm.fileMonitor.execitionTime; 
      if(time !=null && time != ""){
        let param = {
          cron : time
        };
        getNextExecitionTime(param).then(
          resp => {
            if (resp.data.returnCode == "0") {
              this.nextTime = resp.data.data
            }
          },
          error => {
            this.$message.error(error.message);
          }
        );
      }else{
        this.$message.error("请输入执行时间");
      }
    },
    resetDialogForm(done) {
      this.dialogForm = {
        fileMonitor:{
          userSceneName:"",
          executionCycle:"",
          execitionTime:"",
          transmissionWay:"1",
          transmissionType:"1",
          // filePath:"",
          fileRule:"",
          isDeleteFile:"1",
          // isDeleteTempFile:"1",
          // isStart:"0",
          matchRuleType:"1",
        },
        configMessageSrc:{
          authType:"1",
          host:"",
          password:"",
          userName:"",
          password:"",
          privateKey:"",
          sourcePath:"",
          targetPath:"",
        },
        configMessageDst:{
          authType:"1",
          host:"",
          password:"",
          userName:"",
          password:"",
          privateKey:"",
          sourcePath:"",
          targetPath:"",
        },
      };
    },
    //分页
    handleCurrentChange: function(current) {
      this.queryParams.page.current = current;
      this.search();
    },
    handleSizeChange: function(size) {
      this.queryParams.page.size = size;
      this.search();
    },
    //多选
    handleSelectionChange: function(val) {
      this.multipleSelection = val;
    },
    //拼装批量删除数据
    batchdel: function() {
      let list = [];
      for (let temp in this.multipleSelection) {
        list.push(this.multipleSelection[temp].monitorId);
      }
      if (list.length >= 1) {
        this.deleteData.ids = list;
        this.confirmDel();
      } else {
        this.$message.warning("未选择行");
      }
    },
    //删除前确认
    confirmDel() {
      this.$confirm("此操作将永久删除该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          fileMonitorManagementDel(this.deleteData).then(
            resp => {
              if (resp.data.returnCode == "0") {
                this.$message.success("删除成功!");
                this.deleteData.ids = [];
                this.search();
              }
            },
            error => {
              this.$message.error(error.message);
            }
          );
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    closeAdd() {
      this.$refs.dialogForm.resetFields();
      this.nextTime = "";
      this.addnewDialog = false;
      this.search();
    },
    closeRecord(){
      this.reportFileRecordLogDialog=false;
      this.resetForm2();
    },
    //启动停止
    getEnableOrStop:function(){
      this.loading = true;
      let confirm = "";
      if(this.startMap.isStart == "0"){
        confirm = "确认停止";
      }else{
        confirm = "确认启动";
      }
      this.$confirm(confirm, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
      .then(() => { 
        let param = {
          startMap:this.startMap
        };
        getFileMonitorIsStart(param).then(
          resp => {
            if (resp.data.returnCode == "0") {
              this.search();
              if(this.startMap.isStart == "0"){
                this.$message.success("停止成功!");
              }else{
                this.$message.success("启动成功!");
              }
              this.search();
            }
            this.loading = false;
          },
          error => {
            this.loading = false;
            this.$message.error(error.message);
          }
        );
      })
      .catch(() => {
         this.loading = false;
        this.$message({
          type: "info",
          message: "已取消"
        });
      });
    },
   
    //分页
    handleInnerCurrentChange: function (row, currentPage) {
      this.expandedRow = row;
      this.expandedRow.innerPage.current = currentPage;
      this.getFileMonitorLogListById();
    },
    addPaginationParams(row) {
      row.innerPage = {
        current: 1,
        size: 10,
        total: 0
      };
    },
    handleLogInfoExpand(row, expandedRows) {
        this.addPaginationParams(row);
        this.expandedRow = row;
        this.getFileMonitorLogListById();
    },
    getFileMonitorLogListById: function () {
      var params = {
        fileMonitorLogMap: {
          monitorId: this.expandedRow.monitorId,
          isSuccess: this.queryParams.fileMonitorMap.isSuccess,
          failReason: this.queryParams.fileMonitorMap.failReason,
          startTime: this.queryParams.fileMonitorMap.startTime,
          endTime: this.queryParams.fileMonitorMap.endTime,
        },
        page: this.expandedRow.innerPage
      };
      this.tableLoading = true;
      getFileMonitorLogList(params).then(
         resp => {
          if (resp.data.returnCode == "0") {
            this.expandedRow.list = resp.data.data.list;
            this.expandedRow.innerPage = resp.data.data.page;
          }
          this.tableLoading = false;
        },
        error => {
          this.tableLoading = false;
        }
      );
    },
    searchReportFileRecord(){
      this.getReportFileRecordLog(this.tempLogId);
    },
    //文件记录日志表格
    getReportFileRecordLog: function(index) {
      this.tempLogId = index;
      this.recordMap.reportFileRecordMap.logId = index;
      var params = JSON.parse(JSON.stringify(this.recordMap));
      this.recordLoading = true;
      getReportFileRecordLogList(params).then(
        resp => {
          if (resp.data.returnCode == "0") {
            this.recordTableData = resp.data.data.list;
            let page = this.recordMap.page;
            page.size = resp.data.data.page.size;
            page.total = resp.data.data.page.total;
            page.current = resp.data.data.page.current;
          }
          this.recordLoading = false;
        },
        error => {
          this.$message.error(error.message);
          this.recordLoading = false;
        }
      );
    },
     handleCurrentChildChange: function(current) {
      this.recordMap.page.current = current;
      this.getReportFileRecordLog(this.tempLogId);
    },
    // handleSizeChildChange: function(size) {
    //   this.recordMap.page.size = size;
    //   this.getReportFileRecordLog(this.tempLogId);
    // },
    backPass(){
      this.backPassLoading = true;
      let params = {
        logLd: this.tempLogId
      }
      fileMonitorBackPass(params).then(
        resp => {
          if (resp.data.returnCode == "0") {
            this.$message.success("补传成功");
            this.getReportFileRecordLog(this.tempLogId);
          }
          this.backPassLoading = false;
        },
        error => {
          this.$message.error(error.message);
          this.backPassLoading = false;
        }
      );
    },
  }
}
</script>
