<template>
  <div class="dark:!bg-[#060818] bg-white p-3">
    <div class="search bg-white p-3">
      <el-form :model="advancedForm" label-width="160px">
        <el-row>
          <el-col :span="6">
            <el-form-item :label="$t('loss_date')">
              <el-date-picker
                v-model="advancedForm.goodsInventory_time_range"
                type="daterange"
                align="center"
                :editable="false"
                unlink-panels
                :clearable="true"
                :default-time="['00:00:00', '23:59:59']"
                range-separator="-"
                :start-placeholder="$t('start_time')"
                :end-placeholder="$t('end_time')"
                value-format="YYYY-MM-DD HH:mm:ss"
                :picker-options="pickerOptions"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item :label="$t('warechouse')">
              <el-select v-model="advancedForm.warehouse_id">
                <el-option v-for="item in warehouseList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item :label="$t('loss_people')">
              <el-input v-model="advancedForm.creator" :placeholder="$t('place') + $t('loss_people')" width="200px"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="flex justify-end">
        <el-button @click="advancedSearchEvent"> {{ $t('search') }} </el-button>
      </div>
    </div>
    <en-table-layout :tableData="tableData.data" :loading="loading" @selection-change="handleSelectionChange">
      <template #toolbar>
        <div class="inner-toolbar">
          <div class="toolbar-btns">
            <!-- v-if="checkPermission(['goodsInventoryLoss:add'])" -->
            <el-button size="small" type="primary" v-if="store.state.user.user.founder !== 1" @click="handleAddGoodsInventoryLoss">{{ $t('add') }}</el-button>
          </div>
        </div>
      </template>

      <template #table-columns>
        <el-table-column type="selection" width="55" />
        <el-table-column prop="sn" :label="$t('loss_sn')">
          <template #default="scope">
            <span>{{ scope.row.sn }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('warechouse')">
          <template #default="scope">{{ warehouseName(scope.row.warehouse_id) }}</template>
        </el-table-column>
        <el-table-column prop="status" :label="$t('warehousing_state')">
          <template #default="scope">{{ statusFilter(scope.row.status) }}</template>
        </el-table-column>
        <el-table-column prop="report_desc" :label="$t('loss_desc')" />
        <el-table-column :label="$t('operate')" fixed="right" width="450">
          <template #default="scope">
            <!-- v-if="checkPermission(['goodsInventoryLossDetail'])" -->
            <el-button type="mini" size="small" @click="handleDetailGoodsInventoryLoss(scope.row)">{{ $t('view') }}</el-button>
            <!--  && checkPermission(['goodsInventoryLossEdit']) -->
            <el-button
              type="mini"
              size="small"
              v-if="scope.row.status == 'NOT_HANDLE' && store.state.user.user.founder !== 1"
              @click="handleSubmitGoodsInventoryLoss(scope.row)"
              >{{ $t('review') }}</el-button
            >
          </template>
        </el-table-column>
      </template>
      <template #pagination>
        <el-pagination
          v-if="tableData"
          @size-change="handlePageSizeChange"
          @current-change="handlePageCurrentChange"
          :current-page="tableData.page_no"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="tableData.page_size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.data_total"
        >
        </el-pagination>
      </template>
    </en-table-layout>
  </div>
</template>
  
  <script lang="ts" setup>
import { ref, onMounted, watch } from 'vue';
import { useRouter, useRoute } from 'vue-router';
import { ElMessage, ElMessageBox } from 'element-plus';
import * as API_GoodsInventoryLoss from '@/api/goodsInventoryLoss';
import * as API_Setting from '@/api/setting';
import * as API_BasicSetting from '@/api/basicSetting';
/* import { Foundation } from '~/ui-utils'; */
import { useStore } from 'vuex';
const store = useStore();
const router = useRouter();
const route = useRoute();

const warehouseList = ref<any>([]);
const deptList = ref([]);
const loading = ref(false);
const params = ref<any>({
  page_no: 1,
  page_size: 10,
  company_id: store.state.companId,
});
const advancedForm = ref<any>({
  goodsInventory_time_range: [],
});
const tableData = ref<any>([]);
const multipleSelection = ref([]);
const inventoryType = ref([]);

const GET_GoodsInventoryLossList = () => {
  loading.value = true;
  API_GoodsInventoryLoss.getGoodsInventoryLossList(params.value)
    .then((response) => {
      loading.value = false;
      tableData.value = response;
    })
    .catch(() => {
      loading.value = false;
    });
};
/* 时间选择限制器 */
const pickerOptions = {
  disabledDate(time: Date) {
    return time.getTime() - 1 >= new Date(new Date().getFullYear(), new Date().getMonth(), new Date().getDate()).getTime() + 86400000 - 1;
  },
  shortcuts: [
    {
      text: '今天',
      onClick(picker: any) {
        const end = new Date();
        const start = new Date(new Date().toDateString());
        picker.$emit('pick', [start, end]);
      },
    },
    {
      text: '最近一周',
      onClick(picker: any) {
        const end = new Date();
        const start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
        picker.$emit('pick', [start, end]);
      },
    },
    {
      text: '最近一个月',
      onClick(picker: any) {
        const end = new Date();
        const start = new Date();
        start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
        picker.$emit('pick', [start, end]);
      },
    },
  ],
};

const GET_DeptList = () => {
  /*  API_Setting.getDeptList().then((response) => {
    deptList.value = Foundation.buildTree(response, '0');
  }); */
};

const GET_WarehouseList = () => {
  API_BasicSetting.getWarehouseListAll().then((response) => {
    warehouseList.value = response;
  });
  API_Setting.getPage({ dictType: 'inventory_status' }).then((res) => {
    inventoryType.value = res.data;
  });
};

const handleDetailGoodsInventoryLoss = (row) => {
  router.push({
    name: 'goodsInventoryLossAdd',
    query: { id: row.id, type: 'view' },
  });
};

const warehouseName = (val) => {
  let name = '';
  warehouseList.value.map((el: any) => {
    if (el.id == val) {
      name = el.name;
    }
  });
  return name;
};

const handleSubmitGoodsInventoryLoss = (row) => {
  router.push({
    name: 'goodsInventoryLossAdd',
    query: { id: row.id, type: 'examine' },
  });
};

const handleAddGoodsInventoryLoss = () => {
  router.push({
    name: 'goodsInventoryLossAdd',
  });
};

const statusFilter = (val) => {
  let name = '';
  inventoryType.value.map((el: any) => {
    if (el.value == val) {
      name = el.label;
    }
  });
  return name;
};

const handleSelectionChange = (val) => {
  multipleSelection.value = val;
};

const handlePageSizeChange = (size) => {
  params.value.page_size = size;
  GET_GoodsInventoryLossList();
};

const handlePageCurrentChange = (page) => {
  params.value.page_no = page;
  GET_GoodsInventoryLossList();
};

const advancedSearchEvent = () => {
  params.value = {
    ...params.value,
    ...advancedForm.value,
  };
  delete params.value.start_time;
  delete params.value.end_time;
  if (advancedForm.value.goodsInventory_time_range) {
    params.value.start_time = parseInt(String(Number(advancedForm.value.goodsInventory_time_range[0]) / 1000));
    params.value.end_time = parseInt(String(Number(advancedForm.value.goodsInventory_time_range[1]) / 1000));
  }
  delete params.value.goodsInventory_time_range;
  params.value.page_no = 1;
  GET_GoodsInventoryLossList();
};

onMounted(() => {
  GET_GoodsInventoryLossList();
  GET_DeptList();
  GET_WarehouseList();
});

/* watch(route, ({ query }) => {
  params.value.page_no = 1;
  GET_GoodsInventoryLossList();
}); */
</script>
  
  <!-- <style scoped lang="scss">
  .search {
    display: flex;
    align-items: center;
    background: #fff;
    overflow: hidden;
    padding: 30px 10px 10px 10px;
  
    .el-form-item {
      .el-form-item__content {
        .el-input {
          width: 200px;
        }
      }
    }
  
    .el-button {
      margin-top: -19px;
      margin-left: 20px;
    }
  }
  
  .print {
    margin-left: 790px;
  }
  
  .deliverySheet-header {
    display: flex;
    justify-content: center;
    align-items: center;
  }
  
  .tips-t {
    display: flex;
    padding: 30px 0;
  
    div {
      flex: 1;
      text-align: left;
      padding-left: 70px;
    }
  }
  
  .tips-f {
    padding: 20px 0;
  }
  
  .face-image {
    display: block;
    width: 50px;
    height: 50px;
    margin: 0 auto;
  }
  
  .el-dialog__body {
    padding: 10px 20px;
  }
  </style> -->