<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">
      <t-form>
        <t-form-item label="类目名称" name="name">
          <t-input placeholder="请输入类目名称" v-model="name"/>
        </t-form-item>
        <t-form-item label="排序" name="weigt">
          <t-input placeholder="请输入排序" v-model="weigh"/>
        </t-form-item>
      </t-form>
    </t-dialog>
  </t-card>

</template>
<script lang="ts">
export default {
  data() {
    return {
      name: '',
      weigh:0,
      addMode: false,
      isEdit: false,
      editID:0,
      list: [],
      columns: [
        {colKey: 'weigh', title: '排序'},
        {colKey: 'name', title: '类目名称'},
        {colKey: 'createtime', title: '添加时间'},
        {colKey: 'select', title: '操作', width: 200},
      ],
    }
  },
  mounted() {
    //this.member_id=this.$route.query.id;
    this.getList();
  },
  methods: {
    del(d){
      console.log(d);
      this.$request
        .post('/gory/del',{id:d.id})
        .then((res) => {
          if(res.code==1){
            this.$message.success('删除成功！');
            this.getList();
          }else {
            this.$message.error(res.msg);
          }
          console.log(res);
        })
        .catch((e) => {
          console.log(e);
        });
    },
    add(){
      this.weigh='';
      this.name= '';
      this.addMode=true;
      this.isEdit=false;
    },
    edit(d){
      this.editID=d.id;
      this.name=d.name;
      this.weigh=d.weigh;
      this.addMode=true;
      this.isEdit=true;
    },
    getList() {
      this.$request
        .post("/gory/index")
        .then((res) => {
          console.log(res);
          this.list = res.data;
        })
        .catch((e) => {
          console.log(e);
        });
    },
    onSubmit() {
      if (this.name == '') {
        this.$message.error('类目名称不能为空');
        return;
      }
      var url='/gory/add';
      if(this.isEdit){
        url='/gory/update';
      }
      this.$request
        .post(url,{name:this.name,diy_name:this.name,weigh:this.weigh,id:this.editID})
        .then((res) => {
          if(res.code==1){
            this.$message.success('操作成功！');
            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>
