<template>
  <div class="app-container home">
    <template v-if="companyInfo">
      <div class="system-container">
        <div class="page-header-content home-card">
          <div class="enterprise-title" v-if="companyInfo.authStatus === '0'">
            <div class="greeting-title">上午好，{{ companyInfo.name }}！</div>
            <span class="information-status">您提交的企业信息 <span class="under-review">正在审核中</span>，请耐心等待</span>
            <span class="complete-tip">联系客服 <i class="el-icon-arrow-right"></i></span>
          </div>
          <div class="enterprise-title" v-else-if="companyInfo.authStatus === '1'">
            <div class="greeting-title">上午好，{{ companyInfo.name }}！</div>
            <span class="information-status">{{ companyInfo.name }}</span>
            <span class="complete-tip success-review">认证成功</span>
          </div>
          <div class="enterprise-title" v-else-if="companyInfo.authStatus === '2'">
            <div class="greeting-title">上午好，{{ companyInfo.name }}！</div>
            <span class="information-status">
              您企业信息审核<span class="failed-review" @click="completeInfo">未通过</span>，请您 <span class="link-type" @click="completeInfo">重新提交信息</span>
            </span>
            <span class="complete-tip" @click="completeInfo">去完善 <i class="el-icon-arrow-right"></i></span>
            <div class="failed-reason">原因：{{ companyInfo.remarks }}</div>
          </div>
        </div>
        <div class="quick-entry-card home-card">
          <div class="card-header">
            <div class="card-header-title">
              快捷入口
            </div>
            <div class="card-header-action" @click="quickMenuVisible = true; saveHaveList = JSON.parse(JSON.stringify(menuObject.haveList))">
              设置快捷入口
            </div>
          </div>
          <div class="card-body">
            <el-row class="quick-entry-list">
              <template v-if="menuObject.haveList.length">
                <el-col :span="4" v-for="(item,index) in menuObject.haveList" :key="index">
                  <div class="quick-entry-item pointer" @click="handleQuick(item)">
                    <svg-icon :icon-class="item.icon" :style="{fill:menuColor[index%3]}" />
                    <div class="item-name">{{ item.menuName }}</div>
                  </div>
                </el-col>
              </template>
              <div v-else style="margin-top: 15px">暂无数据</div>
            </el-row>
          </div>
        </div>
        <div class="enterprise-information-card home-card">
          <div class="card-header">
            <div class="card-header-title">
              企业信息
            </div>
          </div>
          <div class="card-body">
            <el-row class="body-list" :gutter="15">
              <el-col :span="4" v-for="item in enterpriseInformationList" :key="item.name">
                <div class="enterprise-information-item list-item" :style="{background:item.background}">
                  <img class="item-img" :src="item.img" />
                  <div class="item-info-wrap">
                    <div class="item-name">{{ item.name }}</div>
                    <span class="item-count">{{ item.count }}</span>
                  </div>
                </div>
              </el-col>
              <el-col :span="4" v-for="item in projectOverviewList" :key="item.name">
                <div class="project-overview-item list-item" :style="{background:item.background}">
                  <img class="item-img" :src="item.img" />
                  <div class="item-info-wrap">
                    <div class="item-name">{{ item.name }}</div>
                    <span class="item-count">{{ item.count }}</span>
                  </div>
                </div>
              </el-col>
            </el-row>
          </div>

        </div>
      </div>
      <div class="members-container">
        <div class="member-info-card">
          <div class="member-card-header">
            <img class="card-chip" src="../assets/images/index/VIP.png" />
          </div>
          <div class="card-number">
            <div class="valid-info">
              您的会员时长剩余<span class="number">{{ companyInfo.endLength || 0 }}</span>
