<template>
  <div class="daily-set-wrap ">
    <common-detail-top :topMenuData="topMenuData" :activeId="activeId"></common-detail-top>
    <div class="daily-set-content boder-box">
      <div class="table-condition flex flex-space-between">
        <div class="table-condition-left">
          <el-input placeholder="请输入内容" v-model="conditionObj.searchInput" class="w-264" style="width: 264px;">
            <i slot="prefix" class="el-input__icon el-icon-search"></i>
          </el-input>
          <div class=" w-380 flex-column item-cell-select inline-block">
            <div class="depart-item-wrap">
              <div style="width:213px" class="el-select el-select--large depart-item-content" @click="showDialogLayer('store', conditionObj.storeGroup)">
                <div class="el-select__tags" style="max-width: 181px;">
                  <span>
                    <template v-for="(item, index) in conditionObj.storeGroup">
                      <span class="el-tag el-tag--info el-tag--small" :key="index">
                        <span class="el-select__tags-text">{{ item.name || item.storeName }}</span
                        ><i class="el-tag__close el-icon-close" @click.stop="delDepart(index, conditionObj.storeGroup)"></i>
                      </span>
                    </template>
                  </span>
                </div>
              </div>
            </div>
          </div>
          <el-select v-model="conditionObj.storeType" placeholder="请选择">
            <el-option v-for="item in storeTypeOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option>
          </el-select>
          <el-date-picker v-model="conditionObj.dateRange" type="daterange" align="right" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions2"> </el-date-picker>
        </div>
        <div class="table-condition-right">
          <el-button type="info" @click="multDel">批量删除</el-button>
          <el-button type="primary" @click="exportExcel">导出</el-button>
        </div>
      </div>
      <el-table class="select-table" ref="multipleTable" :data="tableData" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" :filters="[{ text: '选择当页', value: '1' }, { text: '选择全部', value: '2' }]" filter-placement="bottom-start"> </el-table-column>
        <el-table-column label="门店名称" width="120">
          <template slot-scope="scope">{{ scope.row.storeName }}</template>
        </el-table-column>
        <el-table-column prop="" label="任务总数">
          <template slot-scope="scope">{{ scope.row.totalTaskNum }}</template>
        </el-table-column>
        <el-table-column prop="" label="已完成" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.completed }}</template>
        </el-table-column>
        <el-table-column prop="" label="完成率" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.completionRate }}</template>
        </el-table-column>
        <el-table-column prop="" label="待完成" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.unCompleted }}</template>
        </el-table-column>
        <el-table-column prop="" label="逾期中" show-overflow-tooltip>
          <template slot-scope="scope">{{ scope.row.overdue }}</template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="toDetail(scope.$index, scope.row)">查看详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="block common-wrap__page text-right" v-if="tableData.length != 0">
        <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
      </div>
    </div>
    <vue-select-store ref="storeSelector" :appScene="appScene" :treeSet="storeTreeSet" :selectType="'all'" :defaultList="defaultStoreList" @handleSelectedList="handleSelectedList"> </vue-select-store>
  </div>
