<template>
  <div class="app-container">
    <el-row>
      <el-col :span="24">
        <!-- 现在什么筛选条件也没有 -->
        <el-button style="float: right">导出报表N/A</el-button>
        <el-button @click="openInsertDialog" style="float: right; margin-right: 15px">添加任务</el-button>
      </el-col>
    </el-row>

    <!-- 列表区域 -->
    <el-table :data="task" style="width: 100%" v-loading="loading">
      <el-table-column type="index" width="40"></el-table-column>
      <el-table-column prop="name" label="任务名称"></el-table-column>
      <el-table-column prop="code" label="任务编码" width="120"></el-table-column>
      <el-table-column label="任务类型" width="80">
        <template slot-scope="scope">{{info[scope.$index].tagName}}</template>
      </el-table-column>
      <el-table-column label="发起人" width="80">
        <template slot-scope="scope">{{info[scope.$index].publisherName}}</template>
      </el-table-column>
      <el-table-column prop="indate" label="发起时间" width="100"></el-table-column>
      <el-table-column prop="planTime" label="计划时间" width="100"></el-table-column>
      <el-table-column label="接收人" width="80">
        <template slot-scope="scope">{{info[scope.$index].recipientName}}</template>
      </el-table-column>
      <el-table-column label="参与人">
        <template slot-scope="scope">{{info[scope.$index].participant.join("、")}}</template>
      </el-table-column>
      <el-table-column prop="workLoad" label="工作量" width="80"></el-table-column>
      <el-table-column prop="payee" label="收款方" width="80"></el-table-column>
      <el-table-column prop="payer" label="付款方" width="80"></el-table-column>
      <el-table-column prop="fee" label="费用" width="80"></el-table-column>
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          <span v-if="scope.row.state == 0">待派发</span>
          <span v-else-if="scope.row.state == 1">已派发</span>
          <span v-else-if="scope.row.state == 2">已接收</span>
          <span v-else-if="scope.row.state == 3">待审核</span>
          <span v-else-if="scope.row.state == 4">未通过</span>
          <span v-else-if="scope.row.state == 5">已完成</span>
          <span v-else-if="scope.row.state == 6">已延期</span>
          <span v-else-if="scope.row.state == 7">已失效</span>
          <span v-else-if="scope.row.state == 8">已删除</span>
          <span v-else>不明</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button type="primary" @click="toDetail(scope.row.id)">详情</el-button>
          <el-button v-if="scope.row.state == 0" @click="openUpdateDialog(scope.$index)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 页码区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageIndex"
      :page-sizes="[20, 50, 100]"
      :page-size="20"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      background
      style="float: right"
    ></el-pagination>

    <!-- 添加和修改任务的dialog -->
    <el-dialog title="任务" :visible.sync="taskFormVisible" width="600px">
      <el-form ref="form1" :model="taskForm" :rules="rules1" label-width="110px">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="taskForm.name" placeholder="请输入任务名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="任务编码" prop="code">
          <el-input v-model="taskForm.code" placeholder="请输入任务编码" clearable></el-input>
        </el-form-item>
        <el-form-item label="任务类型" prop="tagId">
          <!-- 标签 -->
          <el-tag
            class="tag"
            v-for="tag in tags"
            :key="tag.id"
            :type="taskForm.tagId == tag.id? '': 'info'"
            @click="chooseTag(tag.id)"
            :closable="tag.id>7"
            @close="handleClose(tag.id)"
            style="cursor: pointer;"
          >{{tag.name}}</el-tag>
          <!-- 添加和删除标签的地方 -->
          <el-input
            class="input-new-tag"
            v-if="newTagVisible"
            v-model="newTagValue"
            ref="saveTagInput"
            size="small"
            style="width:80px"
            @keyup.enter.native="handleInputConfirm"
            @blur="newTagVisible=false"
          ></el-input>
          <el-button v-else class="button-new-tag" size="small" @click="showInput">新建分类</el-button>
        </el-form-item>
        <el-form-item label="计划时间" prop="planTime">
          <el-date-picker
            v-model="taskForm.planTime"
            type="date"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            placeholder="请选择计划时间"
            style="width: 100%"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="工作量" prop="workload">
          <el-input v-model="taskForm.workload" placeholder="请输入工作量" clearable></el-input>
        </el-form-item>
        <el-form-item label="接收人" prop="recipientId">
          <el-select
            v-model="taskForm.recipientId"
            filterable
            placeholder="请输入接收人"
            style="width: 100%"
          >
            <el-option v-for="item in user" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="参与人" prop="participantIds">
          <el-select
            v-model="taskForm.participantIds"
            multiple
            filterable
            clearable
            placeholder="请输入参与人"
            style="width: 100%"
          >
            <el-option v-for="item in user" :key="item.id" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="收款方" prop="payee">
          <el-input v-model="taskForm.payee" placeholder="请输入收款方" clearable></el-input>
        </el-form-item>
        <el-form-item label="付款方" prop="payer">
          <el-input v-model="taskForm.payer" placeholder="请输入付款方" clearable></el-input>
        </el-form-item>
        <el-form-item label="费用" prop="fee">
          <el-input v-model="taskForm.fee" placeholder="请输入费用" clearable></el-input>
        </el-form-item>
        <el-form-item label="任务内容" prop="content">
          <el-input v-model="taskForm.content" placeholder="请输入任务内容" clearable></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="taskFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submit">提交</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import store from '@/store'
