<template>
  <div class="table pagePadding">
    <div
      v-if="powers && powers.length > 0 && powers.some((item) => item == '001')"
    >
      <div style="padding: 10px 0 12px 0" class="clear">
        <!-- <div
          class="fl"
          style="height: 32px; line-height: 32px; padding: 0 20px 0 0"
        >
          测试业绩达成报表(含退货)-产品确认单
        </div> -->
        <el-date-picker
          class="fl"
          style="width: 200px"
          v-model="date"
          :size="'small'"
          align="right"
          type="date"
          @change="searchList"
          :editable="false"
          :clearable="false"
          placeholder="选择日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          :picker-options="pickerOptions"
        >
        </el-date-picker>

        <el-col
          :span="4"
          style="width: 66px"
          v-if="powers.some((item) => item == '024')"
        >
          <el-button
            type="warning"
            class="fr"
            :size="searchSize"
            @click="restSearch"
          >
            重置
          </el-button>
        </el-col>
        <el-col
          :span="4"
          style="width: 66px"
          class="fr"
          v-if="powers.some((item) => item == '002')"
        >
          <el-button
            :size="searchSize"
            type="primary"
            style="float: right"
            @click="searchList"
          >
            刷新
          </el-button>
        </el-col>
      </div>
      <el-table
        :data="tableData"
        v-loading="loading"
        border
        :max-height="maxHeight"
        :size="'mini'"
        style="width: 100%"
        row-key="id"
        default-expand-all
        :row-class-name="tableRowClassName"
        :tree-props="{ children: 'child', hasChildren: 'hasChildren' }"
      >
        <el-table-column prop="p" label="业绩类型" fixed="left" width="120">
        </el-table-column>
        <el-table-column label="本日金额" width="120" align="center">
          <el-table-column show-overflow-tooltip label="销售" width="120">
            <template slot-scope="scope">
              <span>{{ scope.row.dtotal | toThousandFilter }}</span>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="退款" width="120">
            <template slot-scope="scope">
              <span>{{ scope.row.thfee | toThousandFilter }}</span>
            </template>
          </el-table-column>
        </el-table-column>

        <el-table-column label="本周金额" width="120" align="center">
          <el-table-column show-overflow-tooltip label="销售" width="120">
            <template slot-scope="scope">
              <span>{{ scope.row.wtotal | toThousandFilter }}</span>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="退款" width="120">
            <template slot-scope="scope">
              <span>{{ scope.row.wthfee | toThousandFilter }}</span>
            </template>
          </el-table-column>
        </el-table-column>

        <el-table-column label="本月金额" width="120" align="center">
          <el-table-column show-overflow-tooltip label="销售指标" width="120">
            <template slot-scope="scope">
              <span>{{ scope.row.tips | toThousandFilter }}</span>
            </template>
          </el-table-column>

          <el-table-column show-overflow-tooltip label="销售" width="120">
            <template slot-scope="scope">
              <span>{{ scope.row.total | toThousandFilter }}</span>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="销售完成率" width="95">
            <template slot-scope="scope">
              <span>{{ scope.row.rate ? scope.row.rate + "%" : "" }}</span>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="退款" width="120">
            <template slot-scope="scope">
              <span>{{ scope.row.mthfee | toThousandFilter }}</span>
            </template>
          </el-table-column>
          <el-table-column show-overflow-tooltip label="净销售" width="120">
            <template slot-scope="scope">
              <span>{{ scope.row.thtotal | toThousandFilter }}</span>
            </template>
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            label="净销售完成率"
            width="95"
          >
            <template slot-scope="scope">
              <span>{{ scope.row.thrate ? scope.row.thrate + "%" : "" }}</span>
            </template>
          </el-table-column>
        </el-table-column>
      </el-table>
    </div>
    <no-auth v-else></no-auth>
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/search/table";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
export default {
  name: "table",
  mixins: [resToken],
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "table"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      loading: true,
      date: "",
      pickerOptions: {
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
      // 表格 - 数据
      tableData: [],
    };
  },
  mounted() {
    this.restSearch();
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if ((row && row.child && row.child.length > 0) || rowIndex === 0) {
        return "warning-row";
      } else {
        return "";
      }
    },
    transformTime() {
      let time = new Date();
      let y = time.getFullYear();
      let M = time.getMonth() + 1;
      let d = time.getDate();
      return y + "-" + (M < 10 ? "0" + M : M) + "-" + (d < 10 ? "0" + d : d);
    },
    restSearch() {
      this.date = this.transformTime();
      this.searchList();
    },

    // 刷新表格
    async searchList() {
      this.loading = true;
      const res = await asyncRequest.list({
        date: this.date ? this.date : "",
      });
      if (res && res.code === 0 && res.data && res.data.length > 0) {
        this.tableData = res.data;
        this.tableData.map((v1, i1) => {
          v1.id = i1 + "";
          v1.p = v1.p ? v1.p : v1.depart;
          if (v1.child && v1.child.length > 0) {
            v1.child.map((v2, i2) => {
              v2.id = "" + v1 + i2;
              return v2;
            });
          }
          return v1;
        });
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
  },
};
</script>

<style lang="scss" scoped>
</style>
