<template>
  <div class="ele-body">
    <a-card :bordered="false">
      <!-- 搜索表单 -->
      <a-form
        :model="where"
        :label-col="{md: {span: 6}, sm: {span: 24}}"
        :wrapper-col="{md: {span: 18}, sm: {span: 24}}">
        <a-row>


          <!-- 测试名称 -->
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="测试名称:">
              <a-input
                v-model:value.trim="where.name"
                placeholder="请输入测试名称"
                allow-clear/>
            </a-form-item>
          </a-col>


          <!-- 状态：1正常 2停用 -->
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="状态:">
              <a-select
                v-model:value="where.status"
                placeholder="请选择状态"
                allow-clear>

                <a-select-option :value="1">正常</a-select-option>

                <a-select-option :value="2">停用</a-select-option>

              </a-select>
            </a-form-item>
          </a-col>


          <!-- 类型：1京东 2淘宝 3拼多多 4唯品会 -->
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="类型:">
              <a-select
                v-model:value="where.type"
                placeholder="请选择类型"
                allow-clear>

                <a-select-option :value="1">京东</a-select-option>

                <a-select-option :value="2">淘宝</a-select-option>

                <a-select-option :value="3">拼多多</a-select-option>

                <a-select-option :value="4">唯品会</a-select-option>

              </a-select>
            </a-form-item>
          </a-col>


          <!-- 是否VIP：1是 2否 -->
          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item label="是否VIP:">
              <a-select
                v-model:value="where.is_vip"
                placeholder="请选择是否VIP"
                allow-clear>

                <a-select-option :value="1">是</a-select-option>

                <a-select-option :value="2">否</a-select-option>

              </a-select>
            </a-form-item>
          </a-col>


          <a-col :lg="6" :md="12" :sm="24" :xs="24">
            <a-form-item style="padding-left:10px;" :wrapper-col="{span: 24}">
              <a-space>
                <a-button type="primary" @click="reload">查询</a-button>
                <a-button @click="reset">重置</a-button>
              </a-space>
            </a-form-item>
          </a-col>
        </a-row>
      </a-form>
      <!-- 表格 -->
      <ele-pro-table
        ref="table"
        row-key="id"
        :datasource="url"
        :columns="columns"
        :where="where"
        v-model:selection="selection"
        :scroll="{x: 'max-content'}">
        <template #toolbar>
          <a-space>
            <a-button type="primary" @click="openEdit()">
              <template #icon>
                <plus-outlined/>
              </template>
              <span>新建</span>
            </a-button>
            <a-button type="primary" danger @click="removeBatch">
              <template #icon>
                <delete-outlined/>
              </template>
              <span>删除</span>
            </a-button>
          </a-space>
        </template>


        <!-- 头像列 -->
        <template #avatar="{ record }">
          <a-image :width="35" :src="record.avatar"/>
        </template>


        <!-- 状态列 -->
        <template #status="{ text, record }">
          <a-switch
            :checked="text===1"
            @change="(checked) => editStatus(checked, record)"/>
        </template>


        <!-- 类型列 -->
        <template #type="{ record }">
          <a-tag
            :color="['blue', 'pink', 'orange', 'red', 'blue', 'pink', 'pink', 'purple', 'purple'][record.type-1]">
            {{ ['京东', '淘宝', '拼多多', '唯品会'][record.type - 1] }}
          </a-tag>
        </template>


        <!-- 是否VIP列 -->
        <template #isVip="{ text, record }">
          <a-switch
            :checked="text===1"
            @change="(checked) => editIsVip(checked, record)"/>
        </template>


        <!-- 操作列 -->
        <template #action="{ record }">
          <a-space>
            <a @click="openEdit(record)">修改</a>
            <a-divider type="vertical"/>
            <a-popconfirm
              title="确定要删除此演示一吗？"
              @confirm="remove(record)">
              <a class="ele-text-danger">删除</a>
            </a-popconfirm>
          </a-space>
        </template>
      </ele-pro-table>
    </a-card>
  </div>
  <!-- 编辑弹窗 -->
  <example-edit
    v-model:visible="showEdit"
    :data="current"
    @done="reload"/>
