<template>
  <div class="app-container">
    <div class="card-change">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="" prop="tankName">
          <el-input v-model="queryParams.inventoryNumber" placeholder="请输入盘点单号"/>
        </el-form-item>
        <el-form-item label="" v-model="queryParams.tankId" prop="status">
          <el-select v-model="queryParams.tankId" placeholder="请选择盘点油罐" style="width:100%" clearable>
            <el-option
              v-for="(item,index) in tankList"
              :key="index"
              :label="item.tankName"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" v-model="queryParams.status" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" style="width:100%" clearable>
            <el-option
              v-for="dict in dict.type.oilPurchasedStatus"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-date-picker
            v-model="beginTime"
            style="width: 160px"
            type="date"
            placeholder="开始日期">
          </el-date-picker>
          至
          <el-date-picker
            v-model="endTime"
            style="width: 160px"
            type="date"
            placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item style="float: right; margin-right: 0px">
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
          <el-button
            type="primary"
            @click="purchaseAdd"
            v-hasPermi="['oilConfig:oilInventory:list']"

          >新增盘点
          </el-button>
        </el-form-item>
      </el-form>

      <div style="height: 76vh;overflow: auto">
        <el-table ref="tables"
                  v-loading="loading"
                  :data="inventoryList"
                  border
                  :default-sort="defaultSort">
          <el-table-column align="center" type="index" label="序号"></el-table-column>
          <el-table-column label="盘点单号" align="center" prop="inventoryNumber"/>
          <el-table-column label="盘点油罐" align="center" prop="tanks"/>
          <el-table-column label="盘点升数" align="center" prop="inventoryVolume"/>
          <el-table-column label="库存差异升数" align="center" prop="stockDifference"/>
          <el-table-column label="盈亏金额（元）" align="center" prop="profitLossAmount" width="100"/>
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <div slot="reference" class="name-wrapper">
                <div style="color: #00CAFF" v-if="scope.row.status == 'await'">待审核</div>
                <div style="color: #0DC291" v-if="scope.row.status == 'qrts'">已审核入库</div>
                <div style="color: #F44522" v-if="scope.row.status == 'yzf'">已作废</div>
                <div style="color: #FF770F" v-if="scope.row.status == 'ysh'">已审核</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="制单人" align="center" width="60" prop="creatorName"/>
          <el-table-column label="审核人" align="center" width="60" prop="reviewerName"/>
          <el-table-column label="入库人" align="center" width="60" prop="reviewerName"/>

          <el-table-column label="制单时间" align="center" prop="orderTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.orderTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="审核时间" align="center" prop="orderTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.orderTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="入库时间" align="center" prop="orderTime" width="160">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.orderTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                v-if="scope.row.status == 'ysh'"
                @click="dispose(scope.row)"
              >盘点入库
              </el-button>
              <el-button
                size="mini"
                type="text"
                v-else-if="scope.row.status == 'await'"
                @click="dispose(scope.row)"
              >盘点审核
              </el-button>
              <el-button
                size="mini"
                type="text"
                v-else
                @click="dispose(scope.row)"
              >查看详情
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.page"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import {getPurchaseApi, getPurchaseOrderApi} from "@/api/oilConfig/oilPurchase";
import {getSuppliersListApi2} from "@/api/oilConfig/oilSuppliers";
import {delInventoryApi, getInventoryApi, selectList2Api} from "@/api/oilConfig/oilInventory";
import {getName, parseTime} from "../../../utils/fuint";

export default {
  name: "list",
  dicts: ['oilPurchasedStatus'],

  data() {
    return {
      inventoryList: [],

      purchaseList: [],

      suppliersList: [],
      // 弹出框标题
      title: '',
      // 显示搜索条件
      showSearch: true,
      disableInput: false, // 默认不禁用
      // 是否显示弹出层
      open: false,
      // 总条数
      total: 0,
      tankList: [],
      // dateRange: '',
      // 查询参数
      queryParams: {
        inventoryNumber: '',
        status: '',
        page: null,
        pageSize: null
      },
      dateRange: [],
      beginTime: "",
      endTime: "",
      // 遮罩层
      loading: false,
      // 默认排序
      defaultSort: {prop: 'createTime', order: 'descending'},
      // 表单校验
      rules: {
        supplierName: [
          {required: true, message: "供应商名称不能为空", trigger: "blur"},
        ],
      }
    }
  },

  async created() {
    await this.getList();
    this.getTankList();
  },
  methods: {

    // 油罐列表
    getTankList() {
      selectList2Api().then(response => {
        this.tankList = response.data
      })
    },
    // 获取列表
    getList() {
      let dateRange = []
      if (this.beginTime && this.endTime) {
        dateRange.push(this.beginTime.toLocaleDateString())
        dateRange.push(this.endTime.toLocaleDateString())
      }

      this.loading = true;
      getInventoryApi(this.addDateRange(this.queryParams, dateRange)).then(response => {
        this.inventoryList = response.data.records;
        console.log("this.inventoryList", this.inventoryList)
        this.total = response.data.total;
      })
      this.loading = false;
    },

    // 添加采购
    purchaseAdd() {
      this.$router.push('/oilInventory/order');
    },
    // 处理单据
    dispose(data) {
      this.$router.push({
        path: '/oilInventory/order',
        query: {
          inventoryId: data.id,
          inventoryNo: data.inventoryNumber,
          inventoryDate: data.inventoryTime,
          state: data.status
        }
      });
    },
    // 删除
    handleDel(data) {
      delInventoryApi(data).then(response => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      })
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = {
        inventoryNumber: '',
        status: '',
        page: null,
        pageSize: null
      };
      this.getList();
    },
  }
}
</script>

<style scoped lang="scss">
.app-container {
  width: 100%;
  height: 100%;
  background: #f6f8f9;
}

.el-form--inline .el-form-item {
  margin-right: 44px;
}

</style>
