<template>
  <div>
    <el-card class="!border-none" v-loading="pager.loading" shadow="never">
      <div class="mt-4">
        <el-table :data="pager.lists" @selection-change="handleSelectionChange">
          <el-table-column
            label="id"
            prop="id"
            show-overflow-tooltip
            width="60"
          />
          <el-table-column
            label="公司名称"
            prop="company_name"
            show-overflow-tooltip
          />
          <el-table-column
            label="公司类型"
            prop="company_type"
            show-overflow-tooltip
          />
          <el-table-column
            label="区县"
            prop="area_name"
            show-overflow-tooltip
          />
          <el-table-column
            label="乡镇"
            prop="street_name"
            show-overflow-tooltip
          />
          <el-table-column
            label="主联系人"
            prop="master_name"
            show-overflow-tooltip
          />
          <el-table-column
            label="联系方式"
            prop="master_phone"
            show-overflow-tooltip
          />
          <el-table-column
            label="片区经理"
            prop="area_manager_name"
            show-overflow-tooltip
          />
          <el-table-column
            label="是否签约"
            prop="is_contract"
            show-overflow-tooltip
          >
            <template #default="{ row }">
              <span v-if="row.is_contract == 1" style="color: #67c23a"
                >已签约</span
              >
              <span v-else style="color: #fe0000">未签约</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="300"
            fixed="right"
          >
            <template #default="{ row }">
              <div style="display: flex">
                <el-button type="primary" link>
                  <router-link
                    :to="{
                      path: getRoutePath('auth.admin/lists'),
                      query: {
                        company_id: row.id,
                        read: true,
                      },
                    }"
                    >查看成员</router-link
                  >
                </el-button>
                <el-button
                  v-perms="['company/edit', 'company/add']"
                  type="primary"
                  link
                >
                  <router-link
                    :to="{
                      path: getRoutePath('company/add:edit'),
                      query: {
                        id: row.id,
                        read: true,
                      },
                    }"
                    >详情</router-link
                  >
                </el-button>
                <el-button
                  v-perms="['company/edit', 'company/add']"
                  type="primary"
                  link
                >
                  <router-link
                    :to="{
                      path: getRoutePath('company/add:edit'),
                      query: {
                        id: row.id,
                        edit: true,
                      },
                    }"
                    >编辑</router-link
                  >
                </el-button>
                <el-button
                  v-perms="['company/delete']"
                  type="danger"
                  link
                  @click="handleDelete(row.id)"
                  >删除</el-button
                >
                <el-button
                  v-if="row.is_authentication == 0"
                  v-perms="['company/authentication']"
                  type="primary"
                  link
                  @click="handleAuthentication(row.id)"
                  >企业认证</el-button
                >
                <template v-if="row.is_authentication && row.is_contract == 0">
                  <el-button
                    v-if="
                      Array.isArray(row.contract) && row.contract.length == 0
                    "
                    v-perms="['company/initiate_contract']"
                    type="primary"
                    link
                    @click="showChangeCompany(row)"
                    >生成合同</el-button
                  >
                  <el-button
                    v-else-if="row.contract?.check_status == 1"
                    v-perms="['company/initiate_contract']"
                    type="warning"
                    link
                    @click="auditing"
                    >审核中</el-button
                  >
                  <el-button
                    v-else-if="row.contract?.check_status == 2"
                    v-perms="['company/Draftingcontracts']"
                    type="primary"
                    link
                    @click="showCreateConctPop(row)"
                    >发送合同</el-button
                  >
                  <el-button
                    v-else-if="row.contract?.check_status == 3"
                    v-perms="['company/postsms']"
                    type="primary"
                    link
                    @click="(showPop = true), (contractId = row.id)"
                    >发送短信</el-button
                  >
                </template>
              </div>
            </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="showConctactPop">
        请确认信息是否有误,发送合同,请确认信息是否有误,发送电子合同后短时间内将不可再次发送.
      </div>
      <div class="content" v-else>
        确认签约短信将在60秒后发送,请注意查收,并点击短信链接进行线上合同签约
      </div>
      <p class="btn_menu">
        <el-button
          type="primary"
          size="large"
          v-if="showConctactPop"
          @click="creContct"
          >确认创建</el-button
        >
        <el-button type="primary" size="large" v-else @click="sendMsg"
          >确认</el-button
        >
        <el-button type="info" size="large" @click="offPop">返回</el-button>
      </p>
    </el-dialog>
  </div>
</template>

<script lang="ts" setup name="companyLists">
import { usePaging } from "@/hooks/usePaging";
import { useDictData } from "@/hooks/useDictOptions";
import {
  apiCompanyLists,
  apiCompanyDelete,
  apiSubordinateList,
} from "@/api/company";
import { generateGontract, sendMsgApi } from "@/api/perms/admin";
import { timeFormat } from "@/utils/util";
import feedback from "@/utils/feedback";
import { dictDataLists } from "@/api/setting/dict";
// import EditPopup from './edit.vue'
import { getRoutePath } from "@/router";

const route = useRoute();
const company_type_show = ref(true);
// 创建合同与发送短信
//弹窗
const showPop = ref(false);
const showConctactPop = ref(false);
const offPop = () => {
  showPop.value = false;
  showConctactPop.value = false;
};
// 创建合同的id
const contractId = ref("");
const showCreateConctPop = (row) => {
  (showPop.value = true),
    (showConctactPop.value = true),
    (contractId.value = row.id);
};
const creContct = () => {
  generateGontract({ id: contractId.value });
  offPop();
};
const sendMsg = () => {
  sendMsgApi({ id: contractId.value });
  offPop();
};
// 创建合同与发送短信结束
// 查询条件
const queryParams = reactive({
  company_name: "",
  area: "",
  street: "",
  company_type: "",
  area_manager: "",
  // 是否签约
  is_contract: "",
  company_id: "",
});

if (route.query.company_type) {
  company_type_show.value = false;
  queryParams["company_type"] = route.query.company_type?.toString() || "";
}
if (route.query.company_id) {
  queryParams.company_id = route.query.company_id?.toString() || "";
}

const datas = reactive({
  dictTypeLists: [],
});
const getdictTypeLists = async () => {
  const data = await dictDataLists({ type_id: 6 });
  datas["dictTypeLists"] = data["lists"];
};
getdictTypeLists();
// 选中数据
const selectData = ref<any[]>([]);

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

// 获取字典数据
const { dictData } = useDictData("");

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
  fetchFun: apiSubordinateList,
  params: queryParams,
});

// 删除
const handleDelete = async (id: number | any[]) => {
  await feedback.confirm("确定要删除？");
  await apiCompanyDelete({ id });
  getLists();
};

getLists();
</script>
<style lang="scss">
.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>
