<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="isedit"
              placeholder="请输入仓库命名"
            />
            <van-field
              v-model="whdata.value.manageUnit"
              label="管理单位"
              input-align="right"
              :readonly="isedit"
              placeholder="请输入管理单位"
            />
            <van-field
              v-model="whdata.value.contactName"
              required
              :readonly="isedit"
              label="联系人"
              input-align="right"
              placeholder="请输入联系人"
            />
            <van-field
              v-model="whdata.value.contactPhone"
              required
              label="联系方式"
              :readonly="isedit"
              input-align="right"
              placeholder="请输入联系方式"
            />
          </van-cell-group>
        </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)">删除</div>
          </div>
          <van-cell-group style="">
            <van-field
              v-model="item.name"
              label="名称"
              input-align="right"
              placeholder="请输入名称"
            />
            <van-field
              v-model="item.model"
              label="型号"
              input-align="right"
              placeholder="请输入型号"
            />
            <van-field
              v-model="item.size"
              label="规格"
              input-align="right"
              placeholder="请输入规格"
            />
            <van-field
              v-model="item.num"
              label="仓储数量"
              type="number"
              input-align="right"
              placeholder="请输入数量"
            />
            <van-field
              v-model="item.availableNum"
              label="可用数量"
              type="number"
              input-align="right"
              placeholder="请输入数量"
            />
            <van-field
              v-model="item.unit"
              label="单位"
              input-align="right"
              placeholder="请输入单位"
            />
          </van-cell-group>
        </div>
      </div>
      <div style="text-align: center">
        <van-button
          @click="doaddwzClick"
          icon="plus"
          style="color: #0b33a8; background: rgba(0, 0, 0, 0); border: none"
          >新增物资</van-button
        >
      </div>

      <div style="padding: 10px 20px">
        <van-button
          type="primary"
          block
          style="border-radius: 30px"
          @click="dosavedata"
          >提交</van-button
        >
      </div>
    </div>
    <div><van-empty v-if="isempt" description="无仓库" /></div>
  </div>
</template>

<script setup>
import { ref, reactive, onUpdated,onMounted } from 'vue';
import { RouterView, useRoute, useRouter } from 'vue-router';
import {
  api_getresource,
  api_getresourcedetail,
  api_putresource,
  api_bj,api_isbj
} from '@/service/warehouse';
import { Toast, Notify,Dialog } from 'vant';

const whdata = reactive({
  value: { name: '', manageUnit: '', contactName: '', contactPhone: '' },
});
const isedit = ref(true);
const isempt = ref(true);
// let  deletwzarr = [];
const wzdata = reactive({ aar: [] });


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 doaddwzClick = () => {
  var wz = {
    name: '',
    model: '',
    size: '',
    num: 0,
    unit: '',
    availableNum: '',
  };
  wzdata.aar.push(wz);
};
const editclick = () => {
  isedit.value = false;
};
const deletewz = (index) => {
  if (index >= wzdata.aar.length) {
    return;
  }
  wzdata.aar.splice(index, 1);
};

const goback = () => {
  try {
    uni.navigateBack();
  } catch (E) {}
};
const dosavedata = () => {
  Toast.loading({
    message: '加载中...',
    duration: 0,
    forbidClick: true,
  });
  var data = whdata.value;
  data['resourceDetailList'] = wzdata.aar;
  api_putresource(data)
    .then((res) => {
      Toast.clear();
      setTimeout(() => {
        Toast('保存成功');
      }, 1000);

      // Notify({ type: 'success', message: '保存成功'});
      getdata();
    })
    .catch((ee) => {
      Toast.clear();
    });
};

const isbj = ref(true);

const route = useRoute();
onMounted(()=>{
  var taskid =  route.query.taskid;
  if(taskid){
    api_isbj(taskid).then((res)=>{
     
     
        isbj.value = !res.data;
        console.log(isbj.value)
      
    })
  }

})

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")
        whdata.value = res.rows[0];
        api_getresourcedetail(whdata.value.id).then((res1) => {
          if (
            res1.data.resourceDetailList == null ||
            res1.data.resourceDetailList.length == 0
          ) {
            // debugger
            Toast('仓库还没有物资，请添加物资！');
          } else {
            wzdata.aar = res1.data.resourceDetailList;
            // console.log(whdata)
          }
        });
      } else {
        Toast('未获取到仓库信息，请联系管理员！');
      }
    })
    .catch((eer) => {
      Toast.clear();
    });
};
getdata();

// onUpdated(()=>{
//     //根据管理单位获取仓库信息
//     api_getresource({}).then((res)=>{

//     })

// });
</script>

<style lang="scss">
.content label,
.content span {
  font-size: 15px;
  font-family: PingFangSC, PingFangSC-Regular;
  color: #333333;
}
.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;
    }
  }
}
</style>
