<template>
  <div>
    <el-card>
      <el-form :model="filterQuery" class="query-form" label-width="100px" size="mini" v-show="showSearch">
        <el-row>
          <el-col :span="8">
            <el-form-item label="采购订单号">
              <el-input v-model="filterQuery.billNo" style="width: 90%" placeholder="请输入单据号" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="采购部门">
              <el-select v-model="filterQuery.deptCode" placeholder="请选择采购部门" @change="change" style="width: 90%">
                <el-option
                  v-for="item in deptList"
                  :key="item.code"
                  :label="item.name"
                  :value="item.code"
                >
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="收货仓库">
              <el-select v-model="filterQuery.invCode" style="width: 90%" placeholder="请选择收货仓库">
                <el-option
                  v-for="item in invList"
                  :key="item.name"
                  :label="item.name"
                  :value="item.code">
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>

            </el-form-item>
          </el-col>
        </el-row>
        <el-row>

          <el-col :span="8">
            <el-form-item class="query-form-item" label="处理状态:">
              <el-select v-model="filterQuery.dealStatus"
                         placeholder="请选择处理状态"
                         clearable
                         style="width: 90%"
              >
                <el-option label="未处理" :value="0"></el-option>
                <el-option label="已处理" :value="1"></el-option>
                <el-option label="已作废" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="8" v-if="showSup">
            <el-form-item class="query-form-item" label="供应商:">
              <el-select
                v-model="filterQuery.supId"
                filterable
                remote
                clearable="true"
                reserve-keyword
                placeholder="供应商"
                :remote-method="findMethod"
                size="mini"
                :loading="corpLoading"
                style="width: 90%"
              >
                <el-option
                  v-for="item in fromOptions"
                  :key="item.name"
                  :label="item.name"
                  :value="item.erpId"
                >
                  <span style="float: left">{{ item.name }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="发布时间:">
              <el-date-picker
                :picker-options="pickerUpOptions"
                v-model="auditTimes"
                type="daterange"
                format="yyyy 年 MM 月 dd 日"
                style="width: 90%"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div class="top-right-btn">
        <el-button-group style="display:flex;">
          <el-button icon="el-icon-view" type="primary" @click="hideSearch">显示/隐藏搜索栏</el-button>
          <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="onSubmitFind">查询</el-button>
          <el-button type="primary" icon="el-icon-check" @click="combine"
          >确定
          </el-button
          >
          <el-button type="primary" icon="el-icon-share" v-if="this.Menustatus === 1" @click="help">帮助</el-button>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>

      <el-table v-loading="loading" :data="list" style="width: 100%" border highlight-current-row="true"
                @current-change="handleDetail">
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="采购订单号" prop="billNo" width="150" show-overflow-tooltip></el-table-column>
        <el-table-column label="发布时间" prop="auditTime" width="150" show-overflow-tooltip></el-table-column>
        <el-table-column label="供应商" prop="supName" show-overflow-tooltip="true" width="180"
                         v-if="showSup"></el-table-column>
        <el-table-column label="处理状态" prop="emergency" width="120">
          <template slot-scope="scope">
            {{ dealStatus[scope.row.emergency] }}
          </template>
        </el-table-column>
        <!--        <el-table-column label="创建时间" prop="createTime" width="180" show-overflow-tooltip></el-table-column>-->
        <el-table-column label="紧急程度" prop="emergency" width="120">
          <template slot-scope="scope">
            {{ emergencyMap[scope.row.emergency] }}
          </template>
        </el-table-column>
        <el-table-column label="到货时间" prop="arrivalTime" width="150" show-overflow-tooltip></el-table-column>
        <el-table-column label="订单说明" prop="remark" width="180" show-overflow-tooltip></el-table-column>
        <el-table-column label="采购部门" prop="deptName" width="150" show-overflow-tooltip></el-table-column>
        <el-table-column label="收货仓库" prop="invName" width="150" show-overflow-tooltip></el-table-column>
        <el-table-column label="操作" fixed="right" width="100">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.native.stop="newDistributionForm(scope.row)"
            >处理
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="filterQuery.page"
        :limit.sync="filterQuery.limit"
        @pagination="handleCurrentChange"
      ></pagination>

    </el-card>
    <el-card class="el-card">
      <el-table v-loading="loading" :data="detailList" style="width: 100%" border highlight-current-row="true">
        <el-table-column label="序号" type="index" width="50"></el-table-column>
        <el-table-column label="物资名称" prop="productName" show-overflow-tooltip width="150"></el-table-column>
        <el-table-column label="规格型号" prop="spec" show-overflow-tooltip width="160"></el-table-column>
        <el-table-column label="计量单位" prop="measname" show-overflow-tooltip width="100"></el-table-column>
        <el-table-column label="计划数量" prop="planCount" width="100"></el-table-column>
        <el-table-column label="订单数量" prop="count" width="100"></el-table-column>
        <el-table-column label="计量单位" prop="spec" show-overflow-tooltip="true" width="100"></el-table-column>

        <el-table-column label=" 生产企业
        " prop="manufactory" show-overflow-tooltip="true" width="160"></el-table-column>
        <el-table-column label="注册/备案号" prop="zczbhhzbapzbh" show-overflow-tooltip="true" width="160"></el-table-column>
        <!--        <el-table-column label="注册/备案人" prop="ylqxzcrbarmc" show-overflow-tooltip="true"></el-table-column>-->
      </el-table>
    </el-card>

  </div>
</template>
<script>
import {delApply, listOrder, listOrderDetail, delOrderDetailAll} from "@/api/purchase/purOrder";
import {selectAuthMenu} from "@/api/system/sysMenuHelp";
import {getBasicUnitMaintains} from "@/api/basic/basicUnitMaintain";
import {addPurOrder} from "@/api/inout/erpOrder";
import {getInvListByUser} from "@/api/system/invWarehouse";
import {getDeptListByUser} from "@/api/auth/authDept";


export default {

  name: "closeDialog",
  props: {
    data: {
      type: Object,
      required: true,
    },
    closeDialog: {
      type: Function,
      required: true,
    },
  },

  data() {
    return {
      BASE_URL: process.env.VUE_APP_BASE_API,
      filterQuery: {
        billAction: null,
        billNo: "",
        originType: null,
        thirdSysFk: "",
        page: 1,
        limit: 10,
        corpName: null,
        type: 1,
        editStatus: 1,
      },
      currentRow: null,
      corpLoading: false,
      fromOptions: [],

      Menustatus: null,
      filePath: "",
      createTimes: null,
      auditTimes: null,
      showSearch: true,
      formName: null,
      formMap: {
        add: "新增采购订单",
        update: "采购订单处理",
      },
      statusMap: {
        1: "草稿",
        2: "未审核",
        3: "已审核",
        4: "已拒绝"
      },
      emergencyMap: {
        1: "普通",
        2: "紧急",
        3: "不紧急",
      },

      dealStatus: {
        1: "已处理",
        2: "未处理",
        3: "作废",
      },
      showSup: false,
      invList: {},
      deptList: {},
      idQuery: {},
      editType: 1,
      total: 0,
      thirdSys: [],
      thirdSysDetail: null,
      busTypes: [],
      originTypes: [],
      list: [],
      detailList: [],
      loading: false,
      actDateRange: [],
      newSpDistributionVisible: false,
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
      pickerUpOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            },
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            },
          },
        ],
      },
    };
  },

  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        billNo: "",
        thirdSysFk: "",
        billFlag: null,
        billAction: null,
        startDate: null,
        endDate: null,
        page: 1,
        limit: 10,
        corpName: null,
        type: 1,
        editStatus: 1,
      };
      this.pickerOptions = [];
      this.pickerUpOptions = [];
      this.getList();
    },
    change(val){
      let param={
        pcode:val
      }
      getInvListByUser(param).then((res) => {
        this.invList = res.data || [];
        this.getBusType();
      })

    },
    onSubmitFind() {
      this.filterQuery.page = 1;
      this.getList();
    },

    combine() {
      if (this.$isBlank(this.currentRow.id)) {
        return;
      }
      let tQuery = {
        id: this.currentRow.id,
      }
      addPurOrder(tQuery)
        .then((response) => {
          if (response.code === 20000) {
            this.closeDialog();
          } else {
            this.$message.error(response.message);
          }
        })
        .catch(() => {
        });
    },

    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    help() {
      this.certFileUrl = this.BASE_URL + "/udiwms/image/register/file/getImage?type=image2&name=";
      window.open(this.certFileUrl + this.filePath);
    },
    getList() {
      if (this.createTimes != null) {
        this.filterQuery.startDate = this.createTimes[0];
        this.filterQuery.endDate = this.createTimes[1];
      } else {
        this.filterQuery.startDate = null;
        this.filterQuery.endDate = null;
      }
      if (this.auditTimes != null) {
        this.filterQuery.startAuditDate = this.auditTimes[0];
        this.filterQuery.endAuditDate = this.auditTimes[1];
      } else {
        this.filterQuery.startAuditDate = null;
        this.filterQuery.endAuditDate = null;
      }
      this.loading = true;
      this.filterQuery.status = 3;
      listOrder(this.filterQuery)
        .then((response) => {
          if (response.code == 20000) {
            this.list = response.data.list || [];
            this.total = response.data.total || 0;
          } else {
            this.$message.error(response.message);
          }
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    handleDetail(row) {
      this.currentRow = row;
      let query = {orderIdFk: row.id};
      this.loading = true;
      listOrderDetail(query) //查找该单号下的所有条码
        .then((response) => {
          this.detailList = response.data.list || [];
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
          this.detailList = [];
          this.total = 0;
        });
    },
    deleteDialog(row) {
      this.$confirm("此操作将永久删除该单据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delOrderDetailAll(row)
            .then((response) => {
              this.loading = false;
              if (response.code == 20000) {
                this.$message.success("删除成功");
                this.getList();
              } else {
                this.$message.error(response.message);
              }
            })
            .catch(() => {
              this.loading = false;
            });

        })
        .catch(() => {
        });
    },
    handleCurrentChange(val) {
      this.filterQuery.page = val.page;
      this.getList();
    },
    selectAuthMenu() {
      let params = {menuName: this.$route.meta.title}
      selectAuthMenu(params).then((res) => {
        if (res.code === 20000) {
          this.Menustatus = res.data.status;
          this.filePath = res.data.filePath;
        }
      })
    },

    findMethod(key) {
      this.corpLoading = true;
      this.fromOptions = [];
      let params = {
        key: key,
        corpType: 2,
        page: 1,
        limit: 20
      };
      getBasicUnitMaintains(params).then((res) => {
        this.corpLoading = false;
        this.fromOptions = res.data.list || [];
      }).catch(() => {
        this.corpLoading = false;
      })
    },


    getInvList() {
      getInvListByUser().then((res) => {
        this.invList = res.data || [];
        this.getBusType();
      })
    },
    getDeptList() {
      getDeptListByUser().then((res) => {
        this.deptList = res.data || [];
      });
    },
    newDistributionForm(row) {
      this.idQuery.id = '';
      if (this.$isNotBlank(row) && this.$isNotBlank(row.id)) {
        this.idQuery.id = row.id;
        this.idQuery.formData = row;
        this.formName = "update";
      } else
        this.formName = "add";
      this.newSpDistributionVisible = true;
    },

  }
  ,
  components: {}
  ,
  filters: {
    statusFilterType(status) {
      const statusMap = {
        2: "warning",
        3: "success",
        4: "danger",
      };
      return statusMap[status];
    }
    ,
  }
  ,
  mounted() {
  }
  ,
  created() {

    let supId = this.$store.getters.customerId;
    if (supId == "110") {
      this.showSup = true;
    }


    this.selectAuthMenu();

    let end = new Date();
    let start = new Date();
    start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
    this.actDateRange = [start, end];
    this.getList();
    this.getInvList();
    this.getDeptList();
  }
  ,
}
;
</script>
<style scoped>

</style>