import request from "@/utils/request";
export default {
  data() {
    return {
      task: [],
      info: [],
      taskForm: {
        id: null,
        name: null,
        code: null,
        tagId: null,
        planTime: null,
        workload: null,
        publishId: null,
        recipientId: null,
        participantIds: null,
        payee: null,
        payer: null,
        fee: null,
        content: null,
        state: null,
        delayTime: null
      },
      tags: [],
      user: [],
      taskFormVisible: false,
      newTagVisible: false,
      newTagValue: "",
      loading: false,
      //分页相关
      pageIndex: 1,
      pageSize: 20,
      total: 0,
      rules1: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        code: [{ required: true, message: "请输入编码", trigger: "blur" }],
        tagId: [{ required: true, message: "请选择类型", trigger: "blur" }],
        planTime: [
          { required: true, message: "请选择计划时间", trigger: "blur" }
        ],
        workload: [
          { required: true, message: "请输入工作量", trigger: "blur" }
        ],
        recipientId: [
          { required: true, message: "请选择接收人", trigger: "blur" }
        ],
        payee: [{ required: true, message: "请输入收款方", trigger: "blur" }],
        payer: [{ required: true, message: "请输入付款方", trigger: "blur" }],
        fee: [{ required: true, message: "请输入费用", trigger: "blur" }],
        content: [
          { required: true, message: "请输入任务内容", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    //获取记录
    getTask() {
      this.loading = true;
      request({
        url: "/task/getTaskListByPage",
        method: "post",
        params: {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize
        }
      })
        .then(response => {
          this.task = response.data.page.records;
          this.total = response.data.page.total;
          this.info = response.data.other;
          this.loading = false;
        })
        .catch(error => {
          this.loading = false;
        });
    },

    //提交创建和更新
    submit() {
      this.$refs.form1.validate(valid => {
        if (valid) {
          this.loading = true;
          var form = new FormData();
          form.append("name", this.taskForm.name);
          form.append("code", this.taskForm.code);
          form.append("planTime", this.taskForm.planTime);
          form.append("workLoad", this.taskForm.workload);
          form.append("payer", this.taskForm.payer);
          form.append("payee", this.taskForm.payee);
          form.append("fee", this.taskForm.fee);
          form.append("tagId", this.taskForm.tagId);
          form.append("recipientId", this.taskForm.recipientId);
          form.append("content", this.taskForm.content);
          form.append("publishId", store.getters.token);
          if (this.taskForm.delayTime != null) {
            form.append("delayTime", this.taskForm.delayTime);
          }
          if (this.taskForm.id != null) {
            //新增的情况
            form.append("id", this.taskForm.id);
          }
          this.taskForm.participantIds.forEach(item => {
            form.append("participantIds", item);
          });
          request({
            url: "/task/editTask",
            method: "post",
            data: form
          })
            .then(response => {
              this.$message({
                message: "操作成功",
                type: "success"
              });
              this.getTask();
              this.loading = false;
              this.taskFormVisible = false;
            })
            .catch(error => {
              this.loading = false;
              this.taskFormVisible = false;
            });
        }
      });
    },

    //打开创建窗口
    openInsertDialog() {
      this.taskForm.id = null;
      this.taskForm.name = "";
      this.taskForm.code = "";
      this.taskForm.tagId = null;
      this.taskForm.planTime = null;
      this.taskForm.workload = "";
      this.taskForm.publishId = store.getters.token;
      this.taskForm.recipientId = null;
      this.taskForm.participantIds = [];
      this.taskForm.payee = "";
      this.taskForm.payer = "";
      this.taskForm.fee = "";
      this.taskForm.content = "";
      this.taskForm.state = 0;
      this.taskForm.delayTime = null;
      this.taskFormVisible = true;
    },

    //打开编辑窗口
    openUpdateDialog(index) {
      this.taskForm.id = this.task[index].id;
      this.taskForm.name = this.task[index].name;
      this.taskForm.code = this.task[index].code;
      this.taskForm.tagId = this.task[index].tagId;
      this.taskForm.planTime = this.task[index].planTime;
      this.taskForm.workload = this.task[index].workLoad;
      this.taskForm.publishId = this.task[index].publishId;
      this.taskForm.recipientId = this.task[index].recipientId;
      this.taskForm.participantIds = this.info[index].participantId;
      this.taskForm.payee = this.task[index].payee;
      this.taskForm.payer = this.task[index].payer;
      this.taskForm.fee = this.task[index].fee;
      this.taskForm.content = this.task[index].content;
      this.taskForm.state = this.task[index].state;
      this.taskForm.delayTime = this.task[index].delayTime;
      this.taskFormVisible = true;
    },

    //获取任务种类标签
    getTags() {
      this.loading = true;
      request({
        url: "/task-tags/getTaskTags",
        method: "post",
        params: {
          pageIndex: this.pageIndex,
          pageSize: this.pageSize
        }
      })
        .then(response => {
          this.tags = response.data;
          this.loading = false;
        })
        .catch(error => {
          this.loading = false;
        });
    },

    //获取用户信息
    getUser() {
      request({
        url: "/user/getUser",
        method: "post"
      })
        .then(response => {
          this.user = response.data;
        })
        .catch(error => {});
    },

    //表单内选择标签
    chooseTag(id) {
      this.taskForm.tagId = id;
    },

    //页码规格变更
    handleSizeChange(val) {
      this.pageSize = val;
      this.getTask();
    },

    //页码页数变更
    handleCurrentChange(val) {
      this.pageIndex = val;
      this.getTask();
    },

    //点击开始新增标签
    showInput() {
      this.newTagVisible = true;
      this.newTagValue = "";
      this.$nextTick(_ => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    //确认新增标签
    handleInputConfirm() {
      if (this.newTagValue != "") {
        this.$confirm(
          "确定要添加新分类" + this.newTagValue + "吗",
          "添加标签",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "info"
          }
        )
          .then(() => {
            request({
              url: "/task-tags/insertTaskTag",
              method: "post",
              params: { name: this.newTagValue }
            })
              .then(response => {
                this.$message({
                  type: "success",
                  message: "添加成功!"
                });
                this.getTags();
                this.newTagVisible = false;
                this.newTagValue = "";
              })
              .catch(error => {
                this.newTagVisible = false;
                this.newTagValue = "";
              });
          })
          .catch(() => {
            this.newTagVisible = false;
            this.newTagValue = "";
          });
      }
    },

    //点击删除标签
    handleClose(id) {
      this.$confirm("确定要删除分类吗", "删除分类", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          request({
            url: "/task-tags/deleteTaskTag",
            method: "post",
            params: { id: id }
          })
            .then(response => {
              this.$message({
                type: "success",
                message: "删除成功!"
              });
              //这里要清除选中的和表单中的tag
              this.taskForm.tagId = null;
              this.getTags();
            })
            .catch(error => {});
        })
        .catch(() => {});
    },

    //到详情页面
    toDetail(id) {
      if (id != null) {
        this.$router.push("/task/" + id);
      } else {
        this.$message({
          message: "发生错误",
          type: "error"
        });
      }
    }
  },
  mounted() {
    this.getTask();
    this.getTags();
    this.getUser();
  }
};
</script>

<style scoped>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>

