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

      <el-button
        type="danger"
        size="small"
        icon="Delete"
        :disabled="multiple"
        @click="handleDelete"
        v-hasPermi="['oncePrice:oncePrice: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('oncePrice:oncePrice: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="60px"
    >
      <el-form-item label="任务类型：" prop="taskTypeId">
        <el-select
          v-model="queryParams.taskTypeId"
          type="text"
          placeholder="选择任务"
          :clearable="true"
          width="100px"
          @change="handleTaskChange"
          @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 label="任务明细：" prop="fromAccountName">
        <el-select
          v-model="queryParams.taskTypeDetailId"
          type="text"
          placeholder="先选择任务类型"
          :clearable="true"
          width="100px"
          @clear="handleTaskDetaiClear"
        >
          <el-option
            v-for="item in taskTypeDetail"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="省市区：" prop="provinceCode" style="width: 300px">
        <el-select
          v-model="queryParams.provinceCode"
          placeholder="省份"
          style="width: 33%"
          @change="handleSelectProvince"
        >
          <el-option
            v-for="item in provinces"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          />
        </el-select>
        <el-select
          v-model="queryParams.cityCode"
          placeholder="城市"
          style="width: 33%"
          @change="handleSelectCity"
        >
          <el-option
            v-for="item in cities"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          />
        </el-select>
        <el-select
          v-model="queryParams.districtCode"
          placeholder="行政区"
          style="width: 33%"
          @change="handleSelectDistrict"
        >
          <el-option
            v-for="item in districts"
            :key="item.code"
            :label="item.name"
            :value="item.code"
          />
        </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="地区"
        min-width="250"
        align="center"
        prop="taskTypeName"
      >
        <template #default="scope">
          <span
            >{{ scope.row.province }}-{{ scope.row.city }}-{{
              scope.row.district
            }}</span
          >
        </template>
      </el-table-column>
      <el-table-column
        label="任务类型"
        align="center"
        min-width="100"
        prop="taskTypeName"
      >
      </el-table-column>
      <el-table-column
        label="任务明细"
        min-width="100"
        align="center"
        prop="taskTypeDetailName"
      >
        <template #default="scope">
          <span v-if="scope.row.taskTypeId == 6"
            >{{ scope.row.alterType }}-{{ scope.row.taskTypeDetailName }}
          </span>
          <span v-else>{{ scope.row.taskTypeDetailName }} </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="100"
        prop="remark"
      />
      <el-table-column
        label="操作"
        align="center"
        min-width="100"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="warning"
            size="small"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['oncePrice:oncePrice:save']"
            >修改</el-button
          >
          <el-button
            link
            type="danger"
            size="small"
            @click="handleDelete(scope.row)"
            v-hasPermi="['oncePrice:oncePrice: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 {
  listOncePrice,
  delOncePrice,
  getTaskTypeList,
  getTaskTypeDetailList,
} from "@/api/system/oncePrice";
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 permissions = useUserStore().permissions;
const all_permission = "*:*:*";
const provinces = ref(proxy.region.getProvinces());
provinces.value.unshift({ code: "", name: "全部" });
const cities = ref([]);
const districts = ref([]);
const multiple = ref(true);
const total = ref(0);
/** 任务类型 */
const taskType = ref([]);
/** 任务类型明细 */
const taskTypeDetail = ref({});
/** 查询对象 */
const queryParams = ref({
  pageNum: 1,
  pageSize: 20,
  price: null,
  annualIncome: null,
  isChanged: null,
  fromTenantId: null,
  toTenantId: null,
});

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

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

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

//判断是否有权限
function viewAdviser(quer) {
  return permissions.includes(all_permission) || permissions.includes(quer);
}

/** 清除选择任务  */
function handleTaskClear() {
  queryParams.value.taskTypeId = null;
}

/** 初始化地区 */
function initRegion() {
  cities.value = proxy.region.getCities(queryParams.value.provinceCode);

  districts.value = proxy.region.getDistricts(queryParams.value.cityCode);
}

/** 任务切换事件 */
function handleTaskChange(item) {
  if (item) {
    getTaskTypeDetailList(item).then((res) => {
      taskTypeDetail.value = res.data;
    });
  } else {
    taskTypeDetail.value = [];
  }
}

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

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.cityCode = "";
  queryParams.value.districtCode = "";
  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 delOncePrice(_ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功！");
    })
    .catch(() => {});
}

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

function handleSelectProvince() {
  const _provinceCode = queryParams.value.provinceCode;
  const _province = provinces.value.find(function (item) {
    return item.code === _provinceCode;
  });
  queryParams.value.province = _province.name;
  cities.value = proxy.region.getCities(_provinceCode);
  if (cities.value.length === 0) {
    // 清空城市数据
    queryParams.value.cityCode = "";
    queryParams.value.city = "";
    cities.value = [{ code: "", name: "全部" }];
    // 清空行政区数据
    queryParams.value.districtCode = "";
    queryParams.value.district = "";
    districts.value = [];
  } else {
    cities.value.unshift({ code: "", name: "全部" });
    queryParams.value.cityCode = cities.value[0].code;
    queryParams.value.city = cities.value[0].name;
    handleSelectCity();
  }
}

function handleSelectCity() {
  const _cityCode = queryParams.value.cityCode;
  const _city = cities.value.find(function (item) {
    return item.code === _cityCode;
  });
  queryParams.value.city = _city.name;
  districts.value = proxy.region.getDistricts(_cityCode);
  if (districts.value.length === 0) {
    // 清空行政区数据
    queryParams.value.districtCode = "";
    queryParams.value.district = "";
    districts.value = [{ code: "", name: "全部" }];
  } else {
    districts.value.unshift({ code: "", name: "全部" });
    queryParams.value.districtCode = districts.value[0].code;
    queryParams.value.district = districts.value[0].name;
  }
}

function handleSelectDistrict() {
  const _districtCode = queryParams.value.districtCode;
  const _district = districts.value.find(function (item) {
    return item.code === _districtCode;
  });
  queryParams.value.district = _district.name;
}

getList();
</script>
