<template>
  <div>
    <el-card class="!border-none mb-4" shadow="never">
      <el-form class="mb-[-16px]" :model="queryParams" inline>
        <!-- <el-form-item label="店铺名称" prop="name">
          <el-input
            class="w-[280px]"
            v-model="queryParams.name"
            clearable
            placeholder="请输入店铺名称"
          />
        </el-form-item> -->
        <el-form-item label="审批类型" prop="name">
          <el-select
            v-model="queryParams.type"
            placeholder="请选择审批类型"
            clearable
            class="w-[280px]"
            @select="getLists"
          >
            <el-option
              v-for="(item, index) in typeList"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态" prop="name">
          <el-select
            v-model="queryParams.check_status"
            placeholder="请选择审核状态"
            clearable
            class="w-[280px]"
            @select="getLists"
          >
            <el-option
              v-for="(item, index) in checkStatusList"
              :key="index"
              :label="item.name"
              :value="item.id"
            ></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>
    </el-card>
    <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" />
          <el-table-column
            label="店铺名称"
            prop="extend.company_name"
            show-overflow-tooltip
          />
          <el-table-column
            label="店铺类型"
            prop="extend.type_name"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            label="商户分类"
            prop="extend.category_name"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column label="类型" show-overflow-tooltip>
            <template #default="{ row }">
              <span v-if="row.type == 2" style="color: #4a5dff">{{
                row.name
              }}</span>
              <span v-else-if="row.type == 3" style="color: #67c23a">{{
                row.name
              }}</span>
              <span v-else>{{ row.name }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="申请时间"
            prop="create_time"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            label="公司名称"
            prop="company_name"
            show-overflow-tooltip
          ></el-table-column>
          <el-table-column
            label="片区经理"
            prop="area_manager"
            show-overflow-tooltip
          >
          </el-table-column>
          <el-table-column
            label="审核状态"
            prop="check_status"
            show-overflow-tooltip
          >
            <template #default="{ row }">
              <span v-if="row.check_status == 3" style="color: #f56c6c"
                >未通过</span
              >
              <span v-if="row.check_status == 2" style="color: #67c23a"
                >已通过</span
              >
              <span v-if="row.check_status == 1" style="color: #e6a23c"
                >待审核</span
              >
            </template>
          </el-table-column>
          <el-table-column label="备注" prop="remark" show-overflow-tooltip>
          </el-table-column>
          <!-- <el-table-column label="操作" width="120" fixed="right">
            <template #default="{ row }">
              <el-button
                v-if="row.check_status <= 1"
                v-perms="['flow/edit']"
                type="primary"
                link
                @click="handleEdit(row, 'edit')"
              >
                审核
              </el-button>
              <el-button
                v-else
                type="primary"
                link
                @click="handleEdit(row, 'details')"
              >
                详情
              </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>
    <audit-other-popup
      v-if="showEdit && showAuditOther"
      ref="auditOtherRef"
      :dict-data="dictData"
      @success="getLists"
      @close="showEdit = false"
    />
    <audit-town-market-popup
      v-else-if="showEdit && openType == 4"
      ref="auditTownMarketRef"
      :dict-data="dictData"
      @success="getLists"
      @close="showEdit = false"
    />
  </div>
</template>
  
<script lang="ts" setup name="flowLists">
import { usePaging } from "@/hooks/usePaging";
import { useDictData } from "@/hooks/useDictOptions";
import { apiApproveList2 } from "@/api/examined";
import { timeFormat } from "@/utils/util";
import feedback from "@/utils/feedback";
import auditOtherPopup from "./components/audit_other.vue";
import auditTownMarketPopup from "./components/audit_town_market.vue";
import { watch } from "vue";
import { extend } from "lodash-es";

const auditOtherRef = shallowRef<InstanceType<typeof auditOtherPopup>>();
const auditTownMarketRef =
  shallowRef<InstanceType<typeof auditTownMarketPopup>>();
// 是否显示编辑框
const showEdit = ref(false);
const openType = ref(1);

// 查询条件
const queryParams = reactive({
  name: "",
  check_status: "",
  type: "2,3",
});

const typeList = ref([
  {
    id: "2,3",
    name: "全部",
  },
  {
    id: "2",
    name: "商户入驻审批",
  },
  {
    id: "3",
    name: "开通交易审批",
  },
]);

const checkStatusList = ref([
  {
    id: 1,
    name: "待审核",
  },
  {
    id: 2,
    name: "已通过",
  },
  {
    id: 3,
    name: "未通过",
  },
]);

// 显示默认审批界面的类型
const showAuditTypeList = ref([1, 5, 6]);
const showAuditOther = computed(() => {
  return showAuditTypeList.value.includes(openType.value);
});

// 选中数据
const selectData = ref<any[]>([]);

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

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

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

// 添加
const handleAdd = async () => {
  showEdit.value = true;
  await nextTick();
  auditOtherRef.value?.open("add");
};

// 编辑
const handleEdit = async (data: any, type = "edit") => {
  openType.value = data.type;
  showEdit.value = true;
  await nextTick();
  if (showAuditOther) {
    auditOtherRef.value?.open(type);
    auditOtherRef.value?.setFormData(data);
  } else if (data.type == 4) {
    auditTownMarketRef.value?.open(type);
    auditTownMarketRef.value?.setFormData(data);
  }
};

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

getLists();
</script>
  