material_pre_request.vue 15.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
<!--
 * @Date: 2024-07-23 12:53:15
 * @LastEditors: hookehuyr hookehuyr@gmail.com
 * @LastEditTime: 2024-07-26 17:22:46
 * @FilePath: /temple_material_request/src/views/material_pre_request.vue
 * @Description: 待申领物资页面
-->
<template>
  <div class="material-pre-request-page">
    <div class="list-wrapper">
      <van-sticky>
        <van-row justify="space-between" class="select-all-item">
          <van-col span="8">
            <span v-if="model === 'edit'">
              <van-icon v-if="is_all_checked" @click="onCheckAll()" name="checked" size="1.25rem" :color="styleColor.baseColor" />
              <van-icon v-else name="passed" @click="onCheckAll()" size="1.25rem" />
              <span @click="onCheckAll()" :style="{ color: styleColor.baseColor }">&nbsp;&nbsp;全选</span>
            </span>
            <span v-else :style="{ color: styleColor.baseColor }">品项数: {{ sum_num }}</span>
          </van-col>
          <van-col span="16" :style="{ textAlign: 'right', fontSize: '0.85rem', color: styleColor.baseColor }">
            <span v-if="model !== 'edit'" @click="onClickEdit"><van-icon name="records-o" />&nbsp;编辑</span>
            <span v-else>
              <span @click="onClickDel"><van-icon name="delete-o" />&nbsp;删除</span>&nbsp;&nbsp;
              <span @click="onClickCancel"><van-icon name="close" />&nbsp;确认编辑</span>
            </span>
          </van-col>
        </van-row>
      </van-sticky>
      <div v-for="item in list" :id="item.good_id" :key="item" class="list-boxer">
        <van-row gutter="10" align="center" justify="space-between">
          <van-col span="16" style="display: flex;">
            <span v-if="item.edit">
              <van-icon v-if="item.checked" @click="onCheck(item)" name="checked" size="1.25rem" :color="styleColor.baseColor" />
              <van-icon v-else name="passed" @click="onCheck(item)" size="1.25rem" />&nbsp;&nbsp;
            </span>
            <div class="van-ellipsis" :style="{ color: styleColor.baseColor, textDecoration: 'underline' }" @click="onClickTitle(item)">
              {{ item.product_name }} / {{ item.spec }}
            </div>
          </van-col>
          <van-col span="8" style="display: flex; align-items: center;">
            <van-field
              v-model="item.apply_number"
              style="border: 1px solid #f0f0f0; padding: 0; border-radius: 5px;"
              label=""
              label-width="0"
              input-align="center"
              placeholder="数量"
              type="number"
              :disabled="!item.edit"
              @blur="onBlur(item)"
            >
              <template #left-icon></template>
            </van-field>&nbsp;&nbsp;<span style="font-size: 0.9rem; color: #666;">个</span>
          </van-col>
        </van-row>
        <div v-if="item.error" style="padding: 0.5rem 2rem 0 0; font-size: 0.85rem; color: red; text-align: right;">输入值有误</div>
      </div>
      <van-empty v-if="!list.length" image="error" description="待申领物资为空" />
      <div style="height: 10rem;"></div>
      <div style="position: fixed; left: 0; right: 0; bottom:4.5rem; padding: 1rem;">
        <van-button icon="plus" type="primary" :color="styleColor.baseColor" plain block :disabled="disabled_btn" @click="addMore">添加更多</van-button>
      </div>
    </div>
    <div class="control-bar">
      <!--  -->
      <div style="display: flex; justify-content: space-between; align-items: center;">
        <div style="display: flex; align-items: center;" @click="showPicker = true">
          <div style="font-size: 0.85rem; color: #202020;">使用时间:</div>&nbsp;&nbsp;
          <div style="border: 1px solid #f0f0f0; border-radius: 5px; padding: 0.5rem 0.35rem 0.5rem 1rem; min-width: 6rem; font-size: 0.85rem; display: flex; align-items: center; justify-content: space-between;">{{ use_time }}&nbsp;&nbsp;<van-icon name="notes-o" size="1rem" :color="styleColor.baseColor"  /></div>
        </div>
        <div style="display: flex; align-items: center;">
          <van-button type="primary" :color="styleColor.baseColor" :disabled="disabled_btn" @click="onConfirmRequest">确定申领</van-button>
        </div>
      </div>
    </div>
  </div>

  <van-popup v-model:show="showPicker" position="bottom">
    <van-date-picker
      v-model="currentDate"
      title="日期选择"
      :min-date="minDate"
      :max-date="maxDate"
      :columns-type="columns_type"
      @confirm="onConfirm"
      @cancel="showPicker = false"
    />
  </van-popup>

  <van-dialog v-model:show="showType" title="" :show-cancel-button="false" :show-confirm-button="false">
    <div style="padding: 1.5rem 0 0; text-align: center; font-size: 0.95rem; font-weight: bold;">请确认您本次是为哪个组别申领物资</div>
    <div style="display: flex; align-items: center; padding: 1rem;">
      <!-- <div style="font-size: 0.9rem; margin-right: 1rem; width: 5rem;">申领组:&nbsp;&nbsp;</div> -->
      <div>
        <van-radio-group v-model="type_checked" direction="horizontal" style="font-size: 0.9rem;">
          <van-radio :name="item.dept_id" v-for="(item, index) in my_depts" :key="index" :checked-color="styleColor.baseColor" style="margin-bottom: 0.25rem;">{{ item.dept_name }}</van-radio>
        </van-radio-group>
      </div>
    </div>
    <div style="padding: 0 1rem;">
      <van-row gutter="10" align="center" style="margin-bottom: 0.5rem;">
        <van-col span="7" style="text-align: right;"><span style="color: red;">*</span>申领人:</van-col>
        <van-col span="17"><van-field v-model="contact_name" label="" placeholder="请输入" style="border: 1px solid #DBDBDB; padding: 0.25rem 1rem; border-radius: 5px;" /></van-col>
      </van-row>
      <van-row gutter="10" align="center" style="margin-bottom: 1rem;">
        <van-col span="7" style="text-align: right;"><span style="color: red;">*</span>联系电话:</van-col>
        <van-col span="17"><van-field v-model="contact_phone" label="" placeholder="请输入" type="tel" maxlength="11" style="border: 1px solid #DBDBDB; padding: 0.25rem 1rem; border-radius: 5px;" /></van-col>
      </van-row>
    </div>
    <van-row gutter="20" justify="space-around" align="center" style="padding: 1rem; border-top: 1px solid #E9E9E9; text-align: center;">
      <van-col span="12">
        <van-button plain block color="#A67939" @click="onDialogCancel">取消</van-button>
      </van-col>
      <van-col span="12">
        <van-button block color="#A67939" @click="onDialogConfirm">确定</van-button>
      </van-col>
    </van-row>
  </van-dialog>

  <choose-material :show="show_choose_material" @close="onCloseChoose"></choose-material>
  <material-detail :show="show_material_detail" :good-id="material_id" @close="onCloseDetail"></material-detail>