</template>

<script>
import {createVNode} from 'vue'
import {DeleteOutlined, ExclamationCircleOutlined, PlusOutlined} from '@ant-design/icons-vue';
import ExampleEdit from './example-edit';

export default {
  name: 'ExamExample',
  components: {
    PlusOutlined,
    DeleteOutlined,
    ExampleEdit
  },
  data() {
    return {
      // 表格数据接口
      url: '/example/list',
      // 表格列配置
      columns: [
        {
          key: 'index',
          title: '编号',
          width: 48,
          align: 'center',
          customRender: ({index}) => this.$refs.table.tableIndex + index
        },


        {
          title: '测试名称',
          dataIndex: 'name',
          align: 'center',
          width: 100,
        },


        {
          title: '头像',
          dataIndex: 'avatar',
          align: 'center',
          width: 100,
          slots: {customRender: 'avatar'}
        },


        {
          title: '内容',
          dataIndex: 'content',
          align: 'center',
          width: 100,
        },


        {
          title: '状态',
          dataIndex: 'status',
          width: 100,
          align: 'center',
          slots: {customRender: 'status'}
        },


        {
          title: '类型',
          dataIndex: 'type',
          width: 100,
          align: 'center',
          slots: {customRender: 'type'}
        },


        {
          title: '是否VIP',
          dataIndex: 'isVip',
          width: 100,
          align: 'center',
          slots: {customRender: 'isVip'}
        },


        {
          title: '排序号',
          dataIndex: 'sort',
          align: 'center',
          width: 100,
        },


        {
          title: '创建时间',
          dataIndex: 'createTime',
          align: 'center',
          customRender: ({text}) => this.$util.toDateString(text)
        },
        {
          title: '更新时间',
          dataIndex: 'updateTime',
          align: 'center',
          customRender: ({text}) => this.$util.toDateString(text)
        },
        {
          title: '操作',
          key: 'action',
          width: 150,
          align: 'center',
          fixed: 'right',
          slots: {customRender: 'action'}
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
    };
  },
  methods: {
    /* 搜索 */
    reload() {
      this.selection = [];
      this.$refs.table.reload({page: 1, where: this.where});
    },
    /*  重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /* 删除单个 */
    remove(row) {
      const hide = this.$message.loading('请求中..', 0);
      this.$http.delete('/example/delete/' + [row.id]).then(res => {
        hide();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        hide();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm({
        title: '提示',
        content: '确定要删除选中的演示一吗?',
        icon: createVNode(ExclamationCircleOutlined),
        maskClosable: true,
        onOk: () => {
          const hide = this.$message.loading('请求中..', 0);
          this.$http.delete('/example/delete', {
            data: this.selection.map(d => d.id)
          }).then(res => {
            hide();
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              this.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            hide();
            this.$message.error(e.message);
          });
        }
      });
    },
    /* 打开编辑弹窗 */
    openEdit(row) {
      this.current = row;
      this.showEdit = true;
    },


    /* 修改状态 */
    editStatus(checked, row) {
      let params = Object.assign({}, {
        id: row.id,
        status: checked ? 1 : 2,
      });
      this.$http.put('/example/status', params).then(res => {
        if (res.data.code === 0) {
          row.status = checked ? 1 : 2;
          this.$message.success(res.data.msg);
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },


    /* 修改是否VIP */
    editIsVip(checked, row) {
      let params = Object.assign({}, {
        id: row.id,
        is_vip: checked ? 1 : 2,
      });
      this.$http.put('/example/isVip', params).then(res => {
        if (res.data.code === 0) {
          row.isVip = checked ? 1 : 2;
          this.$message.success(res.data.msg);
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        this.$message.error(e.message);
      });
    },


  }
}
</script>

<style scoped>
</style>
