<template>
  <div>
    <el-card>
      <el-form :model="query" size="mini" label-width="100px" v-show="showSearch">
        <el-row>
          <el-col :span="6">
            <el-form-item label="记录ID:">
              <el-input v-model="filterQuery.genKey" style="width: 90%" placeholder="请输入记录ID"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="处理状态:">
              <el-select v-model="filterQuery.status" style="width: 90%" placeholder="请选择处理状态">
                <el-option label="全部" value=""></el-option>
                <el-option label="正在处理" value="0"></el-option>
                <el-option label="处理完成" value="3"></el-option>
                <el-option label="处理异常" value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="外部系统:">
              <el-select v-model="filterQuery.thirdSysFk" style="width: 90%" placeholder="请选择第三方系统"
                         @change="sysChange">
                <el-option
                  v-for="item in thirdSys"
                  :key="item.value"
                  :label="item.thirdName"
                  :value="item.thirdId">
                  <span style="float: left">{{ item.thirdName }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.thirdId }}</span>
                </el-option>
              </el-select>
            </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-upload
            v-if="this.thirdSysDetail.enabled && this.thirdSysDetail.fromType==1"
            :action="uploadFileUrl"
            multiple
            :limit="1"
            :data="uploadData"
            :show-file-list="false"
            :on-success="handleChange"
            :file-list="fileList"
          >
            <el-button  icon="el-icon-bottom-right" type="primary">导入往来单位</el-button>
          </el-upload>
          <el-button type="primary" icon="el-icon-download" @click="downloadCorps"
                     v-if="this.thirdSysDetail.enabled && this.thirdSysDetail.fromType==3">
            下载往来单位
          </el-button>
        </el-button-group>
      </div>
      <el-divider style="margin: 15px"></el-divider>
      <el-table v-loading="loading" :data="list" style="width: 100%" @selection-change="handleSelectionChange" border highlight-current-row>
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="记录ID" show-overflow-tooltip></el-table-column>
        <el-table-column label="来源" prop="fromType" show-overflow-tooltip></el-table-column>
        <el-table-column label="更新日期" prop="updateTime" show-overflow-tooltip></el-table-column>
        <el-table-column label="来源系统" prop="thirdSysFk" show-overflow-tooltip :formatter="formatterThirdSys"></el-table-column>
        <el-table-column label="备注" prop="remark" show-overflow-tooltip></el-table-column>
        <el-table-column label="状态" prop="status" show-overflow-tooltip>
          <template slot-scope="scope">
            <el-tag :type="statusFilterType(scope.row.status)">{{ status[scope.row.status] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="120">
          <template slot-scope="scope">
            <el-button type="text" @click.native.stop="handleDetailClick(scope.row)">详情</el-button>
            <el-button type="text" @click.native.stop="deleteDialog(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog
        title="导入往来单位详情"
        :visible.sync="udiImportDetailVisible"
        width="80%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        v-if="udiImportDetailVisible"
      >
        <thrCorpsImportDetail
          :currentRow="currentRow"
        ></thrCorpsImportDetail>
      </el-dialog>
      <el-dialog
        title="下载往来单位信息"
        :visible.sync="thrCorpSelectVisible"
        width="80%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        v-if="thrCorpSelectVisible"
      >
        <thrCorpSelect :selectType="selectType" v-on:cancelDialog="cancelDialog" :isDownThrSys="true"
        ></thrCorpSelect>
      </el-dialog>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="filterQuery.page"
        :limit.sync="filterQuery.limit"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import {filterLog, deleteLog} from "@/api/thrsys/thrCorpsImport";
import {getBasicThirdSys, filterDetailByKey} from "@/api/basic/basicThirdSys";
import {downloadAll} from "@/api/thrsys/thrCorps";
import {selectIp} from "@/api/param/systemParamConfig";
import thrCorpsImportDetail from "./thrCorpsImportDetail";
import thrCorpSelect from "./thrCorpSelect";
import axios from "axios";

export default {
  data() {
    return {
      showSearch: true,
      filterQuery: {
        genKey: null,
        status: null,
        fromType: null,
        page: 1,
        limit: 20,
        thirdSysFk: null,
      },
      mainThirdSys:'',
      udiImportDetailVisible: false,
      thrCorpSelectVisible: false,
      checked: false,
      list: [],
      fileList: [],
      detailList: [],
      thirdSys: [],
      thirdSysDetail: null,
      total: 0,
      currentRow: null,
      editQuery: null,
      fromStatus: {
        0: "产品信息",
        1: "库存信息",
        2: "异常第三方上传"
      },
      status: {
        0: "等待处理",
        1: "正在处理",
        2: "处理异常",
        3: "处理完成"
      },
      uploadFileUrl: null,
      uploadData: {
        thirdSys: "thirdId",
      },
      templateDlUrl: null,
    };
  },

  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        fromType: null,
        genKey: null,
        status: null,
        page: 1,
        limit: 20,
        thirdSysFk: this.mainThirdSys,
      };
      this.getList();
    },
    hideSearch() {
      this.showSearch = !this.showSearch;
    },

    getList() {
      this.loading = true;
      filterLog(this.filterQuery)
        .then((response) => {
          this.loading = false;
          if (response.code == 20000) {
            this.list = response.data.list || [];
            this.total = response.data.total || 0;
          } else {
            this.$message.error(response.message);
          }
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    downloadCorps() {
      this.selectType = 3;
      this.thrCorpSelectVisible = true;
    },
    genInCode() {
      this.selectBasicUdiVisible = true;
    },
    sysChange() {
      this.getThirdSysDetail();
    },


    handleDetailClick(row) {
      this.currentRow = row;
      this.udiImportDetailVisible = true;

    }
    ,
    cancelDialog(val) {
      this.udiImportDetailVisible = false;
      this.thrCorpSelectVisible = false;
      if (val) {
        this.getList();
      }
    },
    deleteDialog(rowId) {
      this.$confirm("此操作将删除该往来单位信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let dQuery = {
            id: rowId,
          };
          deleteLog(dQuery)
            .then((response) => {
              this.loading = false;
              if (response.code == 20000) {
                this.$message.success("删除成功");
              } else {
                this.$message.error(response.message);
              }
              this.getList();
            })
            .catch(() => {
              this.loading = false;
            });
        })
        .catch(() => {
        });
    }
    ,
    getBasicThirdSys() {
      let query = {
        enabled: true,
      };
      getBasicThirdSys(query)
        .then((response) => {
          this.thirdSys = response.data.list || [];
          for(var i=0;i<this.thirdSys.length;i++){
            if(this.thirdSys[i].mainSys){
              this.filterQuery.thirdSysFk = this.thirdSys[i].thirdId;
              this.mainThirdSys = this.thirdSys[i].thirdId;
            }
          }
          this.uploadData.thirdSys = this.filterQuery.thirdSysFk;
          this.getList();
          this.getThirdSysDetail();
          this.selectSysParam();
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
        });
      // axios.get("./config.json").then(res => {
      //     // 基础地址
      //     let response = res.data.BASE_URL;
      //     this.uploadFileUrl = response + "/udiwms/thrOrder/importLog/upload";
      //
      // });

    }
    ,
    selectSysParam() {
      let query = {
        key: "thirdIpUrl",
        thirdSysFk: this.filterQuery.thirdSysFk
      };
      selectIp(query).then((response) => {
        if (response.code == 20000) {
          this.uploadFileUrl = response.data.thridUrl + "/udiwms/erp/corp/upload";
        }
      });
    }
    ,
    handleChange(response, files, fileList) {
      if (response.code != 20000) {
        this.$message.error(response.message);
        this.getList();
      } else {
        this.$message.success(response.data);
        this.getList();
      }
      this.fileList = [];
    }
    ,
    statusFilterType(status) {
      const statusMap = {
        0: "warning",
        1: "warning",
        2: "danger",
        3: "success",
      };
      return statusMap[status];
    }
    ,
    getThirdSysDetail() {
      let query = {
        thirdSysFk: this.filterQuery.thirdSysFk,
        key: "corpUrl",
      };
      filterDetailByKey(query)
        .then((response) => {
          this.thirdSysDetail = response.data;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
        });
      this.uploadData.thirdSys = this.filterQuery.thirdSysFk;
      axios.get("./config.json").then(res => {
        let response = res.data.SERVER_IP;
        this.templateDlUrl = response + "/第三方往来单位信息导入模板.xlsx";
      });

    }
    ,
    downloadDatas() {
      this.$confirm("此操作从第三方系统下载全部往来单位信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      }).then(() => {
        let query = {
          thirdSysFk: this.filterQuery.thirdSysFk
        };
        downloadAll(query).then((response) => {

          if (response.code == 20000) {
            this.$message.success(response.data);
          } else {
            this.$message.error(response.message);
          }
          this.getList();
        });
      });
    }
    ,
    formatterThirdSys(key) {
      let data = null;
      this.thirdSys.forEach((item, index) => {
        if (key.thirdSysFk == item.thirdId) {
          data = item.thirdName;
        }
      });
      return data;
    }
    ,
  },

  mounted() {
  },
  components: {thrCorpsImportDetail, thrCorpSelect},
  created() {
    this.getBasicThirdSys();
    // this.getList();
  },
};
</script>
<style scoped>
.itemTag {
  float: left;
  text-align: left;
  margin-top: 10px;
  width: 100px;
}


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

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

}

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