</template>

<script setup>
import { ref } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { showToast, showConfirmDialog, showDialog } from 'vant';
import { Cookies, $, _, axios, storeToRefs, mainStore, Toast, useTitle } from '@/utils/generatePackage.js'
//import { } from '@/utils/generateModules.js'
//import { } from '@/utils/generateIcons.js'
//import { } from '@/composables'
import { styleColor } from "@/constant.js";
import dayjs from "dayjs";
import chooseMaterial from '@/components/chooseMaterial/index.vue';
import materialDetail from '@/components/materialDetail/index.vue';
import { getCartListAPI, delCartAPI, myDeptsAPI, myUserAPI, editCartAPI, addUseOrderAPI } from "@/api/material";

const $route = useRoute();
const $router = useRouter();
useTitle($route.meta.title);

const activity_id = $route.query.activity_id ? $route.query.activity_id : 800863;
const form_id = $route.query.form_id ? $route.query.form_id : '';
const client_id = $route.query.client_id ? $route.query.client_id : '';

const disabled_btn = ref(false);

/**
 * 获取我的组别列表
 */
const my_depts = ref([]);
const getDeptsList = async () => {
  const { data, code } = await myDeptsAPI();
  if (code) {
    my_depts.value = data;
    if (!data.length) {
      showToast('您暂无组别无法申领物资');
      disabled_btn.value = true;
    }
  }
}

