<template>
  <div>
    <van-nav-bar
      title="应急物资"
      left-arrow
      placeholder
      @click-left="goback"
      :fixed="true"
      @click-right="bjclick"
    >
      <template #right>
        <span v-if="!isbj" class="bjbtn">办结</span>
      </template>
    </van-nav-bar>
    <div v-if="!isempt">
      <div class="content">
        <div>
          <van-cell-group>
            <van-field
              v-model="whdata.value.name"
              label="仓库名称"
              input-align="right"
              :readonly="true"
              placeholder="请输入仓库命名"
            />
            <!-- <van-field
              v-model="whdata.value.manageUnit"
              label="管理单位"
              input-align="right"
              :readonly="true"
              placeholder="请输入管理单位"
            />
            <van-field
              v-model="whdata.value.contactName"
              :readonly="true"
              label="联系人"
              input-align="right"
              placeholder="请输入联系人"
            />
            <van-field
              v-model="whdata.value.contactPhone"
              label="联系方式"
              :readonly="true"
              input-align="right"
              placeholder="请输入联系方式"
            /> -->
          </van-cell-group>
        </div>
         <div style="text-align: right;background: #fff;position: relative;height:55px" class="addnewwz" v-if="isedit">
           <van-row>
              <van-col span="16">
                 <van-search v-model="searchv" placeholder="请输入物资关键词" />
              </van-col>
               <van-col span="8">
                  <van-button
          @click="addwz"
          icon="plus"
          style="color: #0b33a8; background: rgba(0, 0, 0, 0); border: none;margin-top:6px"
          >新增物资</van-button
        >
               </van-col>

           </van-row>
          
       
      </div>

         <div style="margin:10px 0px;background:#fff;font-size:14px;padding:10px 10px;position:relative" @click="godetail(item)" v-for="(item, index) in (wzdata.aar.filter(ii => searchv===''||ii.name.indexOf(searchv)!=-1))" v-bind:key="'good'+index">
          <div>
              
              <div style="font-size:14px;"><span style="color:#646566">物资名称：</span>{{item.name}} (<span style="font-size:12px;">规格:{{item.size}}</span><span style="font-size:12px;margin-left:10px">型号:{{item.model}}</span>)</div>
               <div style="margin-top:10px">
               <div style="font-size:14px;display:inline-block"><span style="color:#646566">可用数量：</span>{{item.availableNum}}{{item.unit}}</div>
              <div style="font-size:14px;display:inline-block;margin-left:20px" ><span style="color:#646566">仓储数量：</span>{{item.num}}{{item.unit}}</div>
             
               </div>
             
              <div style="position: absolute;right: 10px;top: 50%;margin-top: -6px;">
                <van-icon name="arrow" />
              </div>
          </div>

        </div>


        <!-- <div v-for="(item, index) in wzdata.aar" v-bind:key="index" >
          <div class="cell-header">
            <span>应急物资（{{ index + 1 }}）</span>
            <div style="float: right" @click="deletewz(index)" v-if="isedit">删除</div>
          </div>
          <van-cell-group style="">
            <van-field
              v-model="item.name"
              label="名称"
              :readonly="!isedit"
              input-align="right"
              placeholder="请选择名称"
            >
             <template #button>
               <van-button size="small" @click="selectwz(index)" type="primary" style="color:white">选择</van-button>
             </template>
            </van-field>
            <van-field
              v-model="item.model"
              label="型号"
               :readonly="!isedit"
              input-align="right"
              placeholder="请输入型号"
            />
            <van-field
              v-model="item.size"
              label="规格"
               :readonly="!isedit"
              input-align="right"
              placeholder="请输入规格"
            />
            <van-field
              v-model="item.num"
              label="仓储数量"
               :readonly="!isedit"
              type="number"
              input-align="right"
              placeholder="请输入数量"
            />
            <van-field
              v-model="item.availableNum"
              label="可用数量"
               :readonly="!isedit"
              type="number"
              input-align="right"
              placeholder="请输入数量"
            />
            <van-field
              v-model="item.unit"
              label="单位"
               :readonly="!isedit"
              input-align="right"
              placeholder="请输入单位"
            />
            <van-field name="uploader" label="图片" input-align="right">
               <template #input>
                 <van-uploader :max-count="1" v-model='item.pic' :before-read="beforeRead" :disabled="!isedit" />
               </template>
             </van-field>
          </van-cell-group>
        </div> -->
      </div>
     


      <div style="" v-if="false">
        <van-sticky :offset-bottom="20" position="bottom" >
          <div style="background:#fff;width:100%;width: 100%;padding: 20px 10px;text-align: center;">
             

          </div>
          

        </van-sticky>
       
      </div>
      <!-- <van-popup v-model:show="showPicker" position="bottom">
          <van-picker
            :columns="goodlist"
            :default-index ="indexp"
            @confirm="onConfirm"
            @cancel="showPicker = false"
          />
      </van-popup> -->
    </div>
    
    <div v-if="isempt">
      <van-cell-group>
            <van-field
              v-model="whdata.value.name"
              label="仓库名称"
              input-align="right"
              :readonly="true"
              placeholder="请输入仓库命名"
            /></van-cell-group>
      <van-empty v-if="isempt" description="无物资" />
         <van-button
          type="primary"
          block
          style="border-radius: 30px;width: 80%;margin: auto;"

          @click="addwz"
          >新增物资</van-button
        >
      </div>
  </div>
