<template>
  <div class="common-box">
    <!--条件-->
    <el-form :model="listQuery" ref="queryForm" :inline="true">
      <el-form-item label="单据编号">
        <el-input
          v-model="listQuery.id"
          placeholder="请输入单据编号"
        ></el-input>
      </el-form-item>
      <el-form-item label="计划类型">
        <el-select
          v-model="listQuery.planType"
          placeholder="请选择计划类型"
          clearable
        >
          <el-option
            v-for="item in planTypes"
            :key="item.dicCode"
            :label="item.dicVal"
            :value="item.dicCode"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="提交时间">
        <el-date-picker
          v-model="listQuery.docmkDate"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 230px"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="单据状态">
        <el-select
          v-model="listQuery.stas"
          placeholder="请选择单据状态"
          clearable
        >
          <el-option label="待提交" value="A"> </el-option>
          <el-option label="待审核" value="B"> </el-option>
          <el-option label="已通过" value="Y"> </el-option>
          <el-option label="已驳回" value="R"> </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="getData('search')">查询 </el-button>
        <el-button @click="reset()">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="right-button">
      <el-button
        class="lake-Blue"
        type="primary"
        icon="el-icon-plus"
        @click="handleAdd()"
        >新增计划
      </el-button>
    </div>
    <!--列表-->
    <div>
      <el-table
        v-loading="listLoading"
        element-loading-text="加载中"
        :data="list"
        fit
        stripe
        border
      >
        <template slot="empty">
          <img src="@/assets/nopage.png" alt />
          <p>暂无数据</p>
        </template>
        <el-table-column
          fixed
          type="index"
          label="序号"
          width="60"
        ></el-table-column>
        <el-table-column label="单据编号" prop="id" width="250" />
        <el-table-column label="单据状态" prop="stas">
          <template slot-scope="scope">
            <span v-if="scope.row.stas == 'A'">待提交</span>
            <span v-if="scope.row.stas == 'B' || scope.row.stas == '0'"
              >待审核</span
            >
            <span v-if="scope.row.stas == 'Y'">已通过</span>
            <span v-if="scope.row.stas == 'R'">已驳回</span>
          </template>
        </el-table-column>
        <el-table-column label="计划类型" prop="planType" />
        <el-table-column label="明细条数" prop="detlCnt" />
        <el-table-column label="金额" prop="purcAmt" />
        <el-table-column label="制单人" prop="docmker" />
        <el-table-column label="提交时间" prop="docmkDate" width="180" />
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button
              v-if="scope.row.stas == 'A' || scope.row.stas == 'R'"
              class="cyan"
              type="text"
              @click="handleAdd(scope.row)"
              >编辑</el-button
            >
            <el-button
              v-if="scope.row.stas == 'A' || scope.row.stas == 'R'"
              class="red"
              type="text"
              @click="handleDelete(scope.row)"
              >删除</el-button
            >
            <el-button v-else type="text" @click="openDetail(scope.row)"
              >详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!--分页-->
    <yl-pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.current"
      :limit.sync="listQuery.size"
      @pagination="getData"
    />
  </div>
</template>

<script>
import ylPagination from "@/components/yl-pagination";
import {
  getPlanList,
  delPlan
} from "@/api/purchasing-management/procurement-plan-management";
export default {
  components: {
    ylPagination
  },
  data() {
    return {
      list: [],
      listLoading: false,
      listQuery: {
        id: "",
        planType: "",
        docmkDate: [],
        docmkDateStart: "",
        docmkDateEnd: "",
        stas: "",
        //条件查询
        current: 1,
        size: 10
      },
      total: 0, //分页
      planTypes: [
        {
          dicCode: "补点",
          dicVal: "补点"
        },
        {
          dicCode: "临时",
          dicVal: "临时"
        },
        {
          dicCode: "月度",
          dicVal: "月度"
        },
        {
          dicCode: "季度",
          dicVal: "季度"
        },
        {
          dicCode: "年度",
          dicVal: "年度"
        }
      ]
    };
  },

  created() {
    this.getData();
  },

  methods: {
    //获取列表数据
    getData(type) {
      if (type == "search") {
        this.listQuery.current = 1;
      }
      this.listLoading = true;
      this.listQuery.docmkDateStart = this.listQuery.docmkDate[0];
      this.listQuery.docmkDateEnd = this.listQuery.docmkDate[1];
      getPlanList(this.listQuery)
        .then((response) => {
          this.list = response.data.records;
          this.total = response.data.total;
          this.listLoading = false;
        })
        .catch((err) => {
          this.listLoading = false;
        });
    },

    // 重置
    reset() {
      this.listQuery.id = "";
      this.listQuery.planType = "";
      this.listQuery.docmkDate = [];
      this.listQuery.docmkDateStart = "";
      this.listQuery.docmkDateEnd = "";
      this.listQuery.stas = "";
      this.getData();
    },

    // 跳转新增模板
    handleAdd(row) {
      if (row && row.id) {
        this.$router.push({
          path: "/purchasing-management/procurement-plan-management-add",
          query: {
            id: row && row.id ? row.id : "",
            planType: row.planType ? row.planType : "",
            planPrd: row.planPrd ? row.planPrd : "",
            docmkDate: row.docmkDate ? row.docmkDate : "",
            delvDays: row.delvDays ? row.delvDays : ""
          }
        });
      } else {
        this.$router.push({
          path: "/purchasing-management/procurement-plan-management-add"
        });
      }
    },

    // 删除
    handleDelete(row) {
      this.$confirm("确认删除", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          delPlan({ id: row.id })
            .then((response) => {
              this.$message({
                message: "删除成功",
                type: "success"
              });
              this.getData();
            })
            .catch((err) => {});
        })
        .catch(() => {});
    },

    // 详情
    openDetail(row) {
      this.$router.push({
        path: "/purchasing-management/procurement-plan-management-detail",
        query: {
          id: row.id,
          planType: row.planType,
          planPrd: row.planPrd,
          docmkDate: row.docmkDate,
          delvDays: row.delvDays
        }
      });
    }
  }
};
</script>

<style>
</style>