/**
 * 获取我的用户信息
 */
const getUserList = async () => {
  const { data, code } = await myUserAPI();
  if (code) {
    contact_name.value = data.name;
    contact_phone.value = data.mobile;
  }
}

/**
 * 购物车列表
 */
const getCartList = async () => {
  const { data, code } = await getCartListAPI({ activity_id });
  if (code) {
    list.value = data;
    list.value.forEach(item => {
      item.checked = false;
    })
    if (!list.value.length) { // 购物车为空
      // showDialog({
      //   title: '温馨提示',
      //   message: '物资列表为空,将前往添加!',
      //   confirmButtonColor: styleColor.baseColor
      // }).then(() => {
      //   show_choose_material.value = true;
      // });
      sum_num.value = list.value.length; // 品项数量
    } else {
      sum_num.value = list.value.length; // 品项数量
    }
  }
}

onMounted(async () => {
  // 获取列表
  getCartList();
  getDeptsList();
  getUserList();
  //
  minDate.value = new Date();
});

const is_all_checked = ref(false);

const sum_num = ref(0); // 品项的总数量
const model = ref(''); // 默认非编辑模式

const onClickEdit = () => { // 点击编辑
  model.value = 'edit';
  disabled_btn.value = true;
  list.value.forEach(item => {
    item.checked = false;
    item.error = false;
    item.edit = true;
  })
}
const onClickDel = () => { // 点击删除回调
  let shop_cart_list = [];
  // 把选中的值集合
  list.value
  .filter(item => item.checked)
  .forEach(item => {
    shop_cart_list = shop_cart_list.concat(item);
  });
  // 购物车为空提示
  if (!shop_cart_list.length) {
    showToast('删除项为空');
    return;
  }
  // 把选中的值集合
  let del_ids = shop_cart_list.map(item => item.good_id);
  // 删除接口
  showConfirmDialog({
    title: '温馨提示',
    message:
      `您是否需要删除这 ${del_ids.length} 项物资?`,
      confirmButtonColor: styleColor.baseColor,
  })
    .then(async () => {
      const { code, data } = await delCartAPI({ activity_id, good_ids: del_ids });
      if (code) {
        // 还原列表显示
        onClickCancel();
        // 获取购物车列表
        getCartList();
        //
        showToast('删除成功');
      }
    })
    .catch(() => {
      // on cancel
    });
}

const onClickCancel = () => { // 点击取消
  if (checkShoppingCart()) {
    model.value = '';
    disabled_btn.value = false;
    is_all_checked.value = false;
    list.value.forEach(item => {
      item.checked = false;
      item.error = false;
      item.edit = false;
    });
  } else {
    showToast('请检查输入项')
  }
}

const onCheckAll = () => {
  is_all_checked.value = !is_all_checked.value;
  if (is_all_checked.value) {
    list.value.forEach(item => {
      item.checked = true;
    })
  } else {
    list.value.forEach(item => {
      item.checked = false;
      item.error = false;
    })
  }
}

const onCheck = (item) => {
  item.checked = !item.checked;
  // 全部为选中
  let all_checked = list.value.every((item) => item.checked === true);
  if (all_checked) {
    is_all_checked.value = true;
  } else {
    is_all_checked.value = false;
  }
  // 如果取消选中,隐藏错误提示
  if (!item.checked) {
    item.error = false;
  }
}

const onBlur = async (item) => { // 输入框失去焦点回调
  if (item.apply_number === null || item.apply_number === '') {
    item.error = true;
  } else {
    // 输入框失去焦点,购物车更新数量
    item.error = false;
    const { code, data } = await editCartAPI({ activity_id, good_list: [{ good_id: item.good_id, apply_number: item.apply_number }] });
    if (code) {

    }
  }
}

const list = ref([]);

