<template>
  <div class="hintDetail">
    <div class="header">
      <van-nav-bar class="navBar" title="门店拜访" left-arrow @click-left="onClickLeft" />
    </div>
    <div class="tabBox">
      <van-tabs class="myTab" v-model="activeTabName" color="#0057ba" @change="tabChange">
        <van-tab
          v-for="(item, index) in tabsItem"
          :title="item.title"
          :name="item.name"
          :key="index"></van-tab>
      </van-tabs>
      <van-tabs
        class="myTab"
        v-model="activaPantoneName"
        color="#0057ba"
        @change="tabChangePantone"
        v-if="activeTabName == 'pantone'">
        <van-tab
          v-for="(val, key, index) in tabsItemPantone"
          :key="index"
          :title="key"
          :name="key"></van-tab>
      </van-tabs>
      <van-tabs
        class="myTab"
        v-model="activaPantoneName"
        color="#0057ba"
        @change="tabChangePantone"
        v-if="activeTabName == 'colorWarning'">
        <van-tab
          v-for="(val, key, index) in tabsItemPantone"
          :key="index"
          :title="key=='keKong'?'可控店':'金牌店'"
          :name="key"></van-tab>
      </van-tabs>
    </div>
    <div class="content">
      <el-table
        v-if="fromType"
        :data="tableList"
        :load="load"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        border
        class="elTreeTable"
        lazy
        :row-key="rowKey"
        :key="toggleIndex">
        <el-table-column
          class="firstName"
          :label="firstLabel"
          :prop="firstProp"
          align="center"
          width="120">
          <template slot-scope="scope">
            <span>{{ scope.row[firstProp] }}</span>
            <!-- <span v-if="scope.row.reportType != 1">{{ scope.row.deptName }}</span> -->
          </template>
        </el-table-column>
        <el-table-column
          :label="item.label"
          width="46"
          align="center"
          v-for="(item, index) in tableColumn"
          :key="index"
          v-if="tableColumn.length">
          <el-table-column
            :label="val.label"
            :prop="val.prop"
            width="46"
            align="center"
            v-if="item.childredColumn"
            v-for="(val, ind) in item.childredColumn"
            :key="ind">
            <template slot-scope="scope">
              <span>{{ Micrometer(scope.row[val.prop]) }}</span>
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
      <div class="mask" v-if="maskShow"><van-loading type="spinner" color="#1989fa" /></div>
    </div>
  </div>
