<template>
  <div class="app-container">
    <div class="card-change">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
        <el-form-item label="" prop="purchaseNo">
          <el-input v-model="queryParams.purchaseNo" placeholder="请输入进货单号"/>
        </el-form-item>
        <el-form-item label="" prop="supplierId">
          <el-select v-model="queryParams.supplierId" placeholder="请选择供应商" clearable>
            <el-option
              v-for="option in suppliersList"
              :key="option.id"
              :label="option.supplierName"
              :value="option.id"
            ></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="" prop="numberId">
          <el-select v-model="queryParams.numberId" placeholder="请选择油品-油号">
            <el-option
              v-for="option in selectOilTypeByPrice"
              :key="option.numberId"
              :label="option.oilType +' '+option.oilName"
              :value="option.numberId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="" v-model="queryParams.status" prop="status">
          <el-select v-model="queryParams.status" placeholder="请选择状态" 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 label="">
          <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:oilPurchase:list:add']"
          >新增采购
          </el-button>
        </el-form-item>
      </el-form>
      <div style="height: 73vh;overflow: auto;scrollbar-width: none">
        <el-table ref="tables"
                  v-loading="loading"
                  :data="purchaseList"
                  border
                  :default-sort="defaultSort">
          <el-table-column label="序号" align="center" type="index"/>
          <el-table-column label="进货单号" align="center" width="150px" prop="purchaseNo"/>
          <el-table-column label="供应商" align="center" prop="supplierss"/>
          <el-table-column label="油品-油号" align="center" prop="oils"/>
          <el-table-column label="入库油罐" align="center" prop="tanks"/>
          <el-table-column label="单据金额" align="center" prop="theDocumentAmount"/>
          <el-table-column label="状态" align="center" prop="status">
            <template slot-scope="scope">
              <div slot="reference" class="name-wrapper">
                <div style="color: #FF770F" 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: #00CAFF" v-if="scope.row.status == 'ysh'">已审核</div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="制单人" align="center" prop="creatorName"/>
          <el-table-column label="审核人" align="center" prop="reviewerName"/>
          <el-table-column label="入库人" align="center" prop="reviewerName"/>
          <el-table-column label="制单时间" align="center" prop="createdAt" width="200">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createdAt) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="审核时间" align="center" prop="createdAt" width="200">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createdAt) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="入库时间" align="center" prop="createdAt" width="200">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createdAt) }}</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 == 'await'"
                @click="dispose(scope.row)"
              >油品入库
              </el-button>
              <el-button
                size="mini"
                type="text"
                v-else-if="scope.row.status == 'ysh'"
                @click="dispose(scope.row)"
              >采购审核
              </el-button>
              <el-button
                size="mini"
                type="text"
                v-else
                @click="dispose(scope.row)"
              >查看详情
              </el-button>
              <!--            <el-button-->
              <!--              size="mini"-->
              <!--              type="text"-->
              <!--              icon="el-icon-copy-document"-->
              <!--              @click="handleDel(scope.row.id)"-->
              <!--            >删除</el-button>-->
              <!--            v-hasPermi="['']"-->
            </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 {getName, parseTime} from "../../../utils/fuint";
import {getOilNameListApi} from "@/api/oilPrice";


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

  data() {
    return {
      purchaseList: [],
      suppliersList: [],
      // 弹出框标题
      title: '',
      // 显示搜索条件
      showSearch: true,
      disableInput: false, // 默认不禁用
      // 是否显示弹出层
      open: false,
      // 总条数
      total: 0,

      dateRange: '',
      // 查询参数
      queryParams: {
        supplierId: '',
        tankName: '',
        status: '',
        page: null,
        pageSize: null
      },
      selectOilTypeByPrice: [],
      beginTime: "",
      endTime: "",
      // 遮罩层
      loading: false,
      // 默认排序
      defaultSort: {prop: 'createTime', order: 'descending'},
      // 表单校验
      rules: {
        supplierName: [
          {required: true, message: "供应商名称不能为空", trigger: "blur"},
        ],
      }
    }
  },

  async created() {
    await this.getList();
    this.selectSuppliers();
    this.getOilNameList();
  },
  methods: {
    getOilNameList() {
      var this_ = this
      getOilNameListApi().then(response => {
        var list = response.data
        this_.selectOilTypeByPrice = list
      });
    },
    // 获取列表
    getList() {
      this.loading = true;
      let dateRange = []
      if (this.beginTime && this.endTime) {
        dateRange.push(this.beginTime.toLocaleDateString())
        dateRange.push(this.endTime.toLocaleDateString())
      }
      getPurchaseApi(this.addDateRange(this.queryParams, dateRange)).then(response => {
        this.purchaseList = response.data.records;
        console.log("this.tankList", this.purchaseList)
        this.total = response.data.total;
      })
      this.loading = false;
    },
    // 添加采购
    purchaseAdd() {
      this.$router.push('/oilPurchase/purchaseOrder');
    },
    // 处理单据
    dispose(data) {
      this.$router.push({
        path: '/oilPurchase/purchaseOrder',
        query: {
          purchaseId: data.id,
          purchaseNo: data.purchaseNo,
          purchaseDate: data.purchaseDate,
          state: data.status
        }
      });
    },
    handleDel(data) {
    },
    // 查询所有供应商
    selectSuppliers() {
      getSuppliersListApi2().then(response => {
        this.suppliersList = response.data;
      });
    },
    // 搜索按钮操作
    handleQuery() {
      this.queryParams.page = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = {
        supplierId: '',
        tankName: '',
        status: '',
        page: null,
        pageSize: null
      };
      this.beginTime = ''
      this.endTime = ''
      this.getList();
    },
  }
}
</script>

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

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

</style>
