<template>
  <div>
    <el-card class="el-card">
      <el-form :model="filterQuery" class="query-form" label-width="100px" v-show="showSearch">
        <el-row>
          <el-col :span="6">
            <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="6">
            <el-form-item label="单据日期:">
              <el-date-picker
                :picker-options="pickerOptions"
                v-model="actDateRange"
                type="daterange"
                style="width: 90%"
                format="yyyy 年 MM 月 dd 日"
                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-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>

      <el-table v-loading="loading" :data="list" style="width: 100%" border highlight-current-row
                @current-change="handleDetail">
        <el-table-column label="序号" type="index" width="60"></el-table-column>
        <el-table-column label="单据号" prop="billNo"></el-table-column>
        <el-table-column label="单据日期" prop="billDate">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.billDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="供应商" prop="createUser"></el-table-column>
        <el-table-column label="部门" prop="deptName"></el-table-column>
        <el-table-column label="仓库" prop="invName"></el-table-column>
        <el-table-column label="备注" prop="remark"></el-table-column>
        <el-table-column label="状态" prop="status">
          <template slot-scope="scope">
            <el-tag :type="(scope.row.status) | statusFilterType">
              {{ statusMap[scope.row.status] }}
            </el-tag>
          </template>
        </el-table-column>


        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click.native="newDistributionForm(scope.$index, scope.row)">编辑
            </el-button>
            <el-button type="text" size="small" @click.native.stop="deleteDialog(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>
        <el-table-column label="序号" type="index" width="120"></el-table-column>
        <!-- <el-table-column label="产品编码" prop="productId"></el-table-column> -->
        <el-table-column label="产品通用名" prop="productName" show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="规格型号" prop="spec" show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="送货数量" prop="count"></el-table-column>
        <el-table-column label="到货数量" prop="count"></el-table-column>
        <el-table-column label="注册/备案凭证号" prop="zczbhhzbapzbh" show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="生产企业" prop="manufactory" show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="医疗器械注册人" prop="ylqxzcrbarmc" show-overflow-tooltip="true"></el-table-column>
      </el-table>
    </el-card>

    <el-dialog
      :title="formMap[formName]"
      :visible.sync="newSpDistributionVisible"
      width="80%"
      v-if="newSpDistributionVisible"
      @close='closeDialog'
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :before-close="handleClose"
    >
      <purDeliveryEdit
        :isChang="isChang"
        :closeDialog="closeDialog"
        v-on:cancelDialog="getId"
        :idQuery="idQuery"
      ></purDeliveryEdit>
    </el-dialog>
  </div>
</template>
<script>
import purDeliveryEdit from "./purArrivalEditDialog";
import {inserThrOrderWeb, listOrderDetail, delOrderDetailAll, listOrder} from "@/api/purchase/purArrival";

export default {
  data() {
    name: "purDelivery"
    return {
      showSearch: true,
      pId: null,
      isChang: false,
      filterQuery: {
        billAction: null,
        billNo: "",
        originType: null,
        thirdSysFk: "",
        page: 1,
        limit: 10,
        corpName: null,
        type: 1,
        editStatus: 1,
      },
      formName: null,
      formMap: {
        add: "新增送货单",
        update: "编辑送货单",
      },
      statusMap: {
        1: "草稿",
        2: "未审核",
        3: "已审核",
        4: "已拒绝"
      },
      idQuery: {},
      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]);
            },
          },
        ],
      },
    };
  },

  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.actDateRange = [];
      this.getList();
    },
    handleClose() {
      if (this.formName == 'add' && this.pId != null) {
        this.isChang = !this.isChang
      } else {
        this.newSpDistributionVisible = false
      }
    },
    getId(id) {
      if (this.formName == 'add' && id != null) {
        var data = {
          id: id
        }
        this.pId = id;
        //当在新增时取消添加操作删除掉这次的数据
        delOrderDetailAll(data).then((response) => {
          if (response.code == 20000) {
            this.newSpDistributionVisible = false
          }
        });
      }

    },
    onSubmitFind() {
      this.filterQuery.page = 1;
      this.getList();
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    getList() {
      if (this.actDateRange != null) {
        this.filterQuery.startDate = this.actDateRange[0];
        this.filterQuery.endDate = this.actDateRange[1];
      } else {
        this.filterQuery.startDate = null;
        this.filterQuery.endDate = null;
      }
      this.loading = true;
      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) {
      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;
        });
    },
    submitOrder(row) {
      this.$confirm("是否确定提交审核?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.loading = true;
          let tQuery = {
            editStatus: 2,
            purPlanEntity: row,
          }
          inserThrOrderWeb(tQuery)
            .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(() => {
        });
    },
    deleteDialog(row) {
      this.$confirm("此操作将永久删除该单据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delOrderDetailAll({id: row.id})
            .then((response) => {
              this.loading = false;
              if (response.code == 20000) {
                this.$message.success("删除成功");
                this.getList();
                this.detailList = null;
              } else {
                this.$message.error(response.message);
              }
            })
            .catch(() => {
              this.loading = false;
            });

        })
        .catch(() => {
        });
    },
    handleCurrentChange(val) {
      this.filterQuery.page = val.page;
      this.getList();
    },

    closeDialog() {
      this.newSpDistributionVisible = false;
      this.getList();
      this.detailList = [];
    },


    newDistributionForm(index, 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: {
    purDeliveryEdit,
  }
  ,
  filters: {
    statusFilterType(status) {
      const statusMap = {
        1: "warning",
        2: "success",
      };
      return statusMap[status];
    }
    ,
  }
  ,
  mounted() {
  }
  ,
  created() {
    let end = new Date();
    let start = new Date();
    start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
    this.actDateRange = [start, end];
    this.getList();
  }
  ,
}
;
</script>
<style scoped>
.itemTag {
  float: left;
  text-align: left;
  margin-top: 10px;
  width: 25%;
}

.text {
  font-size: 13px;
  font-family: "Microsoft YaHei";
}

.query-form-item {
  display: block !important;
  margin-right: 10px;
  margin-bottom: 5px;
}

.el-row {
  display: flex;
  flex-wrap: wrap;
}

.el-col {
  border-radius: 4px;
  flex-wrap: wrap;
}
</style>

