<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams"  label-width="68px" size="mini">
      <el-form-item class="treeselectForm" label="部门名称" prop="orgIds">
        <tree @treeselectFn="treeselectFn" :treeval="orgIds" ></tree>
      </el-form-item>
      <el-form-item label="待办人" prop="todoUserId">
        <users :userId="queryParams.todoUserId"   @userIdFn="userIdsFn" ></users>
      </el-form-item>
      <el-form-item label="申请人" prop="applyUserId">
        <users :userId="queryParams.applyUserId"   @userIdFn="applyUserFn" ></users>
      </el-form-item>
<!--      <el-form-item label="申请人" prop="applyUserName">-->
<!--        <el-input-->
<!--          v-model="queryParams.applyUserName"-->
<!--          clearable-->
<!--          placeholder="申请人"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="门店编码" prop="storeCode">
        <el-input
          v-model="queryParams.storeCode"
          clearable
          placeholder="门店编码"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          clearable
          placeholder="门店名称"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="流程类型" prop="itemType">
        <el-select v-model="queryParams.itemType" clearable placeholder="请选择流程类型" @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in activityApplyTypeOptions"
            :key="item.key"
            :label="item.name"
            :value="item.key">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="门店类型" prop="storeCategory">
        <el-select v-model="queryParams.storeCategory" clearable placeholder="请选择门店类型" @keyup.enter.native="handleQuery">
          <el-option
            v-for="item in storeCategoryOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="起止时间" prop="datetimerange">
        <el-date-picker
          v-model="datetimerange"
          align="right"
          end-placeholder="结束日期"
          range-separator="至"
          start-placeholder="开始日期"
          style="width: 200px;"
          type="daterange"
          value-format="yyyy-MM-dd"
          @change="datetimeRangeChange">
        </el-date-picker>
      </el-form-item>
      <el-form-item label=" ">
        <el-button icon="el-icon-search" size="mini" type="primary" @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">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange">
      <el-table-column align="center" label="申请人" prop="applyUserName" width="150" :show-overflow-tooltip="true"/>
      <el-table-column align="center" label="门店编码" prop="storeCode" width="110"/>
      <el-table-column align="center" label="门店名称" prop="storeName" />
      <el-table-column align="center" label="门店类型" width="140px">
        <template slot-scope="scope">
          {{ storeCategoryFormatter(scope.row, scope.column) }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="流程类型" prop="modelName" width="200" :show-overflow-tooltip="true"/>
      <el-table-column align="center" label="审批节点" prop="nodeName" width="150"/>
<!--      <el-table-column label="下一级审批人" align="center" prop="todoUserName" width="150"/>-->
      <el-table-column align="center" label="待办人" prop="todoUserName" width="150"/>
      <!--      <el-table-column label="处理人" align="center" prop="handleUserName" width="150" />-->
      <el-table-column align="center" label="任务状态" prop="status" width="100">
        <template slot-scope="scope">
          <span>{{ statusFormat(scope.row, scope.column) }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="申请时间" prop="applyTime" width="100">
        <template slot-scope="scope">
          <span>{{ scope.row.applyTime.split(" ")[0] }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="通知时间" align="center" prop="todoTime" width="180" />-->
      <el-table-column align="center" class-name="small-padding fixed-width" fixed="right" label="操作" width="140">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleUpdate(scope.row)"
          >转办</el-button>
<!--          <apply-after-->
<!--            v-if="scope.row.isHandle"-->
<!--            :row="scope.row"-->
<!--            :taskId="scope.row.taskId"-->
<!--            :taskName="scope.row.taskName"-->
<!--            :type="scope.row.isHandle"-->
<!--            @getList="getList"-->
<!--          ></apply-after>-->
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />
    <el-dialog :title="title" :visible.sync="open" width="1024px" append-to-body>
      <el-descriptions class="margin-top" :column="3"  border>
        <el-descriptions-item label="申请人">
          {{ form.applyUserName }}
        </el-descriptions-item>
        <el-descriptions-item label="门店编码">
          {{form.storeCode}}
        </el-descriptions-item>
        <el-descriptions-item label="门店名称">
          {{form.storeName}}
        </el-descriptions-item>
        <el-descriptions-item label="门店类型">
          {{ storeCategoryFormatter(form)  }}
        </el-descriptions-item>
        <el-descriptions-item label="流程类型">
          {{ form.modelName}}
        </el-descriptions-item>
        <el-descriptions-item label="审批节点">
          {{form.nodeName}}
        </el-descriptions-item>
        <el-descriptions-item label="待办人">
          {{form.todoUserName}}
        </el-descriptions-item>
        <el-descriptions-item label="任务状态">
          {{ statusFormat(form) }}
        </el-descriptions-item>
        <el-descriptions-item label="申请时间">
          {{ form.applyTime }}
        </el-descriptions-item>
      </el-descriptions>
      <br>
      <el-form ref="form" :model="form" :rules="rules" label-width="98px">
        <el-form-item label="待办人" prop="delegateToUser">
          <user :userId="form.delegateToUser" approvalNickName=""  @userIdFn="delegateToUserFn" @input="onInput"></user>
        </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 {delegateList, delegateTodoItem} from "@/api/sfa/todo";
import Treeselect from "@riophae/vue-treeselect";
import tree from "@/views/components/sfa/treeselectParent2";
import user from "@/views/components/sfa/username";
import users from "@/views/components/sfa/user";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {listModeler} from "@/api/activiti/modeler";
export default {
  name: "Todo",
  components: { Treeselect,tree,user,users },
  dicts: ['sys_normal_disable', 'sys_user_sex','source'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      orgIds:[],
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      show:true,
      // 总条数
      total: 0,
      // 待办表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      itemType: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isHandle: "0",
        itemType: null,
        storeName: null,
        storeCode: null,
        startTime: "",
        status: "1",
        endTime: ""
      },
      storeCategoryOptions: [],
      activityApplyTypeOptions: [],
      activityApplyFormOptions: [],
      // 表单参数
      form: {
        status:"",
        itemType:"",
        storeCategory:"",
        delegateToUser:"",
      },
      // 表单校验
      rules: {

      },
      datetimerange: [],
    };
  },
  created() {
    this.getDicts("sfa_store_type").then(response => {
      this.storeCategoryOptions = response.data;
    });
    this.getDicts("activity_apply_form").then(response => {
      this.activityApplyFormOptions = response.data;
    });
    // this.getDicts("activity_apply_type").then(response => {
    //   this.activityApplyTypeOptions = response.data;
    // });
    listModeler().then(response=>{
      this.activityApplyTypeOptions=response.rows
    })
    this.getList();
  },
  methods: {
    onInput(){
      this.$forceUpdate();
    },
    treeselectFn(val){
      this.orgIds = val;
    },
    userIdsFn(val){
      this.queryParams.todoUserId=val
    },
    applyUserFn(val){
      this.queryParams.applyUserId=val
    },
    delegateToUserFn(val){
      this.form.delegateToUser=val
    },
    storeCategoryFormatter(row, column) {
      return this.selectDictLabel(this.storeCategoryOptions, row.storeCategory);
    },
    datetimeRangeChange(val) {
      this.$forceUpdate()
      if (val == undefined || val == null) {
        this.datetimerange = [];
        this.queryParams.startTime = '';
        this.queryParams.endTime = '';
      } else {
        this.queryParams.startTime = val[0];
        this.queryParams.endTime = val[1];
      }
    },
    statusFormat(row, column) {
      return this.selectDictLabel(this.activityApplyFormOptions, row.status);
    },
    itemTypeFormat(row, column) {
      return this.selectDictLabels(this.activityApplyTypeOptions, row.itemType);
    },
    /** 查询待办列表 */
    getList() {
      this.loading = true;
      this.queryParams.deptIds=this.orgIds
      delegateList(this.queryParams).then(response => {
        this.loading=false
        if(response.code==200){
          this.taskList = response.rows;
          this.total = response.total;
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        taskId: undefined,
        delegateToUser:"",
        status:"",
        itemType:"",
        storeCategory:"",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.datetimerange = [];
      this.queryParams.startTime = '';
      this.queryParams.endTime = '';
      this.orgIds = [];
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加待办";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.form=row
      this.title = "转办";
    },
    /** 提交按钮 */
    submitForm: function() {
      if(this.form.delegateToUser!=null&&this.form.delegateToUser!=""&&this.form.delegateToUser!=undefined){
        if(this.show){
          this.show=false
          setTimeout(()=>{
            this.show=true
          },2000)
          delegateTodoItem({taskId:this.form.taskId,delegateToUser:this.form.delegateToUser}).then(response => {
            this.show=true
            if(response.code==200){
              this.$modal.msgSuccess("转办成功");
              this.open = false;
              this.getList();
            }
          });
        }
      }else{
        this.$modal.msgWarning("请选择转办人！");
      }
    },
  }
};
</script>
