<template>
  <div class="page-body">
    <div class="page-search row items-center">
      <div class="col row wrap q-col-gutter-md">
        <div class="col-3">
          <q-input @input="resetSearch" clearable filled v-model="msg.TaskName" label="任务名称" />
        </div>
      </div>
    </div>
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat
        class="sticky-right-column-table sticky-column-table" separator="none" :data="dataList" :columns="columns"
        row-key="name">
        <template v-slot:top="props">
          <div class="col-2 q-table__title">营销任务</div>
          <q-space />
          <div class="page-option">
            <q-btn color="accent" size="sm" icon="add" label="新增任务" @click="EditTask(null)" />
          </div>
        </template>
        <template v-slot:body-cell-TaskStartTimeStr="props">
          <q-td>
            <div>
              {{props.row.TaskStartTimeStr}} - {{props.row.TaskEndTimeStr}}
            </div>
          </q-td>
        </template>
        <template v-slot:body-cell-TaskFinishType="props">
          <q-td :props="props" style="padding-right: 0px">
            {{props.row.TaskFinishType==1?"人数":"销售金额"}}
          </q-td>
        </template>
        <template v-slot:body-cell-TaskType="props">
          <q-td :props="props" style="padding-right: 0px">
            <div v-for="(x,index) in props.value" :key="index">
              <div class="border-bottom">
                {{ x.TaskTypeName }}
              </div>
            </div>
          </q-td>
        </template>
        <template v-slot:body-cell-TaskConditions="props">
          <q-td :props="props" style="padding-right: 0px">
            <div v-for="(x,index) in props.value" :key="index" class="border-bottom">
              <template v-if="x.TargetList">
                <template v-if="x.TargetList.length==1">
                  {{x.TargetList[0].TargetName}}
                </template>
                <template v-if="x.TargetList.length>1">
                  {{x.TargetList[0].TargetName}}
                  <span class="otherTask">
                    {{x.TargetList.length}}
                    <q-popup-proxy>
                      <q-banner>
                        <div style="width:100%;max-height:400px;overflow:auto;">
                          <table class="saleTaskTable" style="border-collapse:collapse;">
                            <tr>
                              <th>任务条件</th>
                            </tr>
                            <tr v-for="(sItem,sIndex) in x.TargetList" :key="sIndex"
                              style="border-bottom:1px dashed #d1d1d1;">
                              <td>{{sItem.TargetName}}</td>
                            </tr>
                          </table>
                        </div>
                      </q-banner>
                    </q-popup-proxy>
                  </span>
                </template>
                <template v-if="x.TargetList.length==0">
                  无
                </template>
              </template>
            </div>
          </q-td>
        </template>

        <template v-slot:body-cell-CompleteValue="props">
          <q-td :props="props" style="padding-right: 0px">
            <div v-for="(x,index) in props.value" :key="index">
              <div class="border-bottom">
                {{ x.TaskFinishValue }}
              </div>
            </div>
          </q-td>
        </template>
        <template v-slot:body-cell-optioned="props">
          <q-td>
            <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
              @click="EditTask(props.row)" />
            <q-btn flat size="xs" icon="delete" color="negative" style="font-weight:400" label="删除"
              @click="RemoveTask(props.row.Id)" />
            <q-btn flat size="xs" icon="iconfont icon-view" style="font-weight:400" label="详情"
              @click="iShowEdit(props.row)" />
          </q-td>
        </template>
        <template v-slot:bottom>
          <q-pagination class="full-width justify-end" v-model="msg.pageIndex" color="primary" :max="pageCount"
            :input="true" @input="changePage" />
        </template>
      </q-table>
    </div>
    <saleForm v-if="isShowTask" :taskOption="taskOption" @close="closeSaleForm" @success="RefreshPage"></saleForm>
    <saleRightForm v-if="isShowRight" :taskOption="taskOption" @close="closeSaleForm"></saleRightForm>
  </div>