<!--              <dict-tag :options="dict.type.buyer_duration_unit" :value="companyInfo.unit" v-if="companyInfo.unit" />-->
<!--              <span v-else>小时</span>-->
            </div>
            <div class="renewal-btn" @click="handleRenewal">去续费</div>
          </div>
          <div class="card-footer">
            <div class="expiration-time-wrap">
              <div class="title">到期时间</div>
              <div class="time">
                <template v-if="companyInfo.expireTime">
                  {{ parseTime(companyInfo.expireTime, '{y}-{m}-{d} {h}:{i}:{s}') }}
                </template>
                <template v-else>已到期</template>
              </div>
            </div>
          </div>
        </div>
        <div class="member-notice-card">
          <div class="card-header">
            <div class="card-header-title">
              企业信息
            </div>
          </div>
          <div class="notice-list" v-loading="messageLoading">
            <template v-if="messageList.length">
              <div class="notice-item" v-for="item in messageList" :key="item.id+item.type">
                <div class="notice-content">{{ `${item.name} 于 ${item.date} ${item.message}，还未上传` }}<span class="link-type">{{ item.type }}</span>,请尽快上传</div>
                <div class="notice-operation-wrap" v-if="item.type==='过程文件'">
                  <div class="operation-button link-gray" @click="handleCloseNotice(item,'todayStatu')">今日不再提醒</div>
                  <div class="operation-button link-gray" @click="handleCloseNotice(item,'closeStatu','todayStatu')">关闭提醒</div>
                </div>
                <div class="notice-operation-wrap" v-if="item.type==='验收资料'">
                  <div class="operation-button link-gray" @click="handleCloseNotice(item,'todayStatus')">今日不再提醒</div>
                  <div class="operation-button link-gray" @click="handleCloseNotice(item,'closeStatus','todayStatus')">关闭提醒</div>
                </div>
              </div>
            </template>
            <el-empty description="暂无消息" v-else></el-empty>
          </div>
        </div>
      </div>
    </template>
    <template v-else>
      <div class="system-container">
        <div class="page-header-content home-card">
          <div class="greeting-title">上午好，{{ nickName }}！</div>
          <div class="enterprise-title">
            <span class="information-status">您还未完善企业信息，暂时不能使用所有功能</span>
            <span class="complete-tip" @click="completeInfo">去完善 <i class="el-icon-arrow-right"></i></span>
          </div>
        </div>
      </div>
    </template>
    <el-dialog
      title="完善企业信息"
      :visible.sync="visible"
      width="30%"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="营业执照" prop="fileNames">
          <ImageUpload v-model:value="form.fileNames" :file-size="1" :limit="1"></ImageUpload>
        </el-form-item>
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入企业名称" />
        </el-form-item>
        <el-form-item label="信用代码" prop="code">
          <el-input v-model="form.code" placeholder="请输入统一信用代码" />
        </el-form-item>
        <el-form-item label="联系人" prop="contacts">
          <el-input v-model="form.contacts" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phoneNumber">
          <el-input v-model="form.phoneNumber" placeholder="请输入企业联系人电话" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="visible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="设置快捷入口"
      :visible.sync="quickMenuVisible"
      width="70%"
      append-to-body
    >
      <div class="quick-menu-title">首页入口</div>
      <el-row class="quick-entry-list">
        <template v-if="saveHaveList.length">
          <el-col :span="4" v-for="(item,index) in saveHaveList" :key="index">
            <div class="quick-entry-item">
              <i class="el-icon-close" @click="handleRemove(item,index)"></i>
              <svg-icon :icon-class="item.icon" />
              <div class="item-name">{{ item.menuName }}</div>
            </div>
          </el-col>
        </template>
        <div v-else style="margin-top: 15px">暂无数据</div>
      </el-row>
      <el-divider />
      <div class="quick-menu-title">其他入口</div>
      <el-row class="quick-entry-list">
        <template v-if="menuObject.sysMenuList.length">
          <el-col :span="4" v-for="(item,index) in menuObject.sysMenuList" :key="index">
            <div class="quick-entry-item" @click="handleAddToHome(item,index)">
              <svg-icon :icon-class="item.icon" />
              <div class="item-name">{{ item.menuName }}</div>
            </div>
          </el-col>
        </template>
        <div v-else style="margin-top: 15px">暂无数据</div>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button @click="quickMenuVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSave">保 存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { batchAddQuickMenu, company, messageList, project, quickMenu } from '@/api'