</template>
<script>
import {
  selectNoVisitsInfo,
  selectPendingCasesInfo,
  getReportMaterialType,
  getReportMaterial,
  selectColorMixerStoreInfo,
  getColorWarning
} from '@/api/index';
import { mapState } from 'vuex';
export default {
  name: 'hintDetail',
  computed: {
    ...mapState({
      userInfo: (state) => state.user.userInfo,
    }),
  },
  data() {
    return {
      empLevel: '',
      deptId: null,
      fromType: null,
      activeTabName: 'noVisit',
      activaPantoneName: '',
      tabsItem: [
        {
          title: '未拜访',
          name: 'noVisit',
        },
        {
          title: '建店',
          name: 'createStore',
        },
        // {
        //   title: '色卡',
        //   name: 'pantone',
        // },
      ],
      activePantoneTabName: '', //色卡/调色预警tabName
      tabsItemPantone: {}, //色卡/调色预警tabItem
      toggleIndex: 0,
      firstLabel: '',
      firstProp: '',
      tableColumn: [],
      tableList: [],
      // 未拜访
      noVisit: [
        {
          label: '可控店',
          childredColumn: [
            {
              label: '(当月)',
              prop: 'keKongNotVisited',
            },
          ],
        },
        {
          label: '金牌店',
          childredColumn: [
            {
              label: '(当月)',
              prop: 'jinPaiNotVisited',
            },
          ],
        },
        {
          label: '同城分销-涂料店',
          childredColumn: [
            {
              label: '(近2月)',
              prop: 'TuLiaoNotVisited',
            },
          ],
        },
        {
          label: '同城分销-其他店',
          childredColumn: [
            {
              label: '(近3月)',
              prop: 'QiTaNotVisited',
            },
          ],
        },
      ],
      // 建店
      createStore: [
        {
          label: '可控店',
          childredColumn: [
            {
              label: '(45天未结案)',
              prop: 'keKongAbnormalNum',
            },
          ],
        },
        {
          label: '金牌店',
          childredColumn: [
            {
              label: '(7天未完工)',
              prop: 'jinPaiUnfinishedNum',
            },
            {
              label: '(30天未结案)',
              prop: 'jinPaiAbnormalNum',
            },
          ],
        },
        {
          label: '同城分销-涂料店',
          childredColumn: [
            {
              label: '(7天未结案)',
              prop: 'TuLiaoAbnormalNum',
            },
          ],
        },
        {
          label: '同城分销-其他店',
          childredColumn: [
            {
              label: '(7天未结案)',
              prop: 'QiTaAbnormalNum',
            },
          ],
        },
      ],
      // 色卡
      pantone: [
        {
          label: '直发销售部',
          childredColumn: [
            {
              label: '(本)',
              prop: 'zfxsbNum',
            },
          ],
        },
        {
          label: '已领用',
          childredColumn: [
            {
              label: '(本)',
              prop: 'ywyylyNum',
            },
          ],
        },
        {
          label: '门店发放',
          childredColumn: [
            {
              label: '(本)',
              prop: 'ywyyffNum',
            },
          ],
        },
        {
          label: '门店未签收',
          childredColumn: [
            {
              label: '(本)',
              prop: 'mdwqsNum',
            },
          ],
        },
        {
          label: '未签收门店',
          childredColumn: [
            {
              label: '(家)',
              prop: 'wqsmdNum',
            },
          ],
        },
      ],
      // 调色预警
      colorWarning: [
        {
          label: '滚动7天未调色机',
          childredColumn: [
            {
              label: '(家)',
              prop: 'colorSeven',
            },
          ],
        },
        {
          label: '当月未调色',
          childredColumn: [
            {
              label: '(家)',
              prop: 'colorMonth',
            },
          ],
        },
        {
          label: '从未调色',
          childredColumn: [
            {
              label: '(家)',
              prop: 'colorStop',
            },
          ],
        },
        {
          label: '布机但近两年DIS为0',
          childredColumn: [
            {
              label: '(家)',
              prop: 'colorDis',
            },
          ],
        },
        {
          label: '合计',
          childredColumn: [
            {
              label: '(家)',
              prop: 'colorMixerSum',
            },
          ],
        },
      ],
      materialCode: null, //色卡物料来源
      maskShow: true,
      controller: null, //取消请求
    };
  },
  watch: {
    // 监听tab切换的状态
    fromType: {
      handler(newVal) {
        this.tableColumn = this[newVal]; //表格头
        this.activeTabName = newVal; // 当前激活tab
      },
      immediate: true,
    },
    // 初始化监听fromType，不同类型显示不同的tabItem
    '$route.query.fromType': {
      handler(newVal) {
        if (newVal) this.setTabsItem();
      },
      immediate: true,
    },
  },
  activated() {
    this.empLevel = this.userInfo.empLevel;
    this.firstLabel = this.empLevel == '1' ? '组织名称/大区主管' : '组织名称/销售部主管';
    this.fromType = this.$route.query.fromType; // fromType: 'noVisit':未拜访; 'createStore':建店; 'pantone':色卡
    this.deptId = this.userInfo.deptId; // 当前用户部门id
    this.activaPantoneName = this.$route.query.pantoneName; //当前色卡
    this.materialCode = this.$route.query.materialCode; // 色卡code
    this.controller = new AbortController(); //取消请求
    this.initData();
  },
  methods: {
    rowKey(row) {
      return row['onlyId'] || row['warehouseCode'];
      // if (this.fromType == 'noVisit' || this.fromType == 'createStore') {
      //   return row['onlyId'];
      // } else if (this.fromType == 'pantone') {
      //   return row['warehouseCode'];
      // }
    },
    setTabsItem() {
      this.tabsItem = [
        {
          title: '未拜访',
          name: 'noVisit',
        },
        {
          title: '建店',
          name: 'createStore',
        },
        {
          title: '调色预警',
          name: 'colorWarning',
        },
      ];
      // 业务员不显示色卡
      if (this.userInfo.empLevel != '3') {
        this.tabsItem.push({
          title: '色卡',
          name: 'pantone',
        });
      }
    },
    /**
     * 初始化数据和搜索操作接口的入口
     * @param {*function} resolve //子级数据数据回调
     * @param {*object} tree //展开时当前节点数据
     * */
    initData(resolve, tree) {
      // 请求子级数据时不需要loading
      if (!resolve && !tree) {
        this.toggleIndex = this.toggleIndex++;
      }
      this.maskShow = true;
      if (this.fromType == 'noVisit') {
        // 未拜访
        this.deptId = tree ? tree.onlyId : null;
        this.firstProp = 'onlyName';
        this.selectNoVisitsInfoFun(resolve);
      } else if (this.fromType == 'createStore') {
        // 建店
        this.deptId = tree ? tree.onlyId : null;
        this.firstProp = 'onlyName';
        this.selectPendingCasesInfoFun(resolve);
      } else if (this.fromType == 'pantone') {
        // 色卡
        this.firstProp = 'warehouseName';
        let deptId = tree ? tree.deptId : null;
        this.getPantoneData(resolve, deptId);
      }else if (this.fromType == 'colorWarning') {
        // 调色预警
        let deptId = tree ? tree.onlyId : null;
        this.firstProp = 'onlyName';
        this.getColorWarningData(resolve, deptId);
      }
    },
    tabChange(val) {
      this.tableList = []; // 清楚tab数
      this.tabsItemPantone = {}; // 清楚色卡tab数据
      this.fromType = val;
      if (this.controller) {
        this.controller.abort();
      }
      this.controller = new AbortController(); //取消请求
      this.initData();
    },
    tabChangePantone(val) {
      this.tableList = [];
      this.materialCode = this.tabsItemPantone[val].materialCode;
      this.initData();
    },
    onClickLeft() {
      this.$router.go(-1);
    },
    linKReportHistory(row, type) {},
    // 加载子节点数据的函数
    load(tree, treeNode, resolve) {
      this.initData(resolve, tree);
    },
    // 未拜访
    selectNoVisitsInfoFun(resolve) {
      selectNoVisitsInfo({ deptId: this.deptId }, this.controller.signal).then((res) => {
        this.maskShow = false;
        if (res.code == 200) {
          res.data.forEach((val) => {
            if (val.empLevel < 3) val.hasChildren = true;
          });
          // 是否是子级
          if (resolve) {
            resolve && resolve(res.data);
          } else {
            this.tableList = res.data;
          }
        } else {
          this.Toast({
            message: res.msg,
            duration: 5000,
          });
        }
      });
    },
    // 建店
    selectPendingCasesInfoFun(resolve) {
      selectPendingCasesInfo({ deptId: this.deptId }, this.controller.signal).then((res) => {
        this.maskShow = false;
        if (res.code == 200) {
          res.data.forEach((val) => {
            // 是否还有子级
            if (val.empLevel < 3) val.hasChildren = true;
          });
          // 是否是子级
          if (resolve) {
            resolve && resolve(res.data);
          } else {
            this.tableList = res.data;
          }
        } else {
          this.Toast({
            message: res.msg,
            duration: 5000,
          });
        }
      });
    },
    // 色卡tab
    getPantoneData(resolve, deptId) {
      if (JSON.stringify(this.tabsItemPantone) != '{}') {
        this.getReportMaterialTypeFun(resolve, deptId);
      } else {
        getReportMaterial().then((res) => {
          this.tabsItemPantone = res.data;
          delete this.tabsItemPantone.tip;
          // 初次获取tab数据延迟等待this.activaPantoneName当前激活tab赋值成功，获取其物料code
          this.$nextTick(() => {
            this.materialCode = this.materialCode
              ? this.materialCode
              : this.tabsItemPantone[this.activaPantoneName].materialCode;
            this.getReportMaterialTypeFun(null, null);
          });
        });
      }
    },
    // 色卡详情
    getReportMaterialTypeFun(resolve, deptId) {
      // deptId 第一次传 null 获取当前等级下的数据（不需要当前层级）
      getReportMaterialType(
        { materialCode: this.materialCode, deptId: deptId },
        this.controller.signal
      ).then((res) => {
        this.maskShow = false;
        if (res.code == 200) {
          res.data.colorCardList.forEach((val) => {
            if (val.isChildren && val.isChildren == 'true') val.hasChildren = true;
          });
          // 是否是子级
          if (resolve) {
            resolve && resolve(res.data.colorCardList);
          } else {
            this.tableList = res.data.colorCardList;
          }
        } else {
          this.Toast({
            message: res.msg,
            duration: 5000,
          });
        }
      });
    },
    // 调色机tab
    getColorWarningData(resolve, deptId) {
      if (JSON.stringify(this.tabsItemPantone) != '{}') {
        this.getColorWarningFun(resolve, deptId);
      } else {
        getColorWarning().then((res) => {
          this.tabsItemPantone = res.data;
          delete this.tabsItemPantone.reminderContent;
          this.$nextTick(() => {
            this.getColorWarningFun(null, null);
          });
        });
      }
    },
    // 调色机详情
    getColorWarningFun(resolve, deptId) {
      // deptId 第一次传 null 获取当前等级下的数据（不需要当前层级）
      selectColorMixerStoreInfo({ deptId: deptId,storeType:this.activaPantoneName }, this.controller.signal).then((res) => {
        this.maskShow = false;
        if (res.code == 200) {
          res.data.forEach((val) => {
            // 是否还有子级
            if (val.empLevel < 3) val.hasChildren = true;
          });
          // 是否是子级
          if (resolve) {
            resolve && resolve(res.data);
          } else {
            this.tableList = res.data;
          }
        } else {
          this.Toast({
            message: res.msg,
            duration: 5000,
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.hintDetail {
  .tabBox {
    position: sticky;
    top: 0px;
    z-index: 10;
  }
}
</style>
<style lang="scss">
#app {
  width: 100%;
  height: 100%;
  .bgcolor {
    width: 100%;
    height: 100%;
    .hintDetail {
      width: 100%;
      height: 100%;
      display: flex;
      flex-direction: column;
      .content {
        flex: 1;
        overflow: hidden;
        padding-bottom: 20px;
        background: #fff;
        position: relative;
        .el-table {
          overflow: hidden;
          height: 100%;
          display: flex;
          flex-direction: column;
          .el-table__body-wrapper {
            overflow: auto;
            flex: 1;
          }
        }
        .mask {
          position: absolute;
          width: 100%;
          height: 100%;
          top: 0;
          background: rgba(255, 255, 255, 0.8);
          display: flex;
          justify-content: center;
          align-items: center;
        }
      }
    }
  }
}
.hintDetail {
  .el-table__head .el-table__body {
    overflow: hidden;
    width: 99.9% !important;
  }
  .elTreeTable table {
    width: 99.9% !important;
  }
  .el-table__body {
    width: 99.9% !important;
  }
  .el-table__empty-block {
    width: 99.9% !important;
  }
  .elTreeTable .el-table__cell:first-child .cell {
    padding-left: 2px !important;
  }

  .elTreeTable .el-table__cell {
    padding: 0px;
  }

  .elTreeTable .el-table__body td.el-table__cell {
    padding: 2px;
  }

  .elTreeTable table col {
    text-align: center;
  }

  .elTreeTable th.el-table__cell > .cell {
    padding: 1px !important;
  }

  .elTreeTable {
    font-size: 12px !important;
  }

  .elTreeTable th.el-table__cell {
    padding: 1px !important;
  }

  .elTreeTable th.el-table__cell > .cell {
    text-align: center;
  }

  .elTreeTable .el-table__header tr:first-child th.el-table__cell:first-child .cell {
    /* width: 98px; */
  }

  .elTreeTable .linkSpan {
    text-decoration: underline;
    color: #0057ba;
    display: block;
    text-align: right;
    padding: 6px 10px;
  }

  .elTreeTable td.el-table__cell div {
    padding: 0px;
  }

  .elTreeTable .cell .spanNum {
    display: block;
    text-align: right;
    padding: 6px 10px;
  }

  .elTreeTable .el-table__expand-icon {
    float: right;
    .el-icon-arrow-right {
      vertical-align: -2px;
    }
  }
}
</style>
