<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商机名称" prop="boName">
        <el-input
          v-model="queryParams.boName"
          placeholder="请输入商机名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务编码" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入任务编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商机阶段" prop="boStage">
        <el-input
          v-model="queryParams.boStage"
          placeholder="请输入商机阶段"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入任务状态"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人名称" prop="linkmanName">
        <el-input
          v-model="queryParams.linkmanName"
          placeholder="请输入联系人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="销售组织名称" prop="salesOrgName">
        <el-input
          v-model="queryParams.salesOrgName"
          placeholder="请输入销售组织名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="部门名称" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入部门名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="负责人名称" prop="staffName">
        <el-input
          v-model="queryParams.staffName"
          placeholder="请输入负责人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="截止时间" prop="deadlineDate">
        <el-date-picker clearable
          v-model="queryParams.deadlineDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择截止时间">
        </el-date-picker>
      </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>
    <TaskList :key="timer" :source = "'Task'" :bo="queryParams" />
  </div>
</template>


<script>
import { listTask, getTask, delTask, addTask, updateTask } from "@/api/business/spd/bo/task";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import TaskList from '../task/taskList.vue'

export default {
  name: "Task",
  dicts: ['mk_bo_taskstate','mk_bo_tasktype'],
  components: {TaskList},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedMkBoBehavior: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务表格数据
      taskList: [],
      // 行动表格数据
      mkBoBehaviorList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        boName: null,
        boStage: null,
        code: null,
        name: null,
        type: null,
        state: null,
        customer: null,
        customerName: null,
        linkman: null,
        salesOrg: null,
        salesOrgName: null,
        dept: null,
        deptName: null,
        staff: null,
        staffName: null,
        deadlineDate: null,
        content: null,
        tenantId: null,
        revision: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //重新加载子组件参数
      timer: '',
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询任务列表 */
    getList() {
      this.loading = true;
      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,
        boName: null,
        boStage: null,
        code: null,
        name: null,
        type: null,
        state: null,
        customer: null,
        customerName: null,
        linkman: null,
        salesOrg: null,
        salesOrgName: null,
        dept: null,
        deptName: null,
        staff: null,
        staffName: null,
        deadlineDate: null,
        content: null,
        tenantId: null,
        revision: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      this.mkBoBehaviorList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.timer = new Date().getTime();
      // this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      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.open = true;
      this.title = "添加任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getTask(id).then(response => {
        this.form = response.data;
        this.mkBoBehaviorList = response.data.mkBoBehaviorList;
        this.open = true;
        this.title = "修改任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.mkBoBehaviorList = this.mkBoBehaviorList;
          if (this.form.id != null) {
            updateTask(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTask(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除任务编号为"' + ids + '"的数据项？').then(function() {
        return delTask(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
	/** 行动序号 */
    rowMkBoBehaviorIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 行动添加按钮操作 */
    handleAddMkBoBehavior() {
      let obj = {};
      obj.type = "";
      obj.time = "";
      obj.customer = "";
      obj.customerName = "";
      obj.linkman = "";
      obj.linkmanName = "";
      obj.purpose = "";
      obj.result = "";
      obj.assist = "";
      obj.assistContent = "";
      obj.salesOrg = "";
      obj.salesOrgName = "";
      obj.dept = "";
      obj.deptName = "";
      obj.staff = "";
      obj.staffName = "";
      obj.content = "";
      obj.tenantId = "";
      obj.revision = "";
      this.mkBoBehaviorList.push(obj);
    },
    /** 行动删除按钮操作 */
    handleDeleteMkBoBehavior() {
      if (this.checkedMkBoBehavior.length == 0) {
        this.$modal.msgError("请先选择要删除的行动数据");
      } else {
        const mkBoBehaviorList = this.mkBoBehaviorList;
        const checkedMkBoBehavior = this.checkedMkBoBehavior;
        this.mkBoBehaviorList = mkBoBehaviorList.filter(function(item) {
          return checkedMkBoBehavior.indexOf(item.index) == -1
        });
      }
    },
    /** 复选框选中数据 */
    handleMkBoBehaviorSelectionChange(selection) {
      this.checkedMkBoBehavior = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('drp-business/task/export', {
        ...this.queryParams
      }, `task_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