</template>
<script>
// import navCrumb from '@/components/nav/nav.vue';
import appDetail from '@/components/app/app-detail.vue';
import vueSelectStore from 'components/common/vueSelectStore';
import commonDetailTop from '@/components/app/common-detail-top.vue';
import { getRequest, postRequest } from '@/api/api';
import errMsg from '@/common/js/error';
export default {
  name: 'reviewed',
  props: {
    brandId: {
      type: String,
      default() {
        return '';
      }
    }
  },
  data() {
    return {
      activeTab: '1',
      activeBrand: this.brandId, // 商户(品牌) id
      topMenuData: [
        {
          id: '1',
          name: '云日报记录'
        }
      ],
      activeId: '1',
      // 条件
      conditionObj: {
        searchInput: '',
        storeGroup: [],
        storeType: '',
        dateRange: []
      },
      storeTypeOptions: [
        {
          label: '直营',
          value: '1'
        },
        {
          label: '联营',
          value: '2'
        }
      ],

      tableData: [
        {
          storeName: '2016-05-03',
          totalTaskNum: '王小虎',
          completed: '10',
          completionRate: '10',
          unCompleted: '10',
          overdue: '1',
          taskId: '123',
          storeId: '123456',
          address: '上海市普陀区金沙江路 1518 弄'
        }
      ],
      multipleSelection: [],
      // 分页参数
      currentPage: 1,
      pageSize: 20,
      total: 0,

      // 门店选择
      appScene: 2,
      defaultStoreList: [],
      storeTreeSet: {
        isSelectPerson: false, // 控制只能选部门(false)
        dialogVisible: false, // 控制显示/隐藏
        isSingle: false // 是否单选
      }
    };
  },
  computed: {},
  methods: {
    /**
     * 返回 的 brandId
     */

    setSelectTab(item) {
      let that = this;
      that.activeTab = item.tabId;
    },
    /**
     * 表格选择
     */
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },

    /**
     * 删除选的部门
     */
    delDepart(index, item) {
      item.splice(index, 1);
    },

    /**
     * 批量删除
     */
    multDel() {
      let that = this;
      if (!that.multipleSelection.length) {
        that.$message.error({
          duration: 1000,
          message: '请选择删除项'
        });
        return false;
      }
      that.dialogRadio = true;
    },

    /**
     * 导出
     */
    exportExcel() {
      let that = this;
      let para = {
        brandId: that.activeBrand
      };
      postRequest('/haoban-manage-web/daily/page-task', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },

    /**
     * 分页---页码变化
     */
    handleSizeChange(val) {
      const that = this;
      that.pageSize = val;
      that.getTableList();
    },

    /**
     * 分页---当前页变化
     */
    handleCurrentChange(val) {
      const that = this;
      that.currentPage = val;
      // let fullPath = that.$route.fullPath;
      that.getTableList();
    },

    /**
     * 弹窗显示事件
     */
    showDialogLayer(type, selData) {
      const that = this;
      that.selectType = type;
      that.changed = type;
      that.defaultStoreList = selData;
      that.storeTreeSet = {
        dialogVisible: true,
        isSingle: false,
        isSelectPerson: true
      };
    },

    /**
     * 处理已选部门
     */
    handleSelectedList(group) {
      const that = this;

      that.conditionObj.storeGroup = group;
    },

    /**
     * 查看详情
     */
    toDetail(index, row) {
      let that = this;
      that.$router.push(`dailyDetail?taskId=${row.taskId}`);
    },

    /**
     * 获取列表数据
     */
    getTableList(val) {
      const that = this;
      let para = {
        search: that.conditionObj.searchInput || '', // 搜索字段
        storeGroupIds: that.conditionObj.storeGroup || '', // 门店分组,数组
        storeType: that.conditionObj.storeType || '', //门店类型
        startDate: that.conditionObj.dateRange[0] || '',
        endDate: that.conditionObj.dateRange[1] || '',
        currentPage: that.currentPage, // 当前页
        pageSize: that.pageSize, // 一页显示个数
        brandId: that.activeBrand
      };
      getRequest('/haoban-manage-web/daily/page-task', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            if (!!resData.result && !!resData.result.result.legnth) {
              resData.result.result.forEach(function(ele, index) {
                if (!!ele.createTime) {
                  return false;
                }
              });
            }
            that.tableData = resData.result.result;
            that.total = resData.result.totalCount;
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    }
  },
  watch: {
    brandId: function(newData, oldData) {
      const that = this;
      that.activeBrand = newData;
    }
  },
  mounted() {
    document.documentElement.style.backgroundColor = '#f0f2f5';
    this.activeBrand = this.brandId;
  },
  destroyed() {
    document.documentElement.style.backgroundColor = '#fff';
  },
  components: {
    appDetail,
    commonDetailTop,
    vueSelectStore
  }
};
</script>
<style type="text/scss" lang="scss" scoped>
.daily-set-wrap {
  .daily-set-content {
    padding: 16px;
  }

  .depart-item-content {
    width: 213px;
    height: 32px;
    overflow: hidden;
    white-space: nowrap;
    border-radius: 4px;
    border: 1px solid #dcdfe6;
    cursor: pointer;
  }

  .item-cell-select {
    /deep/ .el-select__tags {
      white-space: nowrap;
      overflow: hidden;
    }
  }

  .depart-cell {
    position: relative;
    margin: 0 24px 24px 0;

    .el-icon-circle-close {
      position: absolute;
      top: -10px;
      right: -10px;
      font-size: 16px;
      color: #808995;
      cursor: pointer;
    }
  }
}
</style>
