<template>
  <div class="app-container" style="padding: 0px !important;">
    <div class="market-top">
      <el-form
        :model="searchArray"
        ref="searchArrayRef"
        size="small"
        :inline="true"
        style="margin-top:20px;"
      >
        <el-form-item label="营销任务名称" prop="marketTaskName">
          <el-input
            v-model="searchArray.marketTaskName"
            placeholder="请输入营销任务名称"
            clearable
            style="width:100%"
          >
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="searchFn">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetFn">重置</el-button>
        </el-form-item>
      </el-form>
      <el-button 
        type="primary" 
        size="mini" 
        class="add-btn"
        @click="handleImport"
      >
        营销任务创建
      </el-button>
    </div>
    <el-table v-loading="loading" :data="tableData" :height="dyHeight" :key="tableKey">
      <template>
        <el-table-column label="序号" prop="xh" width="80">
          <template slot-scope="scope">
            <span>{{ scope.$index+1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="营销任务名称" prop="marketName" min-width="100" show-overflow-tooltip />
        <el-table-column label="所属机构" prop="deptName" min-width="100" show-overflow-tooltip />
        <el-table-column label="营销任务结束时间" prop="endTime" min-width="100" show-overflow-tooltip />
        <el-table-column label="是否需要反馈" prop="isFeedback" min-width="100" show-overflow-tooltip>
          <template slot-scope="scope">
            <span v-if="scope.row.isFeedback === '0'">否</span>
            <span v-else>是</span>
          </template>
        </el-table-column>
        <el-table-column label="完成率" prop="completeRate" min-width="100" show-overflow-tooltip />
        <el-table-column label="逾期率" prop="isOverdueRate" min-width="100" show-overflow-tooltip />
        <el-table-column label="操作" align="center" width="160">
          <template slot-scope="scope">
            <el-button size="mini" type="text" @click="editFn(scope.row)">编辑</el-button>
            <el-button size="mini" type="text" @click="lookFn(scope.row)">查看任务</el-button>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="pagination_end">
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page="pageNum"
        :page-size="pageSize"
        :page-sizes="[10, 20, 30, 50]"
        @current-change="currentChangeFn"
        :total="total"
        @size-change="sizeChangeFn"
      ></el-pagination>
    </div>
    
    <import-modal 
      ref="importModal" 
      :dialogTitle="dialogTitle"
      :perInfo="perInfo"
      @onRefreshTable="onRefreshTable"
    />
  </div>
</template>

<script>
import {
  marketTaskList
} from "@/api/task/markettask.js";
import _ from "lodash";
import dayjs from "dayjs";
import { mapGetters } from "vuex";
import ImportModal from './components/import-modal'
export default {
  name: "workmngdaytaskindex",
  components: { ImportModal },
  computed: {
    ...mapGetters(["roles"])
  },
  data() {
    return {
      searchArray: {
        marketTaskName: ""
      },
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      total: 0,
      loading: false,
      dyHeight: {},
      tableKey: false,
      dialogTitle: '营销任务创建',
      perInfo: {}
    };
  },
  created() {
    window.addEventListener("resize", () => {
      this.dyHeight = Number(window.innerHeight) - 330;
    });
    this.dyHeight = Number(window.innerHeight) - 330;
    this.resetFn();
  },
  filters: {
    formatFilter(v, type, list) {
      if (type == "status") {
        let arr = _.filter(list, { value: v });
        return arr.length > 0 ? arr[0].label : '';
      }
    }
  },
  methods: {
    resetFn() {
      this.searchArray = {
        marketTaskName: ""
      };
      this.searchFn();
    },
    searchFn() {
      this.pageNum = 1;
      this.pageSize = 10;
      this.total = 0;
      this.queryListFn();
    },
    queryListFn() {
      this.loading = true;
      let p = {
        ...this.searchArray,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
      marketTaskList(p).then(response => {
        this.loading = false;
        if (response.code == 200) {
          this.tableData = response.rows || [];
          this.total = response.total || 0;
        } else {
          this.$message.error(response.msg || "操作失败");
        }
        this.tableKey = !this.tableKey;
      });
    },
    currentChangeFn(val) {
      this.pageNum = val;
      this.queryListFn();
    },
    sizeChangeFn(val) {
      this.pageSize = val;
      this.queryListFn();
    },
    onRefreshTable() {
      this.pageNum = 1
      this.queryListFn();
    },
    handleImport() {
      this.dialogTitle = "营销任务创建";
      this.perInfo = {}
      this.$refs.importModal.onOpen()
    },
    editFn(row) {
      this.dialogTitle = "营销任务编辑";
      this.perInfo = row
      this.$refs.importModal.onOpen()
    },
    lookFn(row) {
      this.$router.push({
        path: '/workmng/markettaskdetail',
        query: {
          marketTaskId: row.id,
          isFeedback: row.isFeedback,
          backUrl: '/workmng/markettask'
        }
      })
    },
  }
};
</script>

<style lang="scss" scoped>
.market-top {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  .add-btn {
    height: 32px;
    margin-top: 20px;
  }
}
.pagination_end {
  margin-top: 20px;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: flex-end;
}
.header-radio {
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid #ebebeb;

  .el-radio-button {
    flex: 1;

    ::v-deep .el-radio-button__inner {
      width: 100%;
      border: none;
      font-weight: 400;
      letter-spacing: 0.44px;
      line-height: 25px;
      font-size: 16px;
      color: #666666;
      padding: 11px 0 12px 0;
    }

    ::v-deep .el-radio-button__orig-radio:checked + .el-radio-button__inner {
      background-color: #4886f8;
      font-weight: 400;
      color: #ffffff;
      border-bottom-left-radius: 0;
      border-bottom-right-radius: 0;
    }

    &:nth-child(2) {
      &::before,
      &::after {
        content: "";
        position: absolute;
        top: 50%;
        transform: translateY(-50%);
        height: 21px;
        width: 1px;
        background: #ebebeb;
        z-index: 1;
      }

      &::after {
        right: 1px;
      }
    }

    &.is-active {
      &::before,
      &::after {
        content: none;
      }
    }
  }
}
</style>