import ImageUpload from '@/components/ImageUpload'
import store from '@/store'
import { deepClone } from '@/utils'
import { updateProject } from '@/api/buyer/project'
import { addCompanyInfo, getCompanyInfo, updateCompanyInfo } from '@/api/buyer/company'

export default {
  name: 'Index',
  components: {
    ImageUpload
  },
  dicts: ['buyer_company_auth_status', 'buyer_duration_unit'],
  data() {
    return {
      visible: false,
      quickMenuVisible: false,
      enterpriseInformationList: {
        employeeTotal: {
          img: require('../assets/images/index/head-count.png'),
          name: '总人数',
          count: 0,
          background: 'linear-gradient(360deg, rgba(255,248,244,0.84) 0%, #FDEFE6 100%)'
        },
        deviceTotal: {
          img: require('../assets/images/index/device.png'),
          name: '设备',
          count: 0,
          background: 'linear-gradient(360deg, rgba(215,246,255,0.52) 0%, #D8F6FF 100%)'
        },
        professionalDeviceTotal: {
          img: require('../assets/images/index/professional-equipment.png'),
          name: '专用设备',
          count: 0,
          background: 'linear-gradient(360deg, rgba(227,197,255,0.34) 0%, #E0C0FF 100%)'
        }
      },
      projectOverviewList: {
        onTotal: {
          img: require('../assets/images/index/ongoing.png'),
          name: '进行中',
          count: 0,
          background: 'linear-gradient(360deg, rgba(255,248,244,0.84) 0%, #FDEFE6 100%)'
        },
        acceptTotal: {
          img: require('../assets/images/index/accepted.png'),
          name: '已验收',
          count: 0,
          background: 'linear-gradient(360deg, rgba(215,246,255,0.52) 0%, #D8F6FF 100%)'
        },
        overtimeTotal: {
          img: require('../assets/images/index/timeouted.png'),
          name: '已超时',
          count: 0,
          background: 'linear-gradient(360deg, rgba(227,197,255,0.34) 0%, #E0C0FF 100%)'
        }
      },
      companyInfo: store.getters.companyInfo || '',
      form: {},
      // 表单校验
      rules: {
        businessLicense: [
          { required: true, message: '请上传营业执照', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入统一信用代码', trigger: 'blur' }
        ],
        contacts: [
          { required: true, message: '请输入联系人', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/, message: '请输入正确的手机号码', trigger: 'blur' }
        ]
      },
      menuObject: {
        haveList: [],
        sysMenuList: []
      },
      saveHaveList: [],
      menuColor: ['#2379db', '#20ba1b', '#f08828'],
      messageList: [],
      messageLoading: false,
      timer: null,
      nickName: store.getters.nickName,
      user: store.getters.user
    }
  },
  mounted() {
    this.initData()
  },
  beforeDestroy() {
    clearInterval(this.timer)
  },
  methods: {
    // 去续费
    handleRenewal() {
      this.$router.push('/system/goods')
    },
    // 保存快捷菜单
    handleSave() {
      batchAddQuickMenu(this.saveHaveList).then(res => {
        this.$message.success('保存成功')
        this.quickMenuVisible = false
        this.getQuickMenu()
      })
    },
    // 点击删除已选择菜单
    handleRemove(item, index) {
      this.saveHaveList.splice(index, 1)
      this.menuObject.sysMenuList.unshift(item)
    },
    // 添加到首页入口
    handleAddToHome(item, index) {
      this.menuObject.sysMenuList.splice(index, 1)
      this.saveHaveList.push(item)
    },
    // 关闭提示
    handleCloseNotice(msg, field1, field2 = '') {
      let params = deepClone(msg)
      params[field1] = '1'
      if (field2) {
        params[field2] = '1'
      }
      updateProject(params).then(res => {
        this.getMessageList()
      })
    },
    // 首页加载 初始化数据
    initData() {
      company().then(res => {
        this.handleData(this.enterpriseInformationList, res)
      })
      project().then(res => {
        this.handleData(this.projectOverviewList, res)
      })
      this.getQuickMenu()
      this.getMessageList()
      this.timer = setInterval(() => {
        this.getMessageList()
      }, 1000 * 60 * 5)
    },
    // 获取用户快捷菜单配置
    getQuickMenu() {
      quickMenu().then(res => {
        this.menuObject = res
      })
    },
    // 获取消息通知列表
    getMessageList() {
      this.messageLoading = true
      messageList().then(res => {
        this.messageList = res.data
      }).finally(() => {
        this.messageLoading = false
      })
    },
    // 处理企业信息以及项目概况后台返回数据
    handleData(target, res) {
      const keys = Object.keys(target)
      keys.forEach(key => {
        target[key].count = res[key]
      })
    },
    // 完善企业信息 获取企业详情
    completeInfo() {
      this.visible = true
      if (store.getters.companyInfo) {
        getCompanyInfo(store.getters.companyInfo.id)
          .then(res => {
            this.form = res.data
          })
      }
    },
    // 提交企业信息
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.authStatus = '0'
            updateCompanyInfo(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.visible = false
            })
          } else {
            const params = { ...this.form, userId: this.user.userId, userName: this.user.userName, phone: this.user.phonenumber }
            addCompanyInfo(params).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.visible = false
              window.location.reload()
            })
          }
        }
      })
    },
    // 快捷菜单跳转
    handleQuick(item) {
      this.$router.push({
        path: `/${item.parentPath}/${item.path}`
      })
    }
  }
}
</script>