</template>
<script>
  import {
    GetCustomerTaskPage,
    RemoveCustomerTask
  } from '../../api/sale/peemanagement';
  import saleForm from '../../components/sale/sale-form';
  import saleRightForm from '../../components/sale/saleRight-form'
  export default {
    meta: {
      title: "营销任务"
    },
    components: {
      saleForm,
      saleRightForm
    },
    data() {
      return {
        columns: [{
            name: 'TaskName',
            label: '任务名称',
            align: 'left',
            field: 'TaskName'
          },
          {
            name: 'TaskStartTimeStr',
            label: '任务时间',
            field: 'TaskStartTimeStr',
            align: 'left',
          },
          {
            name: 'TaskFinishType',
            label: '任务完成类型',
            align: "left",
            field: 'TaskFinishType',
          },
          {
            name: 'TaskType',
            label: '任务类型',
            required: true,
            align: "left",
            field: (row) => row.DetailsList
          },
          {
            name: 'TaskConditions',
            label: '任务条件',
            required: true,
            align: "left",
            field: (row) => row.DetailsList
          },
          {
            name: 'CompleteValue',
            label: '完成值',
            required: true,
            align: "left",
            field: (row) => row.DetailsList
          },
          {
            name: 'ExchangeEndTimeStr',
            label: '兑换截止日期',
            field: 'ExchangeEndTimeStr',
            align: 'left',
          },
          {
            name: 'CreateByName',
            label: '创建人',
            field: 'CreateByName',
            align: 'left',
          },
          {
            name: 'optioned',
            label: '操作',
            field: 'Id'
          }
        ],
        dataList: [],
        loading: true,
        msg: {
          pageIndex: 1,
          pageSize: 12,
          rowsPerPage: 12,
          TaskName: "" //任务
        },
        pageCount: 0,
        taskOption: null,
        isShowTask: false,
        isShowRight: false
      }
    },
    created() {},
    mounted() {
      this.getList()
    },
    methods: {
      resetSearch() {
        this.loading = true;
        this.msg.pageIndex = 1;
        this.getList();
      },
      //删除
      RemoveTask(id) {
        let that = this
        this.$q.dialog({
          title: "删除营销任务",
          message: "你正在进行删除任务行为，一旦执行无法找回，是否确认执行？",
          persistent: true,
          cancel: {
            label: "取消",
            flat: true
          },
          ok: {
            label: "确认",
            flat: true,
            focus: true
          }
        }).onOk(() => {
          RemoveCustomerTask({
            Id: id
          }).then(res => {
            this.$q.notify({
              icon: 'iconfont icon-chenggong',
              color: 'accent',
              timeout: 2000,
              message: '数据删除成功！',
              position: 'top'
            })
            this.getList()
          })
        });
      },
      changePage(val) {
        this.msg.pageIndex = val;
        this.loading = true;
        this.getList();
      },
      RefreshPage() {
        this.getList()
      },
      //新增修改
      EditTask(obj) {
        if (obj) {
          this.taskOption = obj;
        } else {
          this.taskOption = null
        }
        this.isShowTask = true
      },
      iShowEdit(obj) {
        if (obj) {
          this.taskOption = obj;
        } else {
          this.taskOption = null
        }
        this.isShowRight = true
      },
      closeSaleForm() {
        this.isShowTask = false;
        this.isShowRight = false;
      },
      //获取数据
      getList() {
        this.loading = true;
        GetCustomerTaskPage(this.msg).then(res => {
          this.loading = false;
          if (res.Code == 1) {
            this.dataList = res.Data.PageData;
            this.pageCount = res.Data.PageCount;
          }
        }).catch(() => {
          this.loading = false
        })
      }
    },
  }
</script>

<style lang="sass">
  @import url('~assets/css/table.sass')
</style>
<style lang="scss">
  .otherTask {
    display: inline-block;
    width: 22px;
    height: 22px;
    text-align: center;
    border: 1px solid #2961FE;
    border-radius: 50%;
    cursor: pointer;
    color: #2961FE;
  }

  .saleTaskTable {
    width: 400px;
    text-align: center;
  }

  .saleTaskTable tr td {
    height: 40px;
  }

  .saleTaskTable tr th {
    height: 40px;
    background-color: rgb(238, 238, 239);
  }

  .border-bottom {
    border-bottom: 1px dashed #eee;
    padding-bottom: 5px;
    margin-bottom: 5px;
  }
</style>