<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="任务编码" prop="code">
        <el-input
          v-model="queryParams.code"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务名称" prop="name">
        <el-input
          v-model="queryParams.name"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商机名称" prop="boName">
        <el-input
          v-model="queryParams.boName"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户名称" prop="customerName">
        <el-input
          v-model="queryParams.customerName"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="任务状态" prop="state">
        <el-select
          v-model="queryParams.state"
          clearable
        >
          <el-option
            v-for="dict in dict.type.mk_bo_taskstate"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </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"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->

    <div class="btn_grooup">
      <el-button
        type="primary"
        plain
        icon="el-icon-plus"
        size="mini"
        @click="handleAdd"
        >新增</el-button
      >
    </div>

    <el-table
      v-loading="loading"
      :data="taskList"
      @selection-change="handleSelectionChange"
      height="700px"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column width="150" label="任务编码" align="center" prop="code" />
      <el-table-column width="200" show-overflow-tooltip label="任务名称" align="center" prop="name" />
      <el-table-column
        width="200"
        show-overflow-tooltip
        label="商机名称"
        align="center"
        prop="boName"
      />
      <el-table-column label="商机阶段" align="center" prop="boStageName"/>
      <el-table-column
        show-overflow-tooltip
        label="客户名称"
        align="center"
        prop="customerName"
      />
      <el-table-column label="任务类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.mk_bo_tasktype"
            :value="scope.row.type"
          />
        </template>
      </el-table-column>
      <el-table-column label="任务状态" align="center" prop="state">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.mk_bo_taskstate"
            :value="scope.row.state"
          />
        </template>
      </el-table-column>
      <el-table-column label="联系人" align="center" prop="linkmanName" />
      <el-table-column
        show-overflow-tooltip
        label="销售组织名称"
        align="center"
        prop="salesOrgName"
      />
      <el-table-column label="部门名称" align="center" prop="deptName" />
      <el-table-column label="负责人名称" align="center" prop="staffName" />
      <el-table-column
        label="截止时间"
        align="center"
        prop="deadlineTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deadlineTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="任务内容" align="center" prop="content" />
      <el-table-column
        show-overflow-tooltip
        label="操作"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-promotion"
            @click="handleBrowse(scope.row)"
            >跟进</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="1000px"
      append-to-body
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="80px"
        :disabled="this.operatingState == 'Browse'"
      >
        <el-row>
          <el-col :span="8">
            <el-form-item label="商机名称" prop="boName">
              <el-input
                v-model="form.boName"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item
              label="商机阶段"
              prop="boStage"
            >
              <el-select v-model="form.boStage">
                <el-option
                  v-for="dict in dict.type.mk_bo_stage"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户" prop="customerName">
              <el-input
                v-model="form.customerName"
              >
                <el-button
                  slot="append"
                  icon="el-icon-more"
                  @click="refereCustomer"
                ></el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="8">
            <el-form-item label="任务编码" prop="code">
              <el-input
                v-model="form.code"
                :disabled="true"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="任务名称" prop="name">
              <el-input v-model="form.name" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="任务状态" prop="state">
              <el-select v-model="form.state" >
                <el-option
                  v-for="dict in dict.type.mk_bo_taskstate"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="开始时间" prop="startTime">
              <el-date-picker
                clearable
                v-model="form.startTime"
                type="date"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="截止时间" prop="deadlineTime">
              <el-date-picker
                clearable
                v-model="form.deadlineTime"
                type="date"
                value-format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="任务类型" prop="type">
              <el-select v-model="form.type" >
                <el-option
                  v-for="dict in dict.type.mk_bo_tasktype"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="联系人" prop="linkmanName">
              <el-input v-model="form.linkmanName">
                <el-button
                  slot="append"
                  icon="el-icon-more"
                  @click="refereContact"
                ></el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="销售组织" prop="salesOrgName">
              <el-input v-model="form.salesOrgName" :disabled="true">
                <el-button
                  slot="append"
                  icon="el-icon-more"
                  @click="refereOrg"
                  :disabled="true"
                ></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="部门" prop="deptName">
              <el-input v-model="form.deptName" :disabled="true">
                <el-button
                  slot="append"
                  icon="el-icon-more"
                  @click="refereDept"
                  :disabled="true"
                ></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="负责人" prop="staffName">
              <el-input v-model="form.staffName">
                <el-button
                  slot="append"
                  icon="el-icon-more"
                  @click="refereStaff"
                ></el-button>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="任务内容" prop="content">
          <el-input
            type="textarea"
            :rows="2"
            autosize
            v-model="form.content"
          >
          </el-input>
        </el-form-item>
        
        <div class="md-auditInfo">
          <el-divider content-position="left">其它信息</el-divider>
          <el-form :inline="true" label-position="right" :model="form">
            <el-row>
              <el-col :span="6">
                <el-form-item label="创建人">
                  <el-input
                    v-model="form.createByName"
                    size="small"
                    readonly
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="创建时间">
                  <el-input
                    v-model="form.createTime"
                    size="small"
                    readonly
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="修改人">
                  <el-input
                    v-model="form.updateByName"
                    size="small"
                    readonly
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="修改时间">
                  <el-input
                    v-model="form.updateTime"
                    size="small"
                    readonly
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </el-form>

      <BehaviorList
          :key="timer"
          :source="'TaskList'"
          :bo="this.form"
          :boAuthority="boAuthority"
          v-if="this.operatingState != 'Insert'"
        />
        
      <div slot="footer">
        <el-button
          type="primary"
          @click="submitForm"
          v-if="this.operatingState != 'Browse'"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 客户参照 -->
    <CustomerRef
      ref="customerSelect"
      @doSubmit="customerSelectionsToInput"
      :single="true"
    />
    <!-- 联系人参照 -->
    <ContactRef
      ref="contactSelect"
      @doSubmit="contactSelectionsToInput"
      :single="true"
      :outerQueryParams="this.bo"
    />
    <!-- 组织参照 -->
    <OrgRef ref="orgSelect" @doSubmit="orgSelectionsToInput" :single="true" />
    <!-- 部门参照 -->
    <DeptRef
      ref="deptSelect"
      @doSubmit="deptSelectionsToInput"
      :single="true"
    />
    <!-- 员工参照 -->
    <StaffRef
      ref="staffSelect"
      @doSubmit="staffSelectionsToInput"
      :single="true"
    />
  </div>
