<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 label="序号" type="index" width="50" align="center" fixed/>
      <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 show-overflow-tooltip 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 show-overflow-tooltip label="任务内容" align="center" prop="content" />
      <el-table-column
        width="250"
        label="操作"
        fixed="right"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-search"
            @click="handleBrowse(scope.row)"
            >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-top"
            @click="submitTask(scope.row)"
            v-if="scope.row.state == 0"
            >提交</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-if="scope.row.state == 0"
            >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-if="scope.row.state == 0"
            >删除</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
        size="mini"
        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">
              <dr-popover-select
                v-model="form.boName"
                title="商机选择"
                type="BO_PARAM"
                :dataMapping="{
                  bo: 'id',
                  boName: 'name',
                }"
                :source.sync="form"
              >
              </dr-popover-select>
            </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">
              <dr-popover-select
                v-model="form.customerName"
                title="商机选择"
                type="CUSTOMER_PARAM_ZT"
                :dataMapping="{
                  customer: 'id',
                  customerName: 'name',
                }"
                :source.sync="form"
              >
              </dr-popover-select>
            </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" />
            </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">
              <dr-popover-select
                v-model="form.linkmanName"
                title="联系人选择"
                type="LINKMAN_PARAM"
                :dataMapping="{
                  linkman: 'id',
                  linkmanName: 'name',
                }"
                :source.sync="form"
                :queryParams="additionalCondition"
              >
              </dr-popover-select>
            </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>
        <el-divider content-position="left">其它信息</el-divider>
        <el-row>
          <el-col :span="6">
            <el-form-item label="创建人">
              <el-input
                v-model="form.createByName"
                readonly
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="创建时间">
              <el-input
                v-model="form.createTime"
                readonly
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="修改人">
              <el-input
                v-model="form.updateByName"
                readonly
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="修改时间">
              <el-input
                v-model="form.updateTime"
                readonly
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <BehaviorList
          :key="timer"
          :source="'TaskList'"
          :bo="this.form"
          :boAuthority="boAuthority"
          v-if="this.operatingState == 'Browse'"
        />
        
      <div slot="footer">
        <el-button
          size="mini"
          type="primary"
          @click="submitForm"
          v-if="this.operatingState != 'Browse'"
          >确 定</el-button
        >
        <el-button size="mini" @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 员工参照 -->
    <StaffRef
      ref="staffSelect"
      @doSubmit="staffSelectionsToInput"
      :single="true"
    />
  </div>
</template>


<script>
import { listTask, getTask, delTask, addTask, updateTask,submit } 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,
        code: null,
        name: null,
        state: null,
        customerName: null,
      },
      // 表单参数
      form: {
        bo:"",
        boName:"",
      },
      // 表单校验
      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", "change"] },
        ],
        deadlineTime: [
          { required: true, message: "截止时间不能为空", trigger: ["blur", "change"] },
        ],
        content: [
          { required: true, message: "任务内容不能为空", trigger: "blur" },
        ],
      },
      //重新加载子组件参数
      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.operatingState = "Update";
      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();
            });
          }
        }
      });
    },
    //提交任务
    submitTask(row){
      submit(row.id).then((response) => {
        if(response.code == 200){
          this.$modal.msgSuccess("提交成功");
          this.getList();
        }
      });
		},
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除任务编号为"' + row.code + '"的数据项？').then(function() {
        return delTask(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    //联系人弹窗附加查询条件
    additionalCondition(){
      return {
        parame:{
          customer: this.form.customer
        }
      }
    },
    // 触发员工参照列表
    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>
