<template>
  <div class="page-container list-container">
    <div>
      <span style="
          display: inline-block;
          height: 20px;
          line-height: 18px;
          font-size: 15px;
          padding: 10px 20px 0 8px;
          margin-top: 5px;
          font-weight: 700;
          border-left: 3px;
        ">待完成任务</span>
      <el-button type="success" icon="link" :disabled="multiple" size="small" @click="handleTyransfer"
        v-hasPermi="['business:workOrder:tyransfer']">转交</el-button>
    </div>
    <!-- v-hasPermi="['business:workOrder:transfer']" -->

    <!-- 搜索区 -->
    <el-form class="list-search-container" size="small" :model="queryParams" ref="queryRef" :inline="true"
      label-width="68px" @selection-change="handleSelectionChange">
      <el-form-item label="客户名称：" prop="companyName">
        <el-input style="width: 150px" v-model="queryParams.companyName" placeholder="请输入客户名称" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表区 -->
    <el-table v-loading="loading" :data="orderList" size="small" border height="100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="客户名称" align="center" prop="companyName" width="250" />
      <el-table-column label="类型" align="center" width="150">
        <template #default="scope">
          {{ scope.row.type === 1 ? "循环工单" : "代办工单" }}
        </template>
      </el-table-column>
      <el-table-column label="项目" align="center" prop="taskTypeName">
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />
    <el-dialog title="转交执行人" v-model="formOpen" width="500px" append-to-body draggable @close="cancel">
      <el-form ref="dictRef" :model="form" :rules="rules" label-width="100">
        <el-form-item label="转交执行人:" :prop="content">
          <el-autocomplete :fetch-suggestions="querySearchAsync" :trigger-on-focus="true" v-model="selectionName"
            placeholder="输入执行人" popper-class="my-autocomplete" @select="handleSelectEmployee">
            <template #default="{ item }">
              <div style="
                  display: flex;
                  flex-direction: row;
                  justify-content: space-between;
                ">
                <div class="name" style="font-size: 12px">
                  {{ item.nickName }}
                </div>
                <span class="code" style="font-size: 10px; color: darkgrey">{{
                  item.userName
                }}</span>
              </div>
            </template>
          </el-autocomplete>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" icon="Finished" size="small" @click="submitForm">确 定</el-button>
          <el-button icon="close" size="small" @click="formOpen = false">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="index">
import { indexList } from "@/api/business/crm/workOrder";
import { setWorkOrderService } from "@/api/business/crm/workOrder";
import { listUser } from "@/api/system/user";
const { proxy } = getCurrentInstance();
/** 字典数组区 */
/** 查询对象 */
const loading = ref(true);
const orderList = ref([]);
const multiple = ref(true);
const selectionName = ref("");
const formOpen = ref(false);
const selectionUser = ref(null);
const prev = ref([]);
const ids = ref([]);
const total = ref(0);
/** 查询对象 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 20,
  companyName: "",
  orderByColumn: "create_time",
});

const editStatus = {
  startMonth: false,
};

/***********************  方法区  ****************************/

/** 查询company列表 */
function getList() {
  loading.value = true;
  formOpen.value = false;
  indexList(queryParams.value).then((response) => {
    orderList.value = response.rows.map((l) => ({
      ...l,
      editStatus: proxy.deepClone(editStatus),
    }));

    total.value = response.total;
    loading.value = false;
    console.log("查询", response);
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id);
  multiple.value = !selection.length;
}

// 转交按钮操作
function handleTyransfer() {
  formOpen.value = true;
}

//转交弹框确定按钮操作
function submitForm() {
  if (selectionUser.value == null || selectionUser.value.userId == null) {
    proxy.$modal.msgError("请选择用户！");
    return;
  }
  setWorkOrderService({
    ids: ids.value,
    serviceId: selectionUser.value.userId,
  }).then((response) => {
    proxy.$modal.msgSuccess("设置完成!");
    formOpen.value = false;
    getList();
  });
}

/** 输入框输出建议 */
const querySearchAsync = (queryString, cb) => {
  const query = { keyword: queryString };
  listUser(query).then((res) => {
    cb(res.rows);
  });
};

//转交搜索框输入
function handleSelectEmployee(item) {
  console.log(item);
  selectionName.value = item.nickName;
  selectionUser.value = item;
}
getList();
</script>
