<template>
  <div class="page-container list-container">
    <!-- 功能按钮区 -->
    <div class="list-btns-container">
      <el-button type="primary" size="small" icon="Plus" @click="handleAdd" v-show="handleVerifi()"
        v-hasPermi="['business:entrustOrder:add']">新增</el-button>
      <el-button type="danger" size="small" icon="Delete" :disabled="!multiple" @click="handleDelete"
        v-hasPermi="['business:entrustOrder:remove']">删除</el-button>
      <el-button type="success" size="small" icon="Refresh" @click="getList"
        v-hasPermi="['business:entrustOrder:remove']">刷新</el-button>
      <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-hasPermi="['business:entrustOrder: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" @submit.native.prevent>
      <el-form-item label="往来单位：" prop="contactCompany">
        <el-input v-model="queryParams.contactCompany" placeholder="请输入往来单位" style="width: 150px" 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" min-width="150" prop="signingSubject" />
      <el-table-column label="签约日期" align="center" prop="formDate" min-width="80" />
      <el-table-column label="合同编号" align="center" prop="formNo" min-width="150">
      </el-table-column>
      <el-table-column label="往来单位" align="center" prop="contactCompany" min-width="150">
      </el-table-column>
      <el-table-column label="预存金额" prop="depositAmount" header-align="center" align="right" min-width="80">
        <template #default="scope">
          <span>{{ rowNum(scope.row.depositAmount) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结余金额" prop="remainAmount" header-align="center" align="right" min-width="80"><template
          #default="scope">
          <span>{{ rowNum(scope.row.remainAmount) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" align="center" minm-width="100">
        <template #default="scope">
          <dict-tag :options="entrust_order_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="150" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" size="small" @click="handleUpdate(scope.row.id)"
            v-hasPermi="['business:entrustOrder:query']">详情</el-button>
          <el-button v-show="scope.row.status === 0" link type="primary" size="small" @click="handleConfirm(scope.row)"
            v-hasPermi="['business:entrustOrder:confirm']">确认订单</el-button>
          <el-button v-show="scope.row.status === 1" link type="primary" size="small" @click="handleFinished(scope.row)"
            v-hasPermi="['business:entrustOrder:finished']">确认完结</el-button>

          <router-link v-if="scope.row.status !== 0" :to="'/entrust/statusment/order/' + scope.row.id"
            class="link-type">
            <el-button link size="small" style="margin-left: 10px" type="primary"
              v-hasPermi="['business:entrustOrder:statement:list']">对账单列表</el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>
    <entrust-order-form ref="entrustOrderFormRef" :getList="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="EntrustOrder">
  import {
    listEntrustOrder,
    exportEntrustOrder,
    delEntrustOrder,
    confirmEntrustOrder,
    finishedEntrustOrder,
  } from "@/api/business/entrust/entrustOrder";
  import entrustOrderForm from "./form";
  import useUserStore from "@/store/modules/user";
  import { rowNum } from "@/utils/index";
  const permissions = useUserStore().permissions;
  const all_permission = "*:*:*";
  const { proxy } = getCurrentInstance();
  /** 字典数组区 */
  /** 查询 对象 */

  const list = ref([]);
  const loading = ref(true);
  const ids = ref([]);
  const selections = ref([]);
  const single = ref(false);
  const multiple = ref(false);
  const total = ref(0);
  const prev = ref([]);
  const entrustOrderFormRef = ref(null);

  const setEntrustDialogRef = ref(null);

  const { entrust_order_status } = proxy.useDict("entrust_order_status");
  /** 查询对象 */
  const queryParams = ref({
    pageNum: 1,
    pageSize: 20,
    contactCompany: "",
    orderByColumn: "create_time",
    type: 2,
  });

  const editStatus = {
    startMonth: false,
  };

  /***********************  方法区  ****************************/
  onActivated(() => {
    // 你的逻辑
    getList();
  });
  /** 查询company列表 */
  function getList() {
    loading.value = true;
    listEntrustOrder(queryParams.value).then((response) => {
      list.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  }

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

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

  //验证当前版本
  function handleVerifi() {
    return (
      permissions.includes(all_permission) ||
      useUserStore().tenant.versionId == "4"
    );
  }

  // 多选框选中数据
  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() {
    exportEntrustOrder(queryParams.value);
  }

  /** 新增按钮操作 */
  function handleAdd() {
    entrustOrderFormRef.value.open();
  }

  function handleUpdate(id) {
    entrustOrderFormRef.value.open(id);
  }

  /** 删除按钮操作 */
  function handleDelete(row) {
    const _ids = row.id || ids.value;
    proxy.$modal
      .confirm("是否确认删除选中的数据项？")
      .then(function () {
        return delEntrustOrder(_ids);
      })
      .then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功！");
      })
      .catch(() => { });
  }

  function handleConfirm(row) {
    proxy.$modal
      .confirm("确认启用该合同？")
      .then((_) => {
        return confirmEntrustOrder(row);
      })
      .then((res) => {
        console.log(res);
        getList();
        proxy.$modal.msgSuccess("确认成功");
      });
  }

  function handleFinished(row) {
    proxy.$modal
      .confirm("确认完结该合同？")
      .then((_) => {
        return finishedEntrustOrder(row);
      })
      .then((res) => {
        console.log(res);
        getList();
        proxy.$modal.msgSuccess("完结成功");
      });
  }

  function handleViewAccountment(row) { }

  getList();
</script>