<template>
  <div class="page-container list-container">
    <!-- 功能按钮区 -->
    <div class="list-btns-container">
      <el-dropdown trigger="click">
        <el-button type="primary" size="small">
          其它<el-icon class="el-icon--right"><arrow-down /></el-icon>
        </el-button>
        <template #dropdown>
          <el-dropdown-menu>
            <el-dropdown-item icon="Download" @click="handleExport"
              v-if="viewAdviser('business:entrust:workOrder:once:export')">
              导出</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </div>
    <!-- 搜索区 -->
    <el-form class="list-search-container" size="small" :model="queryParams" ref="queryRef" :inline="true"
      label-width="68px">
      <el-form-item label="客户名称：" prop="companyName">
        <el-input 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="list" size="small" border height="100%"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="工单号" align="center" prop="workOrderNo" width="200" /> -->
      <el-table-column label="客户名称" align="center" prop="companyName" />
      <el-table-column label="税号" align="center" prop="socialCreditCode" width="200" />
      <el-table-column label="来源" align="center" width="100" prop="fromCompanyName" />
      <el-table-column label="工单类型" align="center" prop="amount" width="80">
        <template #default="scope">
          {{ scope.row.type === 1 ? "循环工单" : "代办工单" }}
        </template>
      </el-table-column>
      <el-table-column label="项目" align="center" prop="taskTypeName" width="140">
        <template #default="scope">
          {{ scope.row.noContract === 1 ? scope.row.remark : "" }}
          {{ scope.row.taskTypeName }}
          {{
          scope.row.taskTypeDetailName
          ? `-${scope.row.taskTypeDetailName}`
          : ""
          }}
        </template>
      </el-table-column>
      <el-table-column label="工单执行人" align="center" width="100">
        <template #default="scope">
          {{
          scope.row.entrust != null
          ? scope.row.entrust.toAccountName
          : scope.row.serviceName
          }}
        </template>
      </el-table-column>
    </el-table>
    <!-- <contract-form ref="contractRef" :get-list="getList" /> -->
    <!-- 分页 -->
    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize" @pagination="getList" />
  </div>
</template>

<script setup name="Company">
  import { getTenant } from "@/utils/auth";
  import useUserStore from "@/store/modules/user";

  import {
    listFactoryOnceWorkOrder,
    checkCurrent,
    exportOnceWorkOrder,
    delEntrust,
    transNext,
    addEntrust,
  } from "@/api/business/entrust/onceWorkOrder";
  const { proxy } = getCurrentInstance();
  /** 字典数组区 */
  /** 查询 对象 */

  const list = ref([]);
  const loading = ref(true);
  const ids = ref([]);
  const selections = ref([]);
  const single = ref(false);
  const permissions = useUserStore().permissions;
  const all_permission = "*:*:*";
  const multiple = ref(false);
  const total = ref(0);
  const prev = ref([]);

  const setEntrustDialogRef = ref(null);

  const { contract_verify_status } = proxy.useDict("contract_verify_status");
  const { contract_status } = proxy.useDict("contract_status");
  /** 查询对象 */
  const queryParams = ref({
    pageNum: 1,
    pageSize: 20,
    companyName: "",
    toTenantId: getTenant(),
    type: 2,
  });

  const editStatus = {
    startMonth: false,
  };
  onActivated(() => {
    // 你的逻辑
    getList();
  });
  /***********************  方法区  ****************************/

  /** 查询company列表 */
  function getList() {
    loading.value = true;
    listFactoryOnceWorkOrder(queryParams.value).then((response) => {
      list.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }

  //判断是否有权限
  function viewAdviser(quer) {
    return permissions.includes(all_permission) || permissions.includes(quer);
  }

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

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

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

  /** 导出按钮操作 */
  function handleExport() {
    exportOnceWorkOrder(queryParams.value);
  }

  function setEntrustHandle() {
    if (!multiple.value) {
      proxy.$modal.msgError("请选择一条记录！");
      return;
    }
    setEntrustDialogRef.value.open({ selections: selections.value });
  }

  function delEntrustHandle() {
    if (!multiple.value) {
      proxy.$modal.msgError("请选择一条记录！");
      return;
    }
    const entrust = {
      workOrderIds: ids.value,
    };
    proxy.$modal.confirm("是否确认取消委托？").then(() => {
      delEntrust(entrust).then((res) => {
        proxy.$modal.msgSuccess("取消成功");
        getList();
      });
    });
  }

  function handleAddEntrust(data) {
    addEntrust(data).then((response) => {
      proxy.$modal.msgSuccess("设置完成!");
      setEntrustDialogRef.value.cancel();
      getList();
    });
  }

  function transEntrustHandler() {
    proxy.$modal.confirm("确定流转到下一月？").then(() => {
      transNext().then((res) => {
        proxy.$modal.msgSuccess("流转成功");
        checkCurrentMonthHandle();
      });
    });
  }

  getList();
</script>