<template>
  <div>
    <el-card>
      <el-form :model="filterQuery" size="mini" 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-select v-model="filterQuery.thirdSysFk" style="width: 90%" placeholder="请选择第三方系统"
                         clearable="true" @change="thirdSysChange">
                <el-option
                  v-for="item in thirdSys"
                  :key="item.thirdId"
                  :label="item.thirdName"
                  :value="item.thirdId">
                  <span style="float: left;font-size: 13px">{{ item.thirdName }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="单据类型:">
              <el-select v-model="filterQuery.billAction" style="width: 90%" placeholder="请选择第三方系统单据类型"
                         clearable="true">
                <el-option
                  v-for="item in originTypes"
                  :key="item.name"
                  :label="item.name"
                  :value="item.action">
                  <span style="float: left;font-size: 13px">{{ item.name }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px;margin-left: 10px">{{
                      item.thirdSysName
                    }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>


        </el-row>
        <el-row>
          <el-col :span="6">
            <el-form-item label="单据日期:">
              <el-date-picker
                :picker-options="pickerOptions"
                v-model="actDateRange"
                type="daterange"
                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="getList">查询</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="billType"></el-table-column>
        <el-table-column label="单据号" prop="billNo"></el-table-column>
        <el-table-column label="往来单位ID" prop="corpId"></el-table-column>
        <el-table-column label="往来单位" prop="corpName"
                         show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="单据状态" prop="billFlag"></el-table-column>
        <el-table-column label="单据日期" prop="billdate"></el-table-column>
        <el-table-column label="操作" width="60">
          <template slot-scope="scope">
            <el-button type="text" @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="getList"
      />
    </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="batchNo"></el-table-column>
        <el-table-column label="生产日期" prop="productDate"></el-table-column>
        <el-table-column label="失效日期" prop="expireDate"></el-table-column>
        <!--        <el-table-column label="单据数量" prop="reCount"></el-table-column>-->
        <el-table-column label="单据数量" prop="count"></el-table-column>
      </el-table>
    </el-card>
  </div>
</template>
<script>
import {delThrOrders, delAll, downloadAll, getThrOrders, getThrOrderDetails} from "@/api/thrsys/thrOrder";
import {getBussinessType} from "@/api/basic/bussinessType";
import {getOriginBusType} from "@/api/basic/busOriginType";
import {getBasicThirdSys, filterDetailByKey} from "@/api/basic/basicThirdSys";
import {selectIp} from "@/api/param/systemParamConfig";

export default {
  data() {
    return {
      showSearch: true,
      filterQuery: {
        billAction: null,
        billNo: "",
        originType: null,
        thirdSysFk: "",
        page: 1,
        limit: 10,
        corpName: null,
      },
      total: 0,
      thirdSys: [],
      thirdSysDetail: null,
      busTypes: [],
      originTypes: [],
      list: [],
      detailList: [],
      uploadFileUrl: null,
      fileList: [],
      uploadData: {
        thirdSys: "thirdId",
      },
      loading: false,
      actDateRange: [],
      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,
      };
      this.actDateRange = [];
      this.getList();
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },
    getList() {
      if (this.filterQuery.thirdSysFk == null) {
        this.$message.warning("请先选择第三方系统！")
        return;
      }
      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;
      getThrOrders(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.billNo};
      getThrOrderDetails(query)
        .then((response) => {
          if (response.code == 20000) {
            this.detailList = response.data.list || [];
          } else {
            this.$message.error(response.message);
          }
          this.loading = false;
        })
        .catch(() => {
          this.loading = false;
        });

    },
    deleteDialog(row) {
      this.$confirm("此操作将永久删除该单据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let dQuery = {
            id: row.id,
          };
          delThrOrders(dQuery)
            .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(() => {
        });
    },
    clearAll() {
      this.$confirm("此操作将清空所有单据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          delAll()
            .then((response) => {
              this.loading = false;
              if (response.code == 20000) {
                this.$message.success("删除成功");
                this.getList();
              } else {
                this.$message.success(response.message);
              }
            })
            .catch(() => {
              this.loading = false;
            });

        })
        .catch(() => {
        });
    },
    getBasicThirdSys() {
      let query = {
        enabled: true,
      };
      getBasicThirdSys(query)
        .then((response) => {
          this.thirdSys = response.data.list || [];
          this.filterQuery.thirdSysFk = this.thirdSys[0].thirdId;
          this.uploadData.thirdSys = this.filterQuery.thirdSysFk;
          this.selectSysParam();
          this.getThirdSysDetail();
          this.getBusType();
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
        });
    },
    handleChange(response, files, fileList) {
      console.log(response);
      if (response.code != 20000) {
        this.$message.error(response.message);
      } else {
        console.log(files[0] + "\n" + this.fileList[0] + "\n" + fileList[0]);
        this.$message.success("文件上传成功,请稍后刷新查看！");
      }
    },
    selectSysParam() {
      let query = {
        key: "thirdIpUrl",
        thirdSysFk: this.filterQuery.thirdSysFk
      };
      selectIp(query).then((response) => {
        if (response.code == 20000) {
          this.uploadFileUrl = response.data.thridUrl + "/udiwms/erp/pi/upload";
        }
      });
    },
    thirdSysChange() {
      this.uploadData.thirdSys = this.filterQuery.thirdSysFk;
      this.getThirdSysDetail();
      this.filterQuery.billAction = "";
      this.getBusType();
    },
    getThirdSysDetail() {
      let query = {
        thirdSysFk: this.filterQuery.thirdSysFk,
        key: "orderQueryUrl",
      };
      filterDetailByKey(query)
        .then((response) => {
          this.thirdSysDetail = response.data;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
        });

    },
    downloadDatas() {
      let query = {
        billAction: this.filterQuery.billAction
      };
      downloadAll(query).then((response) => {
        if (response.code == 20000) {
          this.$message.success(response.data);
        } else {
          this.$message.error(response.message);
        }
      });

    },
    getActionName(action) {
      for (let i = 0; i < this.busTypes.length; i++) {
        if (this.busTypes[i].action === action) {
          return this.busTypes[i].name;
        }
      }
    },

    getlocalName(action) {
      for (let i = 0; i < this.busTypes.length; i++) {
        if (this.busTypes[i].action === action) {
          return this.busTypes[i].localName;
        }
      }
    },
    getBusType() {
      let query = {
        enabled: true,
        thirdSys: this.filterQuery.thirdSysFk,
      };
      getBussinessType(query)
        .then((response) => {
          this.busTypes = response.data.list || [];
          // this.getList();
        })
        .catch(() => {
        });
      getOriginBusType(query)
        .then((response) => {
          this.originTypes = response.data.list || [];
          // this.filterQuery.originType = this.originTypes[0].action;
          // this.getList();
        })
        .catch(() => {
        });


    },

  },
  components: {},
  mounted() {
  },
  created() {
    this.getBasicThirdSys();
    let end = new Date();
    let start = new Date();
    start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
    this.actDateRange = [start, end];
  },
};
</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>