const material_id = ref('');
const onClickTitle = (item) => { // 点击物资标题回调
  show_material_detail.value = true;
  material_id.value = item.good_id;
}

const showPicker = ref(false);
const currentDate = ref([]);
const columns_type = ref(['year', 'month', 'day']);
const minDate = ref();
const maxDate = ref();
const use_time = ref('');

const onConfirm = ({ selectedValues, selectedOptions }) => {
  use_time.value = selectedValues.join("-");
  showPicker.value = false;
};

const addMore = () => { // 添加更多回调
  show_choose_material.value = true;
}

const shop_cart_list = ref([]); // 购物车列表

const checkShoppingCart = () => { // 检查购物车有效性
  let flag = true;
  // 校验购物车值
  let error_list = list.value.filter((item) => { if (item.apply_number === null || item.apply_number === '') { return item;} })
  if (error_list.length) {
    // 跳到第一个错误框
    scrollToSection(error_list[0].good_id);
    // 标记所有错误
    error_list.forEach(item => {
      item.error = true;
    });
    flag = false
    return;
  }
  return flag;
}

const onConfirmRequest = () => { // 确定申领回调
  if (!use_time.value) {
    showToast('请选择使用日期');
    return;
  }
  let sum = list.value.some((item) => { return +item.apply_number > 0  }); // 判断是否有一个值不等于0
  if (!sum) {
    showToast('物资数量都没有填写');
    return;
  } else {
    showType.value = true;
  }
}

const scrollToSection = (id) => { // 滚动到指定位置
  const current_index = list.value.findIndex((item) => item.good_id === id);
  // 因为布局问题,需要滚动到上一个节点 查询上一个节点ID
  const previous_index = current_index - 2 >= 0 ? current_index - 2 : 0;
  nextTick(() => {
    let element = $('.list-wrapper').find(`#${list.value[previous_index]['good_id']}`);
    if (element[0]) {
      element[0].scrollIntoView({ behavior: 'smooth' });
    } else {
      console.log('Element with ID ' + id + ' not found.');
    }
  })
}

const showType = ref(false);
const type_checked = ref(''); // 组别
const contact_name = ref(''); // 申领人
const contact_phone = ref(''); // 联系电话
const onDialogConfirm = async () => {
  if (!type_checked.value) {
    showToast('请选择组别')
  } else if (!contact_name.value) {
    showToast('申领人不能为空')
  } else if (!contact_phone.value || !contact_phone.value.match(/^1[3456789]\d{9}$/)) {
    showToast('联系电话填写有误')
  } else {
    // 提交申领物资接口
    const { code, data } = await addUseOrderAPI({ activity_id, dept_id: type_checked.value, use_time: use_time.value, contact_name: contact_name.value, contact_phone: contact_phone.value });
    if (code) {
      showType.value = false;
      // 申领成功跳转
      $router.push({
        path: '/success',
        query: {
          activity_id,
          form_id,
          client_id
        }
      });
    }
  }
}
const onDialogCancel = () => {
  showType.value = false;
}

const show_choose_material = ref(false);
const onCloseChoose = () => { // 关闭选择物资弹窗回调
  show_choose_material.value = false;
  // 刷新购物车列表
  getCartList();
}

const show_material_detail = ref(false);
const onCloseDetail = () => { // 关闭物资详情窗口
  show_material_detail.value = false;
}
</script>

<style lang="less" scoped>
.material-pre-request-page {
  .list-wrapper {
    position: relative;
    width: 100%;
    .select-all-item {
      padding: 0.5rem 1rem;
      background-color: white;
      border-bottom: 1px solid #f0f0f0;
    }
    .list-boxer {
      margin: 0; padding: 1rem;
    }
  }

  .control-bar {
    display: flex; position: fixed; padding: 1rem 1.5rem; left: 0; right: 0; bottom: 0; background-color: white; width: calc(100% - 3rem);  box-shadow: 0rem -0.33rem 0.33rem 0.08rem rgba(0,0,0,0.06); display: flex; flex-direction: column;
  }
}

:deep(.van-picker__confirm) {
  color: #A67939;
}
</style>