<template>
  <div class="app-container">
    <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="['drp-business:task: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="['drp-business:task: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="['drp-business:task:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="taskList" @selection-change="handleSelectionChange" >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="商机id" align="center" prop="bo" v-if="source == 'Task'"/> -->
      <el-table-column label="商机名称" align="center" prop="boName" v-if="source == 'Task'"/>
      <el-table-column label="商机阶段" align="center" prop="boStage">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mk_bo_stage" :value="scope.row.boStage"/>
        </template>
      </el-table-column>
      <el-table-column label="客户名称" align="center" prop="customerName" v-if="source == 'Task'"/>
      <el-table-column label="任务编码" align="center" prop="code" />
      <el-table-column label="任务名称" align="center" prop="name" />
      <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 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 label="操作" fixed="right" 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="['drp-business:task:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['drp-business:task: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="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="商机名称" prop="boName">
              <el-input v-model="form.boName" placeholder="请输入商机名称"  :disabled="this.source == 'BoDetails'"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="商机阶段" prop="boStage" v-if="source == 'Task'">
              <el-select v-model="form.boStage" placeholder="请输入商机阶段">
                <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" placeholder="请输入客户" :disabled="this.source == 'BoDetails'">
                <el-button slot="append" icon="el-icon-more" @click="refereCustomer" :disabled="this.source == 'BoDetails'"></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" placeholder="系统自动生成编码" :disabled="true"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="任务名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入任务名称" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="任务状态" prop="state">
              <el-select v-model="form.state" placeholder="请输入任务状态">
                <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"
                placeholder="请选开始时间">
              </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"
                placeholder="请选择截止时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="任务类型" prop="type">
              <el-select v-model="form.type" placeholder="请选择任务类型">
                <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"
            placeholder="请输入任务内容"
            autosize
            v-model="form.content">
          </el-input>
        </el-form-item>
        <BehaviorList  :key="timer" :source="'TaskList'" :bo="this.form" v-if="this.operatingState != 'Insert'"/>
        <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.createBy" 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.updateBy" 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>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</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'


export default {
  name: "TaskList",
  props:["source","bo"],
  dicts: ['mk_bo_taskstate','mk_bo_tasktype','mk_bo_stage'],
  components: {CustomerRef,ContactRef,OrgRef,DeptRef,StaffRef,BehaviorList},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 任务表格数据
      taskList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bo: null,
        boStage: null,
        code: null,
        name: null,
        type: null,
        state: null,
        customer: null,
        customerName: null,
        linkman: null,
        linkmanName: 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: {
        customerName: [
          { required: true, message: "客户不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "任务名称不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "任务状态不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "任务类型不能为空", trigger: "blur" }
        ],
        linkmanName: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        staffName: [
          { required: true, message: "负责人不能为空", trigger: "blur" }
        ],
        startTime: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        deadlineTime: [
          { required: true, message: "截止时间不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "任务内容不能为空", trigger: "blur" }
        ],
      },
      //重新加载子组件参数
      timer: '',
      //当前操作状态
      operatingState: '',
    };
  },
  created() {
    if(this.source == 'BoDetails'){
      this.queryParams.bo = this.bo.id;
    }
    if(this.source == 'Task'){
      this.queryParams = this.bo;
    }
    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,
        bo: null,
        boStage: null,
        code: null,
        name: null,
        type: null,
        state: null,
        customer: null,
        customerName: null,
        linkman: null,
        linkmanName: null,
        salesOrg: null,
        salesOrgName: null,
        dept: null,
        deptName: null,
        staff: null,
        staffName: null,
        startTime: null,
        deadlineTime: null,
        content: null,
        tenantId: null,
        revision: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null
      };
      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
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.operatingState = "Insert";
      this.reset();
      if(this.source == 'BoDetails'){
        this.form.bo = this.bo.id;
        this.form.boName = this.bo.boName;
        this.form.boStage = this.bo.boStage;
        this.form.customer = this.bo.customer;
        this.form.customerName = this.bo.customerName;
      }
      this.form.state = '0';
      this.open = true;
      this.title = "添加任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.operatingState = "Update";
      this.reset();
      const id = row.id || this.ids
      getTask(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改任务";
        this.timer = new Date().getTime();
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          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(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('drp-business/task/export', {
        ...this.queryParams
      }, `task_${new Date().getTime()}.xlsx`)
    },
    // 触发客户参照列表
    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) {
      console.log('selections',selections);
      this.form.linkman = selections[0].id;
      this.form.linkmanName = selections[0].name;
      console.log('this.form',this.form);
    },
    // 触发组织参照列表
    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>