<style scoped lang="scss">
.home {
  width: 100%;
  height: calc(100vh - 100px);
  display: flex;

  .card-header {
    display: flex;
    align-items: center;
    font-size: 16px;
    font-weight: bold;
    color: #1379CD;
    line-height: 21px;

    &::before {
      content: "";
      display: inline-block;
      width: 5px;
      height: 15px;
      background: #2379DB;
      border-radius: 4px;
      margin-right: 6px;
    }

    .card-header-title {
      flex: 1;
    }

    .card-header-action {
      width: 109px;
      height: 36px;
      border-radius: 4px;
      border: 1px solid #2379DB;
      font-size: 14px;
      font-family: MicrosoftYaHei;
      color: #2379DB;
      display: flex;
      align-items: center;
      justify-content: center;
      cursor: pointer;
    }
  }

  .system-container {
    font-size: 16px;
    color: #333333;
    line-height: 21px;
    flex: 1;
    margin-right: 24px;

    .page-header-content {
      font-size: 16px;
      color: #333333;
      padding: 8px 24px;
      font-weight: 500;
      font-family: -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, Noto Sans, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji;

      .enterprise-title {
        margin-top: 20px;

        .greeting-title {
          margin-bottom: 15px;
        }

        .link-type {
          color: #1379CD;
          font-weight: normal;
        }

        .under-review {
          color: #F59A23;
        }

        .failed-review {
          color: #D9001B;
          cursor: pointer;
        }

        .complete-tip {
          color: #1379CD;
          display: inline-block;
          margin-left: 40px;
          margin-right: 15px;
          cursor: pointer;

          &.success-review {
            color: #70B603;
            cursor: default;
          }
        }

        .failed-reason {
          margin-top: 15px;
          font-size: 14px;
          color: #7F7F7F;
          font-weight: 400;
        }
      }
    }

    .quick-entry-card {
      margin-top: 40px;
    }

    .card-body {

      .list-item {
        height: 182px;
        background: linear-gradient(360deg, rgba(255, 248, 244, 0.84) 0%, #FDEFE6 100%);
        border-radius: 12px;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        margin-top: 30px;
        overflow: clip;

        .item-img {
          height: 125px;
          max-width: 90%;
        }

        .item-info-wrap {
          width: 100%;
          padding: 0 15px;
          margin-top: 15px;
          display: flex;

          .item-name {
            flex: 1;
            text-align: left;
          }

          .item-count {
            font-size: 26px;
            font-weight: bold;
            color: #333333;
          }
        }
      }
    }

    .home-card {
      padding: 24px 24px 40px;
      background: #FFFFFF;
      box-shadow: 0px 0px 13px 0px rgba(0, 0, 0, 0.16);
      border-radius: 12px;
    }

    .enterprise-information-card {
      margin-top: 24px;
    }
  }

  .members-container {
    width: 510px;
    height: 100%;
    background: #FFFFFF;
    box-shadow: 0px 0px 14px 0px rgba(0, 0, 0, 0.2);
    border-radius: 12px;
    padding: 25px 16px;
    display: flex;
    flex-direction: column;

    .member-info-card {
      width: 100%;
      height: 240px;
      border-radius: 8px;
      color: #FFFFFF;
      padding: 30px 30px 16px;
      display: flex;
      flex-direction: column;
      background: url('../assets/images/index/card-bg.png') 100% 100% no-repeat;

      .member-card-header {
        display: flex;
        align-items: center;
        flex: 1;
        color: #fff;

        .card-chip {
          width: 55px;
          height: 50px;
        }

        .valid-date {
          display: inline-block;
          margin: 0 10px;
          font-size: 24px;
          font-weight: bold;
        }
      }

      .card-number {
        margin-top: 25px;
        font-size: 21px;
        letter-spacing: 5px;
        flex: 1;
        display: flex;

        .valid-info {
          flex: 1;
        }

        .number {
          font-size: 30px;
          display: inline-block;
          margin: 0 8px;
        }

        .renewal-btn {
          width: 90px;
          height: 29px;
          background: #FFFFFF;
          border-radius: 4px;
          font-size: 16px;
          color: #43A8EC;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
        }
      }

      .card-footer {
        display: flex;
        flex: 1;
        margin-top: 30px;
        align-items: flex-end;

        .expiration-time-wrap {
          display: flex;
          flex: 1;
          font-size: 14px;
          align-items: center;

          .title {
            font-size: 14px;
            color: #fff;
          }

          .time {
            margin-left: 10px;
          }
        }
      }
    }

    .member-notice-card {
      margin-top: 24px;
      flex: 1;
      overflow: auto;

      .notice-list {
        min-height: 150px;

        .notice-item {
          background: #FFFFFF;
          box-shadow: 0px 2px 12px 0px rgba(0, 0, 0, 0.13);
          border-radius: 6px;
          padding: 20px 15px 12px 30px;
          font-size: 14px;
          color: #333333;
          width: calc(100% - 8px);
          margin-left: 4px;
          margin-top: 20px;

          .notice-content {
            .link-type {
              display: inline-block;
              margin: 0 10px;
            }
          }

          .notice-operation-wrap {
            display: flex;
            justify-content: flex-end;
            font-size: 14px;
            color: #666666;
            margin-top: 24px;

            .operation-button {
              margin-left: 20px;
            }
          }
        }
      }
    }
  }
}

.quick-entry-item {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  margin-top: 30px;
  text-align: center;
  position: relative;

  .el-icon-close {
    position: absolute;
    top: -9px;
    right: 15px;
    font-size: 18px;
    cursor: pointer;
  }

  .svg-icon {
    width: 36px;
    height: 36px;
  }

  .item-name {
    margin-top: 10px;
    font-size: 14px;
    color: #333333;
  }
}
</style>

