<template>
  <ai-list class="template" isTabs>
    <template slot="content">
      <ai-search-bar bottomBorder>
        <template #left>
          <ai-select
            v-model="search.type"
            @change="search.current = 1, getList()"
            placeholder="项目类型"
            :selectList="$dict.getDict('questionnaireType')">
          </ai-select>
        </template>
        <template #right>
          <el-input
            v-model="search.title"
            size="small"
            placeholder="请输入模板名称或创建人"
            clearable
            @keyup.enter.native="search.current = 1, getList()"
            @clear="search.current = 1, search.title = '', getList()"
            suffix-icon="iconfont iconSearch">
          </el-input>
        </template>
      </ai-search-bar>
      <ai-search-bar style="margin-top: 12px;">
        <template #left>
          <el-button type="primary" @click="isShow = true">新建模板</el-button>
        </template>
      </ai-search-bar>
      <ai-table
        :tableData="tableData"
        :col-configs="colConfigs"
        :total="total"
        style="margin-top: 6px;"
        :current.sync="search.current"
        :size.sync="search.size"
        @getList="getList">
        <el-table-column slot="type" width="120px" label="项目类型" align="center">
          <template slot-scope="{ row }">
            <div class="tags-wrapper">
              <span class="tags" :class="'type-' + row.type">{{ dict.getLabel('questionnaireType', row.type) }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column slot="options" width="160px" fixed="right" label="操作" align="center">
          <template slot-scope="{ row }">
            <div class="table-options">
              <el-button type="text" @click="toEdit(row.id, row.type)">编辑</el-button>
              <el-button type="text" @click="quote(row.id, row.type)">引用</el-button>
              <el-button type="text" @click="remove(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </ai-table>
      <ai-dialog
        :visible.sync="isShow"
        width="800px"
        title="请选择新建模板类型"
        @onConfirm="onConfirm">
        <div class="type-list">
          <div class="type-item" @click="currIndex = 0" :class="[currIndex === 0 ? 'active' : '']">
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#iconwenjuandiaocha"></use>
            </svg>
            <span>问卷调查</span>
          </div>
          <div class="type-item" @click="currIndex = 1" :class="[currIndex === 1 ? 'active' : '']">
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#iconkaoshiceping"></use>
            </svg>
            <span>考试测评</span>
          </div>
          <div class="type-item" @click="currIndex = 2" :class="[currIndex === 2 ? 'active' : '']">
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#iconbaomingdengji"></use>
            </svg>
            <span>报名登记</span>
          </div>
          <div class="type-item" @click="currIndex = 3" :class="[currIndex === 3 ? 'active' : '']">
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#iconmanyidiaocha"></use>
            </svg>
            <span>满意调查</span>
          </div>
          <div class="type-item" @click="currIndex = 4" :class="[currIndex === 4 ? 'active' : '']">
            <svg class="icon" aria-hidden="true">
              <use xlink:href="#icontoupiaopingxuan"></use>
            </svg>
            <span>投票评选</span>
          </div>
        </div>
      </ai-dialog>
    </template>
  </ai-list>
</template>

<script>
  export default {
    name: 'Template',

    props: {
      instance: Function,
      dict: Object
    },

    data () {
      return {
        search: {
          current: 1,
          status: '',
          type: '',
          size: 10,
          templateType: 1,
          title: ''
        },
        currIndex: 0,
        isShow: false,
        total: 10,
        colConfigs: [
          { prop: 'title',  label: '模板名称', align: 'left' },
          { slot: 'type', label: '项目类型', align: 'center' },
          { prop: 'quoteCount',  label: '引用次数', align: 'center' },
          { prop: 'createUserName', label: '创建人', align: 'center', openType: 'userName' },
          { prop: 'createUnitName', label: '创建单位', align: 'center', openType: 'departmentName' },
          { prop: 'createTime', label: '创建时间', align: 'center' }
        ],
        tableData: []
      }
    },

    mounted () {
      this.getList()
    },

    methods: {
      getList () {
        this.instance.post(`/app/appquestionnairetemplate/list`, null, {
          params: {
            ...this.search
          }
        }).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.records
            this.total = res.data.total
            this.$initWxOpenData()
          }
        })
      },

      quote (id, type) {
        this.$emit('change', {
          type: 'add',
          params: {
            id,
            type,
            isQuote: true,
            templateType: 0
          }
        })
      },

      remove (id) {
        this.$confirm('确定删除该数据？').then(() => {
          this.instance.post(`/app/appquestionnairetemplate/deleteShareTemplate?ids=${id}`).then(res => {
            if (res.code == 0) {
              this.$message.success('删除成功！')
              this.getList()
            }
          })
        })
      },

      toAdd (id) {
        this.$emit('change', {
          type: 'Add',
          params: {
            id
          }
        })
      },

      toEdit (id, type) {
        this.$emit('change', {
          type: 'add',
          params: {
            id,
            type,
            templateType: 1
          }
        })
      },

      onConfirm () {
        this.$emit('change', {
          type: 'add',
          params: {
            id: '',
            templateType: 1,
            type: this.currIndex
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .template {
    .tags-wrapper {
      display: flex;
      justify-content: center;
    }

    .type-list {
      display: flex;
      align-items: center;

      .type-item {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 128px;
        height: 64px;
        margin-right: 20px;
        background: #FFFFFF;
        border-radius: 2px;
        // box-shadow: 0px 4px 8px 0px rgba(0, 0, 0, 0.08);
        border-radius: 2px;
        cursor: pointer;
        border: 1px solid #E4E8EF;

        svg {
          width: 24px;
          height: 24px;
          margin-right: 8px;
        }

        &.active {
          border: 1px solid #2266FF;
        }

        &:last-child {
          margin-right: 0;
        }
      }
    }

    .tags {
      display: block;
      width: 64px;
      height: 24px;
      line-height: 24px;
      text-align: center;
      border-radius: 4px;
      font-size: 12px;
    }

    .type-0 {
      color: #2266FF;
      background: rgba(34, 102, 255, 0.1);
    }

    .type-1 {
      color: rgba(34, 170, 153, 1);
      background: rgba(34, 170, 153, 0.1);
    }

    .type-2 {
      color: rgba(248, 180, 37, 1);
      background: rgba(248, 180, 37, 0.1);
    }

    .type-3 {
      color: rgba(102, 119, 187, 1);
      background: rgba(102, 119, 187, 0.1);
    }

    .type-4 {
      color: rgba(236, 68, 97, 1);
      background: rgba(236, 68, 97, 0.1);
    }
  }
</style>
