<template>
  <t-card :bordered="false">
    <div class="form-step-container">
      <t-button @click="add">新增置顶</t-button>
      <t-table
        rowKey="index"
        :data="list"
        :columns="columns"
        :stripe="false"
        :bordered="false"
        :hover="true"
        size="large"
        table-layout="auto"
        cellEmptyContent="-"
      >
        <template #select="{ row }">
          <t-space size="24px" v-if="row.id!=0">
            <t-button theme="warning" @click="edit(row)">编辑</t-button>
            <t-popconfirm  content="确认删除吗？" @confirm="del(row)">
              <t-button theme="danger" >删除</t-button>
            </t-popconfirm>
          </t-space>
        </template>
      </t-table>
    </div>
    <t-dialog header="新增首页置顶" :visible="addMode" :onClose="onCloseMy" @confirm="onSubmit"  width="650px">
      <t-form>
        <t-form-item label="活动名称" name="password">
          <t-select
            v-model="activity_id"
            :filterable="true"
            placeholder="搜索活动名称"
            style="display: inline-block"
          >
            <t-option v-for="(item,index) in options" :title="item.activity_name" :value="item.id" :key="index" :label="item.activity_name"></t-option>
          </t-select>
        </t-form-item>
        <t-form-item label="排序" name="weigh">
          <t-input placeholder="请输入排序" v-model="weigh"/>
        </t-form-item>
      </t-form>
    </t-dialog>
  </t-card>

</template>
<script lang="ts">
export default {
  data() {
    return {
      weigh:0,
      activity_id: '',
      addMode: false,
      isEdit: false,
      editID:0,
      list: [],
      options:[],
      columns: [
        {colKey: 'weigh', title: '排序'},
        {colKey: 'activity_name', title: '活动名称'},
        {colKey: 'select', title: '操作', width: 200},
      ],
    }
  },
  mounted() {
    //this.member_id=this.$route.query.id;
    this.getList();
    this.getOptionsList();

  },
  methods: {
    getOptionsList() {
      this.$request
        .post("/activity", {page: 1, size: 10000})
        .then((res) => {
          console.log(res);
          if (res.code == 1) {
            this.options = res.data.ret;
          }
        })
        .catch((e) => {
          console.log(e);
        });
    },
    del(d){
      console.log(d);
      this.$request
        .post('/activity_index/del',{id:d.id})
        .then((res) => {
          if(res.code==1){
            this.$message.success(res.msg);
            this.getList();
          }else {
            this.$message.error(res.msg);
          }
          console.log(res);
        })
        .catch((e) => {
          console.log(e);
        });
    },
    add(){
      this.activity_id='';
      this.weigh= 0;
      this.addMode=true;
      this.isEdit=false;
      this.editID=0;
    },
    edit(d){
      console.log(d);
      this.editID=d.id;
      this.activity_id=d.activity_id;
      this.weigh=d.weigh;
      this.addMode=true;
      this.isEdit=true;
    },
    getList() {
      this.$request
        .post("/activity_index/index")
        .then((res) => {
          console.log(res);
          this.list = res.data.ret;
        })
        .catch((e) => {
          console.log(e);
        });
    },
    onSubmit() {
      if (this.activity_id == '') {
        this.$message.error('请选择活动');
        return;
      }
      var url='/activity_index/add';
      if(this.isEdit){
        url='/activity_index/update';
      }
      this.$request
        .post(url,{activity_id:this.activity_id,weigh:this.weigh,id:this.editID})
        .then((res) => {
          if(res.code==1){
            this.$message.success(res.msg);
            this.addMode=false;
            this.getList();
          }else {
            this.$message.error(res.msg);
          }
          console.log(res);
        })
        .catch((e) => {
          console.log(e);
        });
    },
    onCloseMy() {
      this.addMode = false;
    }
  }
}
</script>
<style scoped lang="less">

</style>