</template>


<script>
import { listTask, getTask, delTask, addTask, updateTask } from "@/api/business/spd/bo/task";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import CustomerRef from "@/views/business/spd/bo/refer/customer/index.vue";
import ContactRef from "@/views/business/spd/bo/refer/contact/index.vue";
import OrgRef from "@/views/business/spd/bo/refer/org/index.vue";
import DeptRef from "@/views/business/spd/bo/refer/dept/index.vue";
import StaffRef from "@/views/business/spd/bo/refer/staff/index.vue";
import BehaviorList from "../behavior/behaviorList.vue";
import { getPsnPost } from "@/api/business/spd/bo/authority";

export default {
  name: "Task",
    dicts: ["mk_bo_taskstate", "mk_bo_tasktype", "mk_bo_stage"],
    components: {
      CustomerRef,
      ContactRef,
      OrgRef,
      DeptRef,
      StaffRef,
      BehaviorList,
    },
  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,
        startTime: null,
        deadlineTime: null,
        content: null,
        tenantId: null,
        revision: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      //重新加载子组件参数
      timer: '',
      // 操作撞他
      operatingState:'',
      // 所选商机
      curBo:{},
      // 权限
      boAuthority:{},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询任务列表 */
    getList() {
      this.loading = true;
      this.queryParams.staff = this.$store.state.user.id;
      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,
        deadlineTime: 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.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
    },
    /** 查看按钮操作 */
    handleBrowse(row) {
      this.reset();
      const id = row.id || this.ids;
      getTask(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.operatingState = "Browse";
        this.title = "基础信息";
        //加载权限数据
        getPsnPost(this.form.bo).then((response) => {
          this.boAuthority = response.data;
          this.timer = new Date().getTime();
        });
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加任务";
      this.operatingState = "Insert";
    },
    /** 修改按钮操作 */
    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)
    },
    // 触发客户参照列表
    refereCustomer() {
      this.$refs.customerSelect.init();
    },
    //客户参照列表选择后
    customerSelectionsToInput(selections) {
      this.form.customer = selections[0].id;
      this.form.customerName = selections[0].name;
    },
    // 触发联系人参照列表
    refereContact() {
      this.$refs.contactSelect.init();
    },
    //联系人参照列表选择后
    contactSelectionsToInput(selections) {
      this.form.linkman = selections[0].id;
      this.form.linkmanName = selections[0].name;
    },
    // 触发组织参照列表
    refereOrg() {
      this.$refs.orgSelect.init();
    },
    //组织参照列表选择后
    orgSelectionsToInput(selections) {
      this.form.salesOrg = selections[0].deptId;
      this.form.salesOrgName = selections[0].deptName;
    },
    // 触发部门参照列表
    refereDept() {
      this.$refs.deptSelect.init();
    },
    //部门参照列表选择后
    deptSelectionsToInput(selections) {
      this.form.dept = selections[0].deptId;
      this.form.deptName = selections[0].deptName;
    },
    // 触发员工参照列表
    refereStaff() {
      this.$refs.staffSelect.init();
    },
    //员工参照列表选择后
    staffSelectionsToInput(selections) {
      this.form.staff = selections[0].userId;
      this.form.staffName = selections[0].nickName;
      this.form.dept = selections[0].deptId;
      this.form.deptName = selections[0].deptName;
      this.form.salesOrg = selections[0].orgId;
      this.form.salesOrgName = selections[0].orgName;
    },
  }
};
</script>

<style lang="scss" scoped>
.btn_grooup {
  margin-bottom: 10px;
  display: flex;
  justify-content: flex-end;
}
</style>
