<template>
  <div>
    <el-card>
      <el-form :inline="true" :model="filterQuery" class="query-form" size="mini">
        <el-form-item class="query-form-item" label="单据类型:">
          <el-input
            v-model="filterQuery.key"
            placeholder="请输入单据类型"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item class="query-form-item" label="第三方系统:">
          <el-select v-model="filterQuery.thirdSys" placeholder="请选择第三方系统" @change="thirdSysChange">
            <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-form-item>
          <el-button-group style="margin-left: 10px;display:flex;">
            <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
            <el-button type="primary" icon="el-icon-search" @click="search">查询</el-button>
            <el-button type="primary" icon="el-icon-plus" @click="handleAddClick"
                       :disabled="!configParms.typeThird"
            >新增
            </el-button
            >
            <el-button type="primary" icon="el-icon-download" @click="updateDownload"
                       v-if="this.thirdSysDetail.enabled && this.thirdSysDetail.fromType==3"
                       :disabled="!configParms.typeThird"
            >更新下载
            </el-button>
          </el-button-group>

        </el-form-item>
      </el-form>
      <el-table v-loading="loading" :data="list" style="width: 100%" border>
        <el-table-column label="序号" type="index" width="55"></el-table-column>
        <el-table-column label="单据类型名称" prop="name"></el-table-column>
        <el-table-column label="单据类型代码" prop="action"></el-table-column>
        <el-table-column prop="thirdName" label="第三方系统名称">
          <template slot-scope="scope">
            <span>{{ getThirdSysFkName(scope.row.thirdSys) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="备注" prop="remark"></el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              :disabled="!configParms.typeThird"
              @click.native.stop="handleModifyClick(scope.row)"
            >编辑
            </el-button
            >
            <el-button
              type="text"
              size="small"
              :disabled="!configParms.typeThird"
              @click.native.stop="handleDeleteClick(scope.row)"
            >删除
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-dialog
        title="单据类型-新增"
        :visible.sync="addDialogVisible"
        width="45%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        v-if="addDialogVisible"
      >
        <modifyDialog :inputQuery="inputQuery"></modifyDialog>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" size="small" icon="search" @click="onAddSubmit"
          >提交
          </el-button
          >
          <el-button size="small" icon="search" @click="cancelDialog"
          >取消
          </el-button
          >
        </div>
      </el-dialog>
      <el-dialog
        title="单据类型-编辑"
        :visible.sync="modifyDialogVisible"
        width="45%"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        v-if="modifyDialogVisible"
      >
        <modifyDialog :inputQuery="inputQuery"></modifyDialog>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" size="small" icon="search" @click="onModifySubmit"
          >提交
          </el-button
          >
          <el-button size="small" icon="search" @click="cancelDialog"
          >取消
          </el-button
          >
        </div>
      </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 {
  getOriginBusType,
  updateBussinessType, delBussinessType,
  insertBussinessType, downloadBussinessType
} from "@/api/thrsys/busOriginType";

import modifyDialog from "./busTypeModify";
import axios from "axios";
import {filterDetailByKey, getBasicThirdSys} from "@/api/thrsys/basicThirdSys";
import {findConfig} from "@/api/thrsys/spsSyncStatus";
import {isBlank} from "@/utils/strUtil";

export default {
  data() {
    return {
      filterQuery: {
        thirdSys: null,
        key: "",
        page: 1,
        limit: 20,
      },
      mainThirdSys:'',
      addDialogVisible: false,
      modifyDialogVisible: false,
      list: [],
      inputQuery: {
        action: "",
        name: "",
        enable: "",
        remark: "",
        thirdSys: null,
        thirdSysName: null,

      },
      enableMap: {
        true: "是",
        false: "否",
      },
      thirdSys: [],
      total: 0,
      multipleSelection: [],
      uploadFileUrl: null,
      loading: false,
      thirdSysDetail: {},
      configParms: {},
    };
  },

  methods: {
    onReset() {
      this.$router.push({
        path: "",
      });
      this.filterQuery = {
        thirdSys: this.mainThirdSys,
        name: null,
        enable: null,
        page: 1,
        limit: 20,
      };
      this.getList();
    },
    cancelDialog() {
      this.modifyDialogVisible = false;
      this.addDialogVisible = false;
    },
    search() {
      this.filterQuery.page = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getOriginBusType(this.filterQuery)
        .then((response) => {
          this.loading = false;
          this.list = response.data.list || [];
          this.total = response.data.total || 0;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
          this.total = 0;
        });
    },
    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.thirdSys = this.thirdSys[i].thirdId;
              this.mainThirdSys = this.thirdSys[i].thirdId;
            }
          }
          this.getThirdSysDetail();
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
        });
    },
    updateDownload() {
      let query = {
        thirdSys: this.filterQuery.thirdSys,
      }
      downloadBussinessType(query)
        .then((response) => {
          this.loading = false;
          if (response.code == 20000) {
            this.getList();
          } else {
            this.$message.error(response.message);
          }


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

    onAddSubmit() {

      if (isBlank(this.inputQuery.action)) {
        this.$message.error("单据类型代码不能为空！");
        return;
      }

      if (isBlank(this.inputQuery.name)) {
        this.$message.error("单据类型名称不能为空！");
        return;
      }

      if (isBlank(this.inputQuery.thirdSys)) {
        this.$message.error("第三方系统不能为空");
        return;
      }

      let numRegExp = '^[0-9]*$';
      let numReg = new RegExp(numRegExp);
      if (numReg.test(this.inputQuery.name)) {
        this.$message.error("单据类型名称不得为纯数字类型！");
        return;
      }

      insertBussinessType(this.inputQuery)
        .then((response) => {
          if (response.code === 20000) {
            this.loading = false;
            this.cancelDialog();
            this.getList();
          } else {
            this.$message.error(response.message);
          }
        })
        .catch(() => {
          this.loading = false;
          this.cancelDialog();
        });
    },

    onModifySubmit() {
      if (this.$isBlank(this.inputQuery.name)) {
        this.$message.error("单据类型名称不能为空！");
        return;
      }
      if (this.$isBlank(this.inputQuery.action)) {
        this.$message.error("单据类型代码不能为空！");
        return;
      }
      if (this.$isBlank(this.inputQuery.thirdSys)) {
        this.$message.error("所属系统不能为空！");
        return;
      }
      let numRegExp = '^[0-9]*$';
      let numReg = new RegExp(numRegExp);
      if (numReg.test(this.inputQuery.name)) {
        this.$message.error("单据类型名称不得为纯数字类型！");
        return;
      }

      updateBussinessType(this.inputQuery)
        .then((response) => {
          if (response.code === 20000) {
            this.loading = false;
            this.cancelDialog();
            this.getList();
          } else {
            this.$message.error(response.message);
          }
        })
        .catch(() => {
          this.loading = false;
          this.cancelDialog();
        });
    },

    handleModifyClick(row) {

      this.inputQuery = {
        id: row.id,
        remark: row.remark,
        action: row.action,
        name: row.name,
        enable: row.enable,
        thirdSys: row.thirdSys,
        thirdSysName: row.thirdSysName
      };
      this.modifyDialogVisible = true;
    },


    handleDeleteClick(row) {
      this.deleteDialog(row.id)
    },

    handleAddClick() {
      this.inputQuery = {
        action: "",
        name: "",
        enable: "",
        remark: "",
        thirdSys: null,
        thirdSysName: null
      };
      this.addDialogVisible = true;
    },

    deleteDialog(rowId) {
      this.$confirm("此操作将永久删除该单据类型信息, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          let query = {
            id: rowId,
          }
          delBussinessType(query)
            .then((response) => {
              this.getList();
            })
            .catch(() => {
            });

        })
        .catch(() => {
        });
    },

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

    getThirdSysDetail() {
      let query = {
        thirdSysFk: this.filterQuery.thirdSys,
        key: "busTypeQueryUrl",
      };
      filterDetailByKey(query)
        .then((response) => {
          this.thirdSysDetail = response.data;
        })
        .catch(() => {
          this.loading = false;
          this.list = [];
        });
      this.uploadData.thirdSys = this.query.thirdSysFk;
    },

    getThirdSysFkName(thirdSys) {
      for (let i = 0; i < this.thirdSys.length; i++) {
        if (this.thirdSys[i].thirdId === thirdSys) {
          return this.thirdSys[i].thirdName;
        }
      }
    },

    init() {
      axios.get("./config.json").then(res => {
        // 基础地址
        let response = res.data.BASE_URL;
        this.uploadFileUrl = response + "udiwms/busstiness/file/upload";
      });
    },
    handleChange(response, files, fileList) {
      if (response.code != 20000) {
        this.$message.error(response.message);
      } else {
        this.$message.success(response.data);
        this.getList();
      }
    },
    getSyncConfig() {
      findConfig()
        .then((response) => {
          if (response.code == 20000) {
            this.configParms = response.data;
          }
        })
        .catch(() => {
        });
    },
  },
  components: {
    modifyDialog,
  },
  mounted() {
  },
  created() {
    this.init();
    this.getSyncConfig();
    this.getBasicThirdSys();
    this.getList();

  },
};
</script>