</template>

<script setup>
import { ref, reactive, onUpdated,onMounted,watch } from 'vue';
import { RouterView, useRoute, useRouter } from 'vue-router';
import {
  api_getresource,
  api_getresourcedetail,
  api_putresource,
  api_bj,api_isbj,
  api_addresourcegood,
  api_putresourcegood,
  api_delresourcegood
} from '@/service/warehouse';
import { Toast, Notify,Dialog } from 'vant';
import { BaseMediaUrl } from '@/utils';
import { upload } from '@/api/common';
import { cloneDeep } from 'lodash';

import { useCommonStore } from '@/store';
import router from '@/router';



const whdata = reactive({
  value: { name: '', manageUnit: '', contactName: '', contactPhone: '' },
});

const searchv = ref("")

const isedit = ref(true);
const isempt = ref(true);
// let  deletwzarr = [];
const wzdata = reactive({ aar: [] });



const godetail =(item)=>{
  localStorage.setItem("wzdata",JSON.stringify(item));
  router.push("/warehouse/detailgoods");
  // window.location.reload();
}

const addwz = ()=>{
  localStorage.setItem("wzdata",JSON.stringify({
    name: '',
    id:"",
    model: '',
    size: '',
    num: 0,
    unit: '',
    availableNum: 0,
    pic:"",
    resourceId:whdata.value.id
}));
router.push("/warehouse/detailgoods");
// window.location.reload();
}



const bjclick = () => {
  if(isbj.value){
    return;
  }
  Dialog.confirm({
    title: '提示',
    confirmButtonText: '办结',
    message: '确认是否办结?',
  })
    .then(() => {
      // on confirm
      Toast.loading({
        message: '加载中...',
        duration: 0,
        forbidClick: true,
      });
      api_bj({ taskId: route.query.taskid }).then((res) => {
        Toast.clear();
        try {
          Toast.message("成功")
          uni.navigateBack();
        } catch (E) {
        }
      });
      Toast.clear();
    })
    .catch(() => {
      // on cancel
      Toast.clear();
    });
};


const goback = () => {
  try {
    uni.navigateBack();
  } catch (E) {}
   //window.history.back();
};

const isbj = ref(true);

const route = useRoute();
onMounted(()=>{
  try{
    var p = JSON.parse(localStorage.getItem('cck'));
    if(p){
       whdata.value = p;
    }
  }catch(r){
  }
   isedit.value = localStorage.getItem("cedit")=="true"?true:false;
   getdata();
   try{
     var taskid =  route.query.taskid;
     if(taskid){
      api_isbj(taskid).then((res)=>{
          isbj.value = !res.data;
      })
     }
    }catch(e){}

})

const getdata = () => {
  Toast.loading({
    message: '加载中...',
    duration: 0,
    forbidClick: true,
  });
 
  api_getresource({
    resourceType: 1,
  })
    .then((res) => {
      Toast.clear();
      if (res.rows.length > 0) {
        isempt.value = false;
        // whdata.value = res.rows.find(i=>i.id=="1481549762984415233")
        
        if(whdata.value.id==undefined||whdata.value.id==null||whdata.value.id==""){
           whdata.value = res.rows[0];
        }
        if(whdata.value.name==undefined||whdata.value.name==null||whdata.value.name==""){
          whdata.value.name ="";
        }
       
        api_getresourcedetail(whdata.value.id).then((res1) => {
          if (res1.data==null||
            res1.data.resourceDetailList == null ||
            res1.data.resourceDetailList.length == 0
          ) {
            // debugger
            isempt.value = true;
            // Toast('仓库还没有物资，请添加物资！');
          } else {
             isempt.value = false;
            // res1.data.resourceDetailList.map(item=>{
            //   if(item.pic!="" && item.pic!=null){
            //     item.pic = [{url:BaseMediaUrl+item.pic, deletable: isedit.value,}]
            //   }else{
            //      item.pic = []
            //   }
              
            // }) 
            wzdata.aar = cloneDeep( res1.data.resourceDetailList);

            // console.log(whdata)
          }
        });
      } else {
        Toast('未获取到仓库信息，请联系管理员！');
      }
    })
    .catch((eer) => {
      Toast.clear();
    });
};

</script>

<style lang="scss">
.content label,
.content span {
  // font-size: 12px;
  // font-family: PingFangSC, PingFangSC-Regular;
  // color: #fff;
}
.addnewwz .van-search__content{
  height: 100%;
}
.addnewwz .van-button__content span{
  color: rgb(11, 51, 168);
  font-size: 12px;
}
.van-field__button .van-button__content span{
  color: #fff;
  font-size: 12px;
}
.bjbtn {
  font-size: 14px;
  font-weight: 500;
  color: #ffffff;
  font-size: 13px;
  font-weight: 500;
  background: #bb0000;
  padding: 3px 8px;
  border-radius: 2px;
}
.content {
  .cell-header {
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    line-height: 42px;
    padding: 0px 15px;
    span {
      font-size: 12px;
    }
    div {
      font-size: 14px;
      color: #0b33a8;
    }
  }
}
:root{
  --van-disabled-opacity:1

}

</style>
