<template>
  <div>
    <el-card class="!border-none" v-loading="pager.loading" shadow="never">
      <el-form class="mb-[-16px]" inline>
        <el-form-item label="公司名称" prop="company_name">
          <el-input
            class="w-[280px]"
            v-model="queryParams.company_name"
            clearable
            placeholder="请输入公司名称"
          />
        </el-form-item>
        <el-form-item label="合同编号" prop="contract_no">
          <el-input
            class="w-[280px]"
            v-model="queryParams.contract_no"
            clearable
            placeholder="请输入合同编号"
          /> </el-form-item
        ><el-form-item label="合同状态" prop="status">
          <el-select
            v-model="queryParams.status"
            placeholder="请选择合同类型"
            clearable
            class="w-[280px]"
          >
            <el-option label="未上传" :value="0"></el-option>
            <el-option label="已上传" :value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="resetPage">查询</el-button>
          <el-button @click="resetParams">重置</el-button>
        </el-form-item>
      </el-form>
      <div class="mt-4">
        <el-table
          :data="pager.lists"
          :default-sort="{ prop: 'create_time', order: 'descending' }"
          @selection-change="handleSelectionChange"
        >
          <el-table-column label="id" prop="id" />
          <el-table-column label="合同类型" align="center">
            <template #default="{ row }">
              {{ typeFn(row.type) }}
            </template>
          </el-table-column>
          <el-table-column
            label="合同编号"
            prop="contract_no"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            label="甲方"
            prop="company_a_name"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            label="乙方"
            prop="company_b_name"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            label="类型"
            prop="product_count"
            align="center"
            show-overflow-tooltip
          >
            <template #default="{ row }"> 公司 </template>
          </el-table-column>
          <el-table-column
            label="租车数量(辆)"
            prop="num"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            label="状态"
            prop="product_count"
            align="center"
            show-overflow-tooltip
          >
            <template #default="{ row }">
              <span :style="`color:${statusFn(row.status).color}`">
                {{ statusFn(row.status).tit }}</span
              >
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="auto"
            fixed="right"
          >
            <template #default="{ row }">
              <el-button
                type="primary"
                link
                v-perms="['contract.contract/wind_control']"
                v-if="row.status == 0"
              >
                <router-link
                  :to="{
                    path: 'vehicle_detail',
                    query: {
                      id: row.id,
                    },
                  }"
                  >审核</router-link
                >
              </el-button>
              <el-button
                type="primary"
                link
                v-perms="['contract.contract/wind_control']"
                v-if="row.status > 0"
              >
                <router-link
                  :to="{
                    path: 'vehicle_detail',
                    query: {
                      id: row.id,
                    },
                  }"
                  >详情</router-link
                >
              </el-button>
              <el-button
                v-if="row.status == 1 && row.contract_logistic_id != 0"
                type="primary"
                link
                @click="sendContractPopFn(row.id)"
                >发送合同</el-button
              >
              <el-button
                v-if="row.status == 2"
                :disabled="row.nums > 0"
                @click="sendAgaintPopFn(row.id)"
                link
                type="primary"
                >重新发送{{ row.nums ? row.nums + "s" : "" }}</el-button
              >
              <el-button v-if="row.status == 3" type="primary" link>
                <a :href="JSON.parse(row.contract_evidence).party_a"
                  >下载证据</a
                >
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="flex mt-4 justify-end">
        <pagination v-model="pager" @change="getLists" />
      </div>
    </el-card>

    <el-dialog v-model="showPop" @close="offPop">
      <h1>重要提醒</h1>
      <div class="content" v-if="!sendAgainPop">
        请确认信息是否有误,发送合同,请确认信息是否有误,发送电子合同后短时间内将不可再次发送.
      </div>
      <div class="content" v-else>
        确认签约短信将在60秒后发送,请注意查收,并点击短信链接进行线上合同签约
      </div>
      <p class="btn_menu">
        <el-button
          type="primary"
          size="large"
          v-if="!sendAgainPop"
          @click="sendContractFn"
          >确认</el-button
        >
        <el-button type="primary" size="large" v-else @click="sendAgain"
          >重新发送</el-button
        >
        <el-button type="info" size="large" @click="offPop">返回</el-button>
      </p>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup name="flowTypeLists">
import { usePaging } from "@/hooks/usePaging";
import { useDictData } from "@/hooks/useDictOptions";
import { leaseContractList } from "@/api/contract";
import { sendContract, sendMsg } from "@/api/car_contract";
import feedback from "@/utils/feedback";
// 发送合同与重新发送(弹窗)

const index = ref(0);
// 发送合同的id
var contractId = 0;
const showPop = ref(false);
const sendAgainPop = ref(false);
const sendAgain = () => {
  sendMsg({ id: contractId }).then((res) => {
    var inde = pager.lists.findIndex(function (element) {
      return element.id === contractId;
    });
    (sendAgainPop.value = true), (index.value = inde);
    pager.lists[index.value].nums = 10;
    timeFn(index.value);
  });
};
const timeFn = (i: number) => {
  const timers = setInterval(() => {
    pager.lists[i].nums--;
    if (pager.lists[i].nums <= 0) {
      clearInterval(timers);
    }
  }, 1000);
};
const sendContractPopFn = (id: number) => {
  (showPop.value = true), (contractId = id);
};
const sendAgaintPopFn = (id: number) => {
  (showPop.value = true), (contractId = id);
};
const sendContractFn = () => {
  sendContract({ id: contractId }).then((res) => {
    feedback.msgSuccess("发送成功");
    var inde = pager.lists.findIndex(function (element) {
      return element.id === contractId;
    });
    index.value = inde;
    showPop.value = false;
    pager.lists[index.value].status = 2;
    pager.lists[index.value].nums = 10;
    timeFn(index.value);
  });
};

const offPop = () => {
  showPop.value = false;
  //   showConctactPop.value = false;
};

// 查询条件
const queryParams = reactive({
  company_name: "",
  contract_no: "",
  status: "",
});
const statusFn = (status: number) => {
  switch (status) {
    case 0:
      return { tit: "待上传", color: "rgb(238, 190, 119)" };
    case 1:
      return { tit: "已上传", color: "rgb(103, 194, 58)" };
    case 2:
      return { tit: "签约中", color: "red" };
    case 3:
      return { tit: "已签约", color: "rgb(103, 194, 58)" };
    // default:
    //   return { tit: "审核通过", color: "green" };
  }
};
const typeFn = (status: number) => {
  switch (status) {
    case 0:
      return "租赁合同";

    case 1:
      return "自由车辆合同";

    case 2:
      return "解除合同";
    default:
  }
};
// 选中数据
const selectData = ref<any[]>([]);

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
  selectData.value = val.map(({ id }) => id);
};

// 获取字典数据
const { dictData } = useDictData("");
// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: leaseContractList,
  params: queryParams,
});

getLists();
</script>
<style lang="scss" scoped>
.btn {
  position: absolute;
}

h1 {
  text-align: center;
  font-weight: bold;
  font-size: 30px;
  color: red;
  margin-bottom: 10px;
}

.content {
  font-size: 20px;
}

.info {
  color: red;
  font-weight: bold;
  font-size: 18px;
  display: inline-block;
  margin: 0 5px;
}

.btn_menu {
  margin-top: 10vh;
  display: flex;
  justify-content: space-around;
}
</style>
