<template>
  <div class="page">
    <div class="warpper mt24">
      <el-card>
        <div class="download-card">
          <img :src="iconDownload" alt="图标" />
          <div class="text-box">
            <div class="title">百望云PC版（支持数电发票）</div>
            <div class="text">
              百望云全功能客户端，整合多种票财税云服务，税控盘、金税盘、税务Ukey用户均可使用。下载即享云开票体验。
            </div>
            <div class="actions">
              <el-button type="text" style="cursor: default">
                现有客户、商品和税控发票已同步，无感切换
                <i class="el-icon-d-arrow-right el-icon--right"></i>
              </el-button>
              <el-button type="primary" @click="handleDownload">
                立即下载<i class="el-icon-download el-icon--right"></i>
              </el-button>
            </div>
          </div>
        </div>
      </el-card>

      <el-alert
        title="操作提示"
        type="warning"
        description="请您下载并安装百望云开票软件后联系客服完成配置。"
        :closable="false"
        show-icon
        class="mt24"
      >
      </el-alert>

      <el-descriptions
        title="百望云账户："
        :column="2"
        border
        class="mt24"
        :labelStyle="{ 'text-align': 'right' }"
      >
        <template slot="extra">
          <el-button
            plain
            size="small"
            @click="handleRefreshUserInfo"
            :disabled="loadingRefresh"
          >
            刷新
            <i class="el-icon-refresh-right el-icon--right"></i>
          </el-button>
        </template>

        <el-descriptions-item label="用户名">
          <el-input v-model="account" readonly v-if="account"></el-input>
          <div v-else>-</div>
        </el-descriptions-item>
        <el-descriptions-item label="用户密码">
          <el-input
            v-model="password"
            readonly
            :show-password="showPassword"
            class="password-box"
            v-if="password"
          >
            <el-button
              slot="suffix"
              type="text"
              @click="showPassword = !showPassword"
            >
              <i class="el-icon-view el-icon--right"></i>
              {{ showPassword ? "查看" : "隐藏" }}
            </el-button>
          </el-input>
          <div v-else></div>
        </el-descriptions-item>
        <el-descriptions-item label="迁移商品信息">
          <el-button
            type="primary"
            plain
            @click="handleProductSyncBW"
            :disabled="!account"
          >
            同步到百望云
            <i class="el-icon-upload el-icon--right"></i>
          </el-button>
        </el-descriptions-item>
        <el-descriptions-item label="迁移客户信息">
          <el-button
            type="primary"
            plain
            @click="handleCustomerSyncBW"
            :disabled="!account"
          >
            同步到百望云
            <i class="el-icon-upload el-icon--right"></i>
          </el-button>
        </el-descriptions-item>
      </el-descriptions>
    </div>
  </div>
</template>

<script>
import { getEntBWCloudInfo, syncCustomer, syncProduct } from "@/api/bw";
import cache from "@/utils/cache";

export default {
  name: "digital_electronic_invoice",
  data() {
    return {
      iconDownload: require("@/assets/icons/download.png"),
      loadingRefresh: false,

      account: "",
      password: "",
      showPassword: true,
    };
  },
  mounted() {
    this.handleRefreshUserInfo(false);
  },
  methods: {
    useErrorMessage(use = true, message) {
      if (message) {
        if (use) this.$message.error(message);
        console.error(message);
      }
    },

    handleDownload() {
      window.open(
        "https://zhushou.efapiao.com/download-efpzs/efpzs/BWClient-SMB.exe"
      );
    },
    handleRefreshUserInfo(useMessage = true) {
      const company = JSON.parse(cache.getSession("company"));
      if (!company) {
        this.useErrorMessage(useMessage, "抱歉...请选择企业后重试！");
        return false;
      }

      this.loadingRefresh = true;
      getEntBWCloudInfo(company.entTaxId)
        .then((val) => {
          if (val.data) {
            this.account = val.data.account;
            this.password = val.data.passwd;

            // 获取账户成功后执行同步；
            this.handleProductSyncBW(false);
            this.handleCustomerSyncBW(false);
          } else {
            this.useErrorMessage(useMessage, "暂无数据，请稍后重试！");
          }
        })
        .catch((err) => {
          this.useErrorMessage(useMessage, err);
        })
        .finally(() => {
          this.loadingRefresh = false;
        });
    },
    handleProductSyncBW(useMessage = true) {
      const company = JSON.parse(cache.getSession("company"));
      syncProduct(company.entTaxId)
        .then((val) => {
          if (val.data) {
            if (useMessage) {
              if (val.data.canSync) {
                this.$message.success(
                  `商品信息同步开始...预估耗时 ${val.data.preTime} 秒！`
                );
              } else {
                this.$message.success(val.data.remark);
              }
            }
          } else {
            this.useErrorMessage(useMessage, "结果未知！");
          }
        })
        .catch((err) => {
          this.useErrorMessage(useMessage, err);
        })
        .finally(() => {});
    },
    handleCustomerSyncBW(useMessage = true) {
      const company = JSON.parse(cache.getSession("company"));
      syncCustomer(company.entTaxId)
        .then((val) => {
          if (val.data) {
            if (useMessage) {
              if (val.data.canSync) {
                this.$message.success(
                  `客户信息同步开始...预估耗时 ${val.data.preTime} 秒！`
                );
              } else {
                this.$message.success(val.data.remark);
              }
            }
          } else {
            this.useErrorMessage(useMessage, "结果未知！");
          }
        })
        .catch((err) => {
          this.useErrorMessage(useMessage, err);
        })
        .finally(() => {});
    },
  },
};
</script>

<style lang="stylus" scoped>
.page{
    display: flex;
    flex-direction: column;
    align-items: center;
}

.warpper {
  width: 600px;
}

.mt24{
    margin-top: 24px;
}

.download-card{
    display: flex;

    img{
      width: 150px;
      height: 150px;
    }

    .text-box{
        margin-top: 8px;

        .title{
            text-overflow: ellipsis;
            white-space: nowrap;
            font-size: 24px;
            line-height: 40px;
        }
    }

    .text{
        color: #999;
        font-size: 12px;
        line-height: 20px;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    .actions{
        margin-top: 16px;
    }
}

::v-deep {
  .el-descriptions__header{
    margin-bottom: 12px;
  }
  .el-input__inner{
    border: none;
    padding: 0;
  }
}
</style>
