<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <div class="query-box">
        <div class="query-box__left">
          <el-form-item label="" prop="keepingTime">
            <el-date-picker style="width:340px" v-model="queryParams.date"
                            type="datetimerange" value-format="yyyy-MM-dd HH:mm:ss"
                            start-placeholder="开始时间" end-placeholder="结束时间"
                            size="small" clearable
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="" prop="orderStatus">
            <el-select style="width:160px" v-model="queryParams.orderStatus" placeholder="请选择状态" size="small"
                       clearable>
              <el-option v-for="item in orderOptions" :key="item.dictValue" :value="item.dictValue"
                         :label="item.dictLabel"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="" prop="erpNo">
            <el-input style="width:160px" v-model="queryParams.erpNo" placeholder="请输入ERP单号" size="small" clearable
                      @keyup.enter.native="handleQuery"
            ></el-input>
          </el-form-item>
          <el-form-item label="" prop="orderNo">
            <el-input style="width:160px" v-model="queryParams.orderNo" placeholder="请输入订单号" size="small"
                      clearable
                      @keyup.enter.native="handleQuery"
            ></el-input>
          </el-form-item>
          <el-form-item label="" prop="supplierId">
            <el-select style="width:160px" v-model="queryParams.supplierId" placeholder="请选择供货单位" size="small"
                       clearable>
              <el-option v-for="item in supplierOptions" :key="item.id" :value="item.id" :label="item.name"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </div>
      </div>
    </el-form>

    <el-table v-loading="loading" :data="list" size="mini" border>
      <el-table-column type="index" width="55" align="center" label="序号"/>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="ERP单号" align="center" prop="erpNo"/>
      <el-table-column label="订单号" align="center" prop="orderNo"/>
      <el-table-column label="状态" align="center" prop="orderStatus">
        <template slot-scope="scope">
          <dict-tag :options="orderOptions" :value="scope.row.orderStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="药房" align="center" prop="pharmacyName"/>
      <el-table-column label="供货单位" align="center" prop="supplierName"/>
      <el-table-column label="计划入库" align="center" prop="planWarehousingAmount"/>
      <el-table-column label="实际入库" align="center" prop="actualWarehousingAmount"/>
      <el-table-column label="计划金额" align="center" prop="planPrice"/>
      <el-table-column label="实际金额" align="center" prop="actuaPrice"/>
      <el-table-column label="操作人" align="center" prop="updateBy"/>
      <el-table-column label="操作时间" align="center" prop="updateTime"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220" fixed="right">
        <template slot-scope="scope">

          <template v-if="scope.row.orderStatus === '0'">
            <el-button style="width: 40px;" type="primary" size="mini"
                       v-hasPermi="['inventory:erp:edit']"
                       @click="handleEdit(scope.row)"
            >接收
            </el-button>
            <el-button style="width: 40px;" type="danger" size="mini"
                       v-hasPermi="['inventory:erp:remove']"
                       @click="handleDelete(scope.row)"
            >作废
            </el-button>
          </template>
          <template v-if="scope.row.orderStatus !== '0'">
            <el-button style="width: 40px;" type="default" size="mini"
                       v-hasPermi="['inventory:erp:preview']"
                       @click="handlePreview(scope.row)"
            >查看
            </el-button>
          </template>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改入库单对话框 -->
    <el-dialog :fullscreen="true" :title="title" :visible.sync="open" width="1180px" append-to-body size="mini">
      <edit v-if="model.id" :id="model.id" :editable="editable" @close="open = false;$event && getList()" @delete="handleDelete"></edit>
    </el-dialog>
  </div>
</template>

<script>
import {listSupplier} from "@/api/pharmacy/supplier";
import selectTable from '@/components/selectTable/selectTable.vue'
import dayjs from "dayjs";

import {erpDelete, erpList} from "@/api/inventory/erp";
import Edit from "@/views/erp/edit.vue";

export default {
  name: "ErpList",
  components: {
    Edit,
    selectTable
  },
  data() {
    return {
      list: [],
      orderOptions: [
        {dictLabel: '待接收', dictValue: '0'},
        {dictLabel: '已接收', dictValue: '1'},
        {dictLabel: '已作废', dictValue: '2'},
      ],
      supplierOptions: [],
      model: {},
      editable: false,
      // 选中数组
      ids: [],
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        pharmacyId: window.localStorage.getItem("pharmacyId"),
        erpNo: '',
        orderNo: '',
        orderStatus: '',
        supplierId: '',
        date: [],
      },
    };
  },
  created() {
    // this.getDicts("bill_state")
    this.getListSupplier();
    this.resetQuery();
  },
  methods: {
    getList() {
      this.loading = true;
      erpList(this.queryParams).then((response) => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      }).catch((error) => { this.loading = false; });
      this.model = {};
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.date = [
        dayjs().startOf('day').format('YYYY-MM-DD HH:mm:ss'),
        dayjs().endOf('day').format('YYYY-MM-DD HH:mm:ss'),
      ]
      this.handleQuery();
    },

    handlePreview(row) {
      this.model = {...row};
      this.editable = false;
      this.open = true;
    },
    handleEdit(row) {
      this.model = {...row};
      this.editable = true;
      this.open = true;
    },
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否作废该订单？", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => erpDelete(ids))
        .then(() => {
          this.open = false;
          this.getList();
          this.msgSuccess("作废成功");
        })
        .catch(() => {});
    },
    //供货单位
    getListSupplier() {
      listSupplier().then((res) => {
        this.supplierOptions = res.rows;
      });
    },
  },
};
</script>
<style scoped>
.el-input--small .el-input__inner {
  height: 36px;
  line-height: 36px;
}
</style>
