<template>
  <div>
    <el-form :model="editLogin" label-width="120px" ref="editLogin" :rules="platformRules">
      <el-card>
        <el-row :gutter="20" class="el-row" type="flex">
          <el-col :span="10" class="el-col" type="flex">
            <div class="text item">
              <el-form-item label="客户名称" prop="unitId">
                <el-select
                    v-model="editLogin.unitId"
                    style="width: 100%"
                    filterable
                    :disabled="editDialogVisible"
                    placeholder="请选择客户"
                >
                  <el-option
                      v-for="item in corpNamesList"
                      :key="item.erpId"
                      :label="item.name"
                      :value="item.erpId"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="10" class="el-col" type="flex">
            <div class="edit-text">
              <el-form-item label="自助平台名称" prop="platformId">
                <el-input v-model="editLogin.name" :disabled="true" style="width: 100%" size="small"></el-input>
              </el-form-item>
            </div>
          </el-col>
        </el-row>


        <el-row :gutter="20" class="el-row" type="flex">
          <el-col :span="10" class="el-col" type="flex">
            <div class="text item">
              <el-form-item label="账号" prop="platformUsername">
                <el-input
                    v-model="editLogin.platformUsername"
                    placeholder="请输入账号"
                    style="width: 100%"
                    size="small"
                ></el-input>
              </el-form-item>
            </div>
          </el-col>
          <el-col :span="10" class="el-col" type="flex">
            <div class="text item">
              <el-form-item label="密码" prop="platformPassword">
                <el-input
                    v-model="editLogin.platformPassword"
                    placeholder="请输入密码"
                    style="width: 70%"
                    size="small"
                    type="password"
                    show-password
                ></el-input>
                <el-button type="primary" style="margin-left: 2%;" @click.native="testUserInfo">
                  账号验证
                </el-button>
              </el-form-item>
            </div>
          </el-col>
        </el-row>
      </el-card>
    </el-form>

    <el-card>
      <el-button size="mini" type="primary" style="float: right; margin-right: 35px;margin-bottom: 12px;" :disabled="!check"  @click.native="addDelect()">添加</el-button>
      <el-table :data="delectList" border highlight-current-row style="width: 100%">
        <el-table-column label="本地单据" prop="sourceName" fixed></el-table-column>
        <el-table-column label="目标部门" prop="invName" fixed></el-table-column>
        <el-table-column label="目标仓库" prop="invSubName" fixed></el-table-column>
        <el-table-column label="目标单据类型" prop="targetName" fixed></el-table-column>
              <el-table-column label="操作" fixed="right">
                <template slot-scope="scope">
                  <el-button type="text" size="small" :disabled="!check" @click.native.stop="addDelect(scope.row)">编辑</el-button>
                  <el-button type="text" size="small" :disabled="!check" @click.native.stop="deleteDialog(scope.row)">删除</el-button>
                </template>
              </el-table-column>
      </el-table>

      <pagination
          v-show="total>0"
          :total="total"
          :limit.sync="query.limit"
          :page.sync="query.page"
          @pagination="handleCurrentChange"
      ></pagination>
    </el-card>

    <el-dialog
        :title="formName[formQuery]"
        :visible.sync="editLoginVisible"
         v-if="editLoginVisible"
        append-to-body
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        width="40%"
    >
      <bindPlatformEditModel
          :formQuery="formQuery"
          :editQuery="editLogin"
          :closeDialog="closeDialogEdit"
      ></bindPlatformEditModel>
    </el-dialog>

  </div>
</template>

<script>
import {deletePlatform, testUserInfo} from "@/api/userManage/platform";
import {getCorpLists, getPlatFormDelect} from "@/api/basic/basicUnitMaintain";
import {getBussinessType} from "@/api/basic/bussinessType";
import {isBlank} from "@/utils/strUtil";
import bindPlatformEditModel from "@/views/userManage/bindPlatformEditModel";

