<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item label="任务编号" prop="taskNo">
        <el-input
          v-model="queryParams.taskNo"
          placeholder="请输入任务编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发布人" prop="swpw">
        <el-input
          v-model="queryParams.swpw"
          placeholder="请输入发布人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标题" prop="taskTitle">
        <el-input
          v-model="queryParams.taskTitle"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地点" prop="taskAddress">
        <el-input
          v-model="queryParams.taskAddress"
          placeholder="请输入任务地点"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="planStartTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.planStartTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择计划应开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="planEndTime">
        <el-date-picker clearable size="small"
                        v-model="queryParams.planEndTime"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="选择计划应结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="主要负责人" prop="belongMain">
        <el-input
          v-model="queryParams.belongMain"
          placeholder="请输入主要负责人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="执行人" prop="executor">
        <el-input
          v-model="queryParams.executor"
          placeholder="请输入执行人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务状态" prop="taskStatus">
        <el-select v-model="queryParams.taskStatus" placeholder="请选择任务状态" clearable size="small">
          <el-option
            v-for="dict in dict.type.yq_task_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </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="['yiqing:yqTask: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="['yiqing:yqTask: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="['yiqing:yqTask: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="['yiqing:yqTask:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="yqTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="任务编号" align="center" prop="taskNo"/>
      <el-table-column label="发布人" align="center" prop="swpw"/>
      <el-table-column label="标题" align="center" prop="taskTitle"/>
      <el-table-column label="排班规则" align="center" prop="ruleId">
        <template slot-scope="scope">
          <div v-for="item in ruleList">
            <span v-if="item.ruleId === scope.row.ruleId">
              {{item.ruleName}}
            </span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="地点" align="center" prop="taskAddress"/>
      <el-table-column label="开始时间" align="center" prop="planStartTime" width="180"/>
      <el-table-column label="结束时间" align="center" prop="planEndTime" width="180"/>
      <el-table-column label="主要负责人" align="center" prop="belongMain">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.belongMain !== undefined " type="success" style="margin: 3px">{{scope.row.belongMain}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="二级负责人" align="center" prop="belongMinors" width="180">
        <template slot-scope="scope">
          <el-tag style="margin: 3px" v-for="(item,index) in scope.row.belongMinors" :key="index">{{item}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="执行人" align="center" prop="executors" width="180">
        <template slot-scope="scope">
          <el-tag type="warning" style="margin: 3px" v-for="(item,index) in scope.row.executors" :key="index">{{item}}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="taskStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.yq_task_status" :value="scope.row.taskStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="实际时间" align="center">
        <el-table-column label="开始" align="center" prop="actualStartTime"/>
        <el-table-column label="结束" align="center" prop="actualEndTime"/>
      </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="['yiqing:yqTask:edit']"
          >编辑/详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-flag"
            @click="publish(scope.row)"
          >发布
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['yiqing:yqTask: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="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item label="任务发布人" prop="swpw">
          <el-input v-model="form.swpw" disabled placeholder="请输入任务发布人"/>
        </el-form-item>
        <el-form-item label="标题" prop="taskTitle">
          <el-input v-model="form.taskTitle" placeholder="请输入任务标题"/>
        </el-form-item>
        <el-form-item label="排班规则" prop="ruleId">
          <el-select v-model="form.ruleId">
            <el-option v-for="item in ruleList"
                       :key="item.ruleId"
                       :value="item.ruleId"
                       :label="item.ruleName"
                       placeholder="请选择排班规则">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地点" prop="taskAddress">
          <el-input v-model="form.taskAddress"
                    type="textarea" placeholder="请输入任务地点"/>
        </el-form-item>
        <el-form-item label="开始时间" prop="planStartTime">
          <el-date-picker clearable size="small"
                          v-model="form.planStartTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择计划应开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="planEndTime">
          <el-date-picker clearable size="small"
                          v-model="form.planEndTime"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择计划应结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务状态" prop="taskStatus">
          <el-select v-model="form.taskStatus" placeholder="请选择任务状态">
            <el-option
              v-for="dict in dict.type.yq_task_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="说明" prop="remark">
          <el-input v-model="form.remark"
                    type="textarea" placeholder="请输入备注(说明)"/>
        </el-form-item>
        <el-form-item v-if="!xyb" label="请选择">
          <el-button type="primary" @click="xybBtn">下一步</el-button>
          <el-button @click="cancel">取 消</el-button>
        </el-form-item>
        <div v-if="xyb">
          <el-form-item label="主要负责人">
            <div>
              <el-button v-if="type === 1" @click="openTheStaffingWindow(form.yqTaskId,'0')">查看</el-button>
              <el-button @click="openFilterIndexWindow(form.yqTaskId,0)">选择</el-button>
            </div>
          </el-form-item>
          <el-form-item label="二级负责人" prop="executor">
            <div>
              <el-button v-if="type === 1" @click="openTheStaffingWindow(form.yqTaskId,'1')">查看</el-button>
              <el-button @click="openFilterIndexWindow(form.yqTaskId,1)">选择</el-button>
            </div>
          </el-form-item>
          <el-form-item label="执行人" prop="executor">
            <div>
              <el-button v-if="type === 1" @click="openTheStaffingWindow(form.yqTaskId,'2')">查看</el-button>
              <el-button @click="openFilterIndexWindow(form.yqTaskId,2)">选择</el-button>
            </div>
          </el-form-item>
        </div>
      </el-form>
      <div v-if="xyb" 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="title" :visible.sync="userListOpen" append-to-body>
      <taskBelong ref="tb" :taskId="taskId" :level="level"/>
    </el-dialog>

    <el-dialog :title="title" :visible.sync="filterIndexOpen" append-to-body width="1200px">
      <filterIndex @getList="getList" ref="fi" :taskId="taskId" :level="belongSubmitType"
                   :fatherOpen="filterIndexOpen"/>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listYqTask,
    getYqTask,
    delYqTask,
    addYqTask,
    updateYqTask,
    exportYqTask,
    listAllRule,
    pushTask
  } from '@/api/yiqing/yqTask'
  import taskBelong from '../taskBelong'
  import filterIndex from '../yiqing'
  import user from "@/store/modules/user";

  export default {
    name: 'YqTask',
    dicts: ['yq_task_status', 'is_delete'],
    components: {
      taskBelong,
      filterIndex
    },
    data() {
      return {
        xyb: false,
        belongSubmitType: 0,
        // 遮罩层
        loading: true,
        // 导出遮罩层
        exportLoading: false,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 疫情防控-任务排班表格数据
        yqTaskList: [],
        ruleList: [],
        // 弹出层标题
        title: '',
        // 是否显示弹出层
        open: false,
        type: 0,
        userListOpen: false,
        filterIndexOpen: false,
        taskId: null,
        level: 0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orgNo:null,
          taskNo: null,
          swpw: null,
          taskTitle: null,
          ruleId: null,
          taskAddress: null,
          planStartTime: null,
          planEndTime: null,
          belongMain: null,
          executor: null,
          taskStatus: null,
          isDelete: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          taskTitle: [
            { required: true, message: '标题不能为空', trigger: 'blur' }
          ],
          taskAddress: [
            { required: true, message: '地点不能为空', trigger: 'blur' }
          ],
          planStartTime: [
            { required: true, message: '请选择开始时间', trigger: 'blur' }
          ],
          planEndTime: [
            { required: true, message: '请选择结束时间', trigger: 'blur' }
          ],
          ruleId: [
            { required: true, message: '请选择规则', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '说明不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询疫情防控-任务排班列表 */
      getList() {
        this.loading = true
        this.filterIndexOpen = false
        this.queryParams.orgNo =  user.state.organ;
        listYqTask(this.queryParams).then(response => {
          this.yqTaskList = response.rows
          this.total = response.total
          this.loading = false
        })
        listAllRule().then(re => {
          this.ruleList = re.data
        })
      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          yqTaskId: null,
          taskNo: null,
          orgNo: null,
          swpw: null,
          taskTitle: null,
          ruleId: null,
          taskAddress: null,
          planStartTime: null,
          planEndTime: null,
          belongMain: null,
          executor: null,
          taskStatus: null,
          isDelete: 0,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null
        }
        this.resetForm('form')
      },
      /*打开人员分配窗口*/
      openFilterIndexWindow(id, level) {
        this.filterIndexOpen = true
        this.taskId = id
        this.belongSubmitType = level
        setTimeout(() => {
          this.$refs.fi.getList()
        }, 200)
      },
      /*打开查看人员窗口*/
      openTheStaffingWindow(id, level) {
        this.userListOpen = true
        this.taskId = id
        this.level = level
        setTimeout(() => {
          this.$refs.tb.getList()
        }, 200)
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm('queryForm')
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.yqTaskId)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.type = 0
        this.xyb = false
        this.open = true
        this.title = '添加任务排班'
        this.form.taskStatus = 0
        this.form.swpw = localStorage.getItem('userName')
      },
      /** 下一步按钮*/
      xybBtn() {
        this.submitForm()
      },
      /** 发布 */
      publish(row) {
        this.reset()
        const yqTaskId = row.yqTaskId || this.ids
        pushTask(yqTaskId).then(() => {
          this.$modal.msgSuccess('发布成功')
          this.getList()
        })
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        this.type = 1
        this.xyb = true
        const yqTaskId = row.yqTaskId || this.ids
        getYqTask(yqTaskId).then(response => {
          this.form = response.data
          this.open = true
          this.title = '修改任务'
        })
      },
      /** 提交按钮 */
      submitForm() {
        this.form.orgNo = user.state.organ;
        console.log(this.form.orgNo)
        this.$refs['form'].validate(valid => {
          if (valid) {
            if (this.form.yqTaskId != null) {
              updateYqTask(this.form).then(() => {
                this.$modal.msgSuccess('修改成功')
                this.open = false
                this.getList()
              })
            } else {
              addYqTask(this.form).then(re => {
                this.$modal.msgSuccess('新增成功')
                this.form = re.data
                this.xyb = true
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const yqTaskIds = row.yqTaskId || this.ids
        this.$modal.confirm('是否确认删除疫情防控-任务排班编号为"' + yqTaskIds + '"的数据项？').then(function() {
          return delYqTask(yqTaskIds)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        }).catch(() => {
        })
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams
        this.$modal.confirm('是否确认导出所有疫情防控-任务排班数据项？').then(() => {
          this.exportLoading = true
          return exportYqTask(queryParams)
        }).then(response => {
          this.$download.name(response.msg)
          this.exportLoading = false
        }).catch(() => {
        })
      }
    }
  }
</script>
