<template>
  <div>
    <ai-list class="list">
      <ai-title
        slot="title"
        title="分类首页商品跟踪"
        isShowBottomBorder>
      </ai-title>
      <template slot="content">
        <div class="content">
          <ai-search-bar>
            <template #left>
              <el-button type="button" :class="'el-button el-button--primary'" @click="addStore()">添加分类</el-button>
            </template>
            <template #right>
              <el-button size="small" circle icon="el-icon-refresh-right" @click="getList()"></el-button>
            </template>
          </ai-search-bar>
          <ai-table
            :tableData="tableData"
            :col-configs="colConfigs"
            :total="total"
            style="margin-top: 8px;"
            @selection-change="onChooseChange"
            :current.sync="search.current" :size.sync="search.size"
            @getList="getList">
            <el-table-column slot="options" label="操作" width="240px" show-overflow-tooltip align="center" fixed="right">
              <template slot-scope="{ row }">
                <div class="table-options">
                  <el-button type="text" @click="deleteMonitor(row.id)">删除</el-button>
                  <el-button type="text" @click="renew(row.id)">续费</el-button>
                  <el-button type="text" @click="toDetail(row.categoryId)">详情</el-button>
                </div>
              </template>
            </el-table-column>
            <el-table-column slot="categoryId" label="分类" show-overflow-tooltip align="center">
              <template slot-scope="{ row }">
                <div><a class="el-link el-link--primary" :href="'https://www.temu.com' + row.categoryUrl" target="_blank">{{ row.categoryName }}</a></div>
              </template>
            </el-table-column>
          </ai-table>
        </div>
      </template>
    </ai-list>

    <ai-dialog
      title="添加分类"
      :visible.sync="isDlgShow"
      :close-on-click-modal="false"
      width="790px"
      customFooter
      @close="isDlgShow = false">
      <el-alert
        title="添加一个分类，将消耗300金币"
        type="success"
        :closable="false">
      </el-alert>
      <el-form class="ai-form" style="margin-top: 20px" :model="form" label-width="120px" ref="form">
        <el-form-item
          prop="categoryId"
          label="分类"
          :rules="[{ required: true, message: '请选择分类', trigger: 'blur' }]">
          <el-cascader style="width: 380px" v-model="form.categoryId" :props="props"></el-cascader>
        </el-form-item>
      </el-form>
      <div class="dialog-footer" slot="footer">
        <el-button @click="isDlgShow = false">取 消</el-button>
        <el-button type="primary" @click="saveStore">确定</el-button>
      </div>
    </ai-dialog>
  </div>
</template>

<script>

  export default {
    name: 'List',

    data () {
      let _this = this
      return {
        search: {
          current: 1,
          type: '1',
          size: 10
        },
        colConfigs: [
          { type: "selection", width: '70px', align: 'left' },
          { slot: 'categoryId', label: '分类', align: 'left' },
          { prop: 'status', label: '状态', align: 'left', format: v => this.$dict.getLabel('monitor_status', v), },
          { prop: 'expireTime', label: '失效时间', align: 'left' },
          { prop: 'createTime', label: '添加时间', width: '180px', fixed: 'right'}
        ],
        tableData: [],
        total: 0,

        form: {
          categoryId: ''
        },
        props: {
          value: 'optId',
          label: 'name',
          lazy: true,
          lazyLoad (node, resolve) {
            _this.$http.post('/api/category/listAllByParentId', null, {
              params: {parentId: node.value || 0}
            }).then(res => {
              let parentId = node.value || 0
              if (res.code === 0) {
                  resolve(res.data.map(v => {
                  return {
                      ...v,
                      leaf: parentId == 0 ? false: true
                  }
                  }))
              }
            })
          }
        },

        isDlgShow: false,
        selectRows: []
      }
    },

    created () {
      this.$dict.load('monitor_status');
      this.getList()
    },

    methods: {
      getList () {
        this.$http.post('/api/specialMonitor/myPage',null,{
            params: {
                ...this.search
            }
        }).then(res => {
            this.tableData = res.data.records
            this.total = res.data.total
        })
      },
      onChooseChange (val) {
        this.selectRows = val;
      },
      saveStore () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.$http.post('/api/specialMonitor/check',null, {params: {type: 1}}).then(res => {
              if (res.code == 0) {
                this.$http.post(`/api/specialMonitor/add`, {categoryId: this.form.categoryId[this.form.categoryId.length - 1]}).then(res => {
                  if (res.code == 0) {
                    this.$message.success('添加成功！')
                    this.$store.dispatch('getUserInfo')
                    this.getList()
                    this.isDlgShow = false
                  }
                })
              }
            })
          }
        })
      },
      
      batchRemove() {
        if (this.selectRows.length <= 0) {
          this.$message.error('请选择要删除的分类');
          return;
        }
        let ids = this.selectRows.map(item => {
          return item.id
        })

        this.$confirm('确定要删除？', '温馨提示', {
          type: 'warning'
        }).then(() => {
          this.$http.post('/api/specialMonitor/delByIds', ids
          ).then(res => {
            if (res.code == 0) {
              this.$message.success('删除成功！')
              this.getList()
            }
          })
        })
      },
      renew(id) {
        this.$confirm('确定要续费？', '温馨提示', {
          type: 'warning'
        }).then(() => {
          this.$http.post('/api/specialMonitor/renew',null, {
            params: {
              id: id
            }
          }
          ).then(res => {
            if (res.code == 0) {
              this.$message.success('续费成功！')
              this.$store.dispatch('getUserInfo')
              this.getList()
            }
          })
        })
      },
      deleteMonitor(id) {
        this.$confirm('确定要删除？', '温馨提示', {
          type: 'warning'
        }).then(() => {
          this.$http.post('/api/specialMonitor/del',null, {
            params: {
              id: id
            }
          }
          ).then(res => {
            if (res.code == 0) {
              this.$message.success('删除成功！')
              this.getList()
            }
          })
        })
      },
      toDetail (id) {
        this.$emit('change', {
          type: 'Detail',
          params: {
            id: id || ''
          }
        })
      },
      addStore() {
        this.form.mallId = ''
        this.isDlgShow = true
      }
    }
  }
</script>

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