<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入巡检任务标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="执行日期" prop="pushTime">
        <el-date-picker
          v-model="queryParams.soudata"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <!--<el-form-item label="创建人" prop="adminId">-->
        <!--<el-input-->
          <!--v-model="queryParams.adminId"-->
          <!--placeholder="请输入创建人"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@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="['pipetask:task: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="['pipetask:task: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="['pipetask:task: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="['pipetask:task:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane v-for="item in pipeTaskTypeOptions " :label="item.dictLabel" :name="item.remark">
        <HeightTaskTable v-if="activeName == item.remark"/>
      </el-tab-pane>

      <!--<el-tab-pane label="中高压管线任务" name="height">-->
        <!--<HeightTaskTable v-if="activeName == 'height'"/>-->
      <!--</el-tab-pane>-->
      <!--<el-tab-pane label="村内管线任务" name="country">-->
        <!--<CountryTaskTable v-if="activeName == 'country'"/>-->
      <!--</el-tab-pane>-->
    </el-tabs>


    <!-- 添加或修改管线巡线任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1200px" append-to-body destroy-on-close :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="任务标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入巡检任务标题" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="任务类型" prop="pipeTaskType">
              <el-select
                style="width: 100%"
                v-model="form.pipeTaskType"
                @change="pipeTaskTypeChange"
                placeholder="请选择任务类型"
              >
                <el-option
                  v-for="dict in pipeTaskTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="开始日期" prop="planExecuteDate">
              <el-date-picker clearable size="small"
                              v-model="form.planExecuteDate"
                              type="date"
                              :picker-options="pickerOptions"
                              placeholder="选择开始日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行次数" prop="planExecuteNum">
              <el-input v-model="form.planExecuteNum" placeholder="请输入执行次数" type="number" :min="1" :max="10" @input="handleInput"/>
            </el-form-item>
          </el-col>
        </el-row>

        <PointDetailInfo
          v-if="open"
          ref="ponitDetailInfo"
          :pipeType="pipeType"
        />

      </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 { listTask, getTask, delTask, addTask, updateTask, exportTask } from "@/api/pipetask/task";
import PointDetailInfo from "./components/PointDetailInfo";
import HeightTaskTable from "./components/HeightTaskTable";
import CountryTaskTable from "./components/CountryTaskTable";
export default {
  name: "Task",
  components: {
    PointDetailInfo,
    HeightTaskTable,
    CountryTaskTable
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 管线巡线任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        soudata: null,
        begindata: null,
        enddata:null,
      },
      timeOptionRange: null,
      // 日期选择七天内,且当前时间后不能选择
      pickerOptions:{
          // 监听每一次选择日期的事件
          onPick: ({ maxDate, minDate }) => {//最大时间  最小时间
            this.choiceDate = minDate.getTime();//当选一个日期时 就是最小日期
            // // 如何你选择了两个日期了，就把那个变量置空
            if (maxDate) this.choiceDate = ''
          },
          // 设置禁用状态  time是日历上的每一个时间
          disabledDate: time => {
            // 如何选择了一个日期
            if (this.choiceDate) {
              // 7天的时间戳
              const one = 6 * 24 * 3600 * 1000;//这里如果乘以7相当于限制8天以内的  所以乘以6
              // 当前日期 - one = 7天之前
              const minTime = this.choiceDate - one;
              // 当前日期 + one = 7天之后
              const maxTime = this.choiceDate + one;
              return (
                time.getTime() < minTime ||
                time.getTime() > maxTime ||
                // 限制不能选择今天及以后
                time.getTime()  <=  new Date().setHours(0,0,0,0)
              )
            } else {
              // 如果没有选择日期，就要限制不能选择今天及以后
              return time.getTime() <  new Date().setHours(0,0,0,0)
            }
          }
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "任务标题不能为空", trigger: "blur" },
        ],
        pipeTaskType: [
          { required: true, message: "请选择任务类型", trigger: "change" },
        ],
        planExecuteDate: [
          { required: true, message: "请选择执行日期", trigger: "change" },
        ],
        planExecuteNum: [
          { required: true, message: "执行次数不能空", trigger: "change" },
        ],
      },
      activeName:"day",
      pipeTaskTypeOptions: [],
      pipeType: "1"
    };
  },
  created() {
    //this.getList();
    this.getDicts("t_cycle_type").then(response => {
        this.pipeTaskTypeOptions = response.data;
    });
  },
  methods: {
    /** 查询管线巡线任务列表 */
    getList() {
      if(this.queryParams.soudata!=null){
        var data = this.queryParams.soudata;
        this.queryParams.begindata = data[0].getFullYear()+'-'+(data[0].getMonth()+1)+'-'+data[0].getDate();
        this.queryParams.enddata = data[1].getFullYear()+'-'+(data[1].getMonth()+1)+'-'+data[1].getDate();
      }else{
        this.queryParams.begindata = null;
        this.queryParams.enddata = null;
      }
      this.loading = true;
      this.queryParams.pipeTaskType = this.pipeType;
      console.log(this.queryParams)
      listTask(this.queryParams).then(response => {
        this.taskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        pipeTaskType: null,
        pushTime: null,
        adminId: null,
        createTime: null,
        planExecuteDate: null,
        planExecuteNum:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.soudata=null;
      this.queryParams.begindata = '';
      this.queryParams.enddata = '';
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.pipeTaskType = this.pipeType;
      this.open = true;
      this.title = "添加管线巡线任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTask(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) {
            updateTask(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除管线巡线任务名称为"' + row.title + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTask(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有管线巡线任务数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportTask(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    //详情
    handleDetail(row){
      getTask(row.id).then(response => {
        this.$router.push({path:"taskInspectDetail",query:{taskId:row.id,pushTime:row.pushTime}});
      });
    },
    handleClick(tab,event){
      this.activeName = tab.name;
      var model = this.pipeTaskTypeOptions.find(item => item.remark == tab.name);
      this.pipeType = model.dictValue;
    },
    handleInput(value) {
      // 实时限制输入范围
      if (value < 0) {
        this.form.planExecuteNum = 1
      } else if (value > 10) {
        this.form.planExecuteNum = 10
      }
    },
    pipeTaskTypeChange(val){
      this.$refs.ponitDetailInfo.getPointDetail(val);
    }
  }
};
</script>
<style lang="scss" scoped>
  ::v-deep .el-select{
    width: 100%;
  }
  ::v-deep .el-date-editor.el-input, .el-date-editor.el-input__inner {
    width: 100%;
  }
</style>
