<template>
  <div class="page-container list-container">
    <!-- 功能按钮区 -->
    <div class="list-btns-container">
      <el-button
        type="primary"
        size="small"
        icon="Plus"
        @click="handleAdd"
        v-hasPermi="['priceList:priceList:save']"
        >新增</el-button
      >

      <el-button
        type="danger"
        size="small"
        icon="Delete"
        :disabled="multiple"
        @click="handleDelete"
        v-hasPermi="['priceList:priceList: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-if="viewAdviser('priceList:priceList: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="taskTypeId">
        <el-select
          v-model="queryParams.taskTypeId"
          type="text"
          placeholder="选择任务"
          :clearable="true"
          width="100px"
          @clear="handleTaskClear"
        >
          <el-option
            v-for="item in taskType"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </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="priceListList"
      size="small"
      border
      height="100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号" align="center" type="index" /> -->
      <el-table-column
        label="任务类型"
        align="center"
        min-width="120"
        prop="taskTypeName"
      />
      <el-table-column
        label="是否零申报"
        align="center"
        min-width="120"
        prop="isZero"
      >
      <template #default="scope">
          <span v-if="scope.row.isZero == 1">{{
            scope.row.isZero ? "是" : "否"
          }}</span>
          <span v-else-if="scope.row.isZero == 0">{{
            scope.row.isZero ? "是" : "否"
          }}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column
        label="单价"
        header-align="center"
        align="right"
        min-width="80"
        prop="price"
      />
      <el-table-column
        label="年收入"
        align="center"
        min-width="120"
        prop="annualIncome"
      >
        <template #default="scope">
          <span v-if="scope.row.taskTypeId == 1">{{
            scope.row.annualIncome
          }}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>

      <el-table-column
        label="(社保/公积金)是否变更"
        align="center"
        min-width="80"
        prop="isChanged"
      >
        <template #default="scope">
          <span v-if="scope.row.taskTypeId == 2 || scope.row.taskTypeId == 3">{{
            scope.row.isChanged ? "是" : "否"
          }}</span>
          <span v-else>--</span>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        min-width="150"
        prop="remark"
      />
      <el-table-column
        label="操作"
        align="center"
        width="100"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="warning"
            size="small"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['priceList:priceList:save']"
            >修改</el-button
          >
          <el-button
            link
            type="danger"
            size="small"
            @click="handleDelete(scope.row)"
            v-hasPermi="['priceList:priceList:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 表单 -->
    <priceList-form ref="priceListRef" :get-list="getList"></priceList-form>
  </div>
</template>

<script setup name="PriceList">
import {
  listPriceList,
  delPriceList,
  getTaskTypeList,
} from "@/api/system/priceList";
import { yesOrNo } from "@/utils/default";
import useUserStore from "@/store/modules/user";
import priceListForm from "./formDialog";
const { proxy } = getCurrentInstance();
/** 字典数组区 */
/** 查询 对象 */

const priceListList = ref([]);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const permissions = useUserStore().permissions;
const all_permission = "*:*:*";
const total = ref(0);
/** 任务类型 */
const taskType = ref([]);
/** 查询对象 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 20,
  price: null,
  annualIncome: null,
  isChanged: null,
  fromTenantId: null,
  toTenantId: null,
});

/***********************  方法区  ****************************/

/** 查询价目表系统列表 */
function getList() {
  loading.value = true;
  console.log(queryParams.value);
  listPriceList(queryParams.value).then((response) => {
    priceListList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });

  //查询任务类型
  getTaskTypeList().then((res) => {
    console.log(res);
    taskType.value = res.data;
  });
}

//判断是否有权限
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);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  proxy.$refs.priceListRef.open();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const id = row.id || ids.value;
  proxy.$refs.priceListRef.open(id, row);
}

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

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "priceList/priceList/export",
    {
      ...queryParams.value,
    },
    `循环价目表_${new Date().getTime()}.xlsx`
  );
}

getList();
</script>