export default {
  name:'bindPlatformModel',
  props: {
    closeDialog: {
      type: Function,
      required: true,
    },
    editQuery: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      editLogin: {
        id: null,
        unitId: null,
        name: null,
        corpName: null,
        platformId: null,
        platformUsername: null,
        platformPassword: null,
        appid: null,
        appKey: null,
        sceretKey: null,
        sourceAction: null,
        targetAction: null,
        platformList: [],
        invCode: "",
        invSubCode: "",
      },
      platformRules: {
        corpName: [
          {required: true, message: "请选择往来单位", trigger: "change"}
        ],
        platformId: [
          {required: true, message: "请选择自助平台", trigger: "change"}
        ],
        invCode: [
          {required: true, message: "请选择目标分库", trigger: "change"}
        ],
        invSubCode: [
          {required: true, message: "请选择目标分库", trigger: 'change'}
        ],
        platformUsername: [
          {required: true, message: "请输入账号", trigger: "change"}
        ],
        platformPassword: [
          {required: true, message: "请输入密码", trigger: "change"}
        ],
        sourceAction: [
          {required: true, message: "请选择本地单据类型", trigger: "change"}
        ],
        targetAction: [
          {required: true, message: "请选择目标单据类型", trigger: "change"}
        ]
      },
      editDialogVisible: false,
      corpNamesList: [],
      delectList: [],
      curPlatformId: "",
      targetPlatformEdit: true, //目标平台启用编辑
      editPlatformQuery: {
        unitId: null,
        name: null,
        host: null
      },
      query: {
        platformId: null,
        page: 1,
        limit: 20
      },
      formName:{
        1 : "添加客户信息",
        2 : "编辑客户信息"
      },
      formQuery:null,
      editLoginVisible:false,
      platformList: [],
      bussinessTypes: [],
      targetActions: [],
      targetInvs: [],
      targetSubInvs: [],
      total: 0,
      check:false
    };
  },
  components: {bindPlatformEditModel},
  methods: {
    getUnitMaintain() {
      getCorpLists()
          .then((response) => {
            this.corpNamesList = response.data || [];
          })
          .catch(() => {
            this.loading = false;
          });
    },
    intentPlatform(value) {
      this.editPlatformQuery.id = value;
      this.curPlatformId = value;
      this.targetPlatformEdit = true;
      //拉取对应平台的单据类型
      this.getTargetInv(value);
    },
    testUserInfo() {
      if (isBlank(this.editLogin.platformId)) {
        this.$message.error("请选择自助平台");
        return;
      }
      if (isBlank(this.editLogin.platformUsername)) {
        this.$message.error("请输入用户名");
        return;
      }
      if (isBlank(this.editLogin.platformPassword)) {
        this.$message.error("请输入密码");
        return;
      }
      let data = {
        platformId: this.editLogin.platformId,
        username: this.editLogin.platformUsername,
        password: this.editLogin.platformPassword
      };
      testUserInfo(data).then((res) => {
        if (res.code === 20000) {
          this.targetPlatformEdit = false;
          this.check=true;
          this.editLogin.appid=res.data.appid;
          this.editLogin.apiKey=res.data.apiKey;
          this.editLogin.secretKey=res.data.secretKey;
          this.editLogin.customerId=res.data.customerId;
          this.$message.success("验证成功");
        } else {
          this.$message.error(res.message);
        }
      }).catch((error) => {
      });
    },
    closeDialog() {
      this.orderMutiVisible = false;
      this.getPlatFormDelect();
    },
    deleteDialog(row) {
      this.$confirm("确认删除该自助平台信息?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
          .then(() => {
            let tQuery = {
              id: row.id
            };
            deletePlatform(tQuery).then(() => {
              this.getPlatFormDelect()
            });
          })
          .catch(() => {
          });
    },
    getLocalBussinessType() {
      let tQuery = {};
      getBussinessType(tQuery).then((res) => {
        this.bussinessTypes = res.data.list || [];
      }).catch((error) => {
        this.$message.error(error.message);
      });
    },
    handleCurrentChange(val) {
      this.query.page = val.page;
      this.getPlatFormDelect();
    },
    addDelect(row){
      if(this.check=true){
        if(row==null){
          this.formQuery=1;
          this.editLoginVisible=true;
        }else{
          this.formQuery=2;
          this.editLogin.id=row.id
          this.editLogin.platformId = row.platformId;
          this.editLogin.appid = row.appid;
          this.editLogin.apiKey = row.apiKey;
          this.editLogin.secretKey = row.secretKey;
          this.editLogin.unitId = row.unitId;
          this.editLogin.customerId = row.customerId
          this.editLogin.sourceAction = row.sourceAction
          this.editLogin.invCode = row.invCode
          this.editLogin.invSubCode = row.invSubCode
          this.editLogin.targetAction = row.targetAction
          console.log(this.editLogin)
          this.editLoginVisible=true;
        }
      }else{
        this.$message.error("请先验证账号信息！");
      }
    },
    closeDialogEdit(){
      this.editLoginVisible=false;
      this.getPlatFormDelect();
    },
    //获取详情列表
    getPlatFormDelect() {
      this.query.platformId=this.editLogin.platformId
      getPlatFormDelect(this.query).then((res) => {
        this.delectList = res.data.list || [];
        this.total = res.data.total || 0;
      }).catch((error) => {
        this.$message.error(error.message);
      });
    },
  },
  created() {
    this.editLogin.platformId = this.editQuery.platformId;
    this.editLogin.name = this.editQuery.name;
    this.editLogin.unitId=this.editQuery.unitId;
    this.editLogin.customerId=this.editQuery.customerId
    this.editLogin.sourceAction = this.editQuery.sourceAction
    this.editLogin.invCode = this.editQuery.invCode
    this.editLogin.invSubCode = this.editQuery.invSubCode
    this.editLogin.targetAction = this.editQuery.targetAction
    if(this.editLogin.unitId!=null){
      this.editDialogVisible=true
    }
    this.getUnitMaintain()
    this.getLocalBussinessType()
    this.getPlatFormDelect()
  }
};
</script>

<style scoped>

</style>
