<template>
  <div>
    <div class="app-container">
      <el-form :model="formValidate" ref="formValidate" size="small" :inline="true" label-width="68px">
        <el-form-item>
          <el-select v-model="formValidate.status" placeholder="是否显示：" clearable @change="userSearchs" class="form_content_width">
            <el-option value="1" label="显示"></el-option>
            <el-option value="0" label="不显示"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label-for="status2">
          <el-input clearable placeholder="请输入分类名称" v-model="formValidate.title" class="form_content_width" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="userSearchs">查询分类</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="el-icon-plus" @click="classAdd">新增</el-button>
        </el-col>
      </el-row>
      <el-table border highlight-current-row :loading="loading" :tree-props="{children: 'children', hasChildren: 'hasChildren'}" :data="classList"
        row-key="id">

        <el-table-column prop="title" label="分类名称" min-width="150"></el-table-column>
        <el-table-column prop="id" label="分类ID" min-width="150"></el-table-column>
        <el-table-column prop="engTitle" label="分类字段" min-width="150"></el-table-column>
        <el-table-column prop="status" label="状态" min-width="150">
          <template v-slot="{ row }">
            <el-switch :active-value="1" :inactive-value="0" v-model="row.status" :value="row.status" @change="onchangeIsShow(row)" size="large">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="action" label="操作" width="220" fixed="right">
          <template v-slot="{ row }">
            <el-button type="text" icon="el-icon-edit" @click="goList(row)">配置列表</el-button>
            <el-button type="text" icon="el-icon-edit" @click="handleUpdate(row)">编辑</el-button>
            <el-button type="text" icon="el-icon-delete" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="acea-row row-right page">
        <pagination v-if="total" :total="total" :page.sync="formValidate.pageNum" :limit.sync="formValidate.pageSize" @pagination="getList" />
      </div>
    </div>
    <!-- 新建  编辑表单-->
    <!-- 添加或修改配置分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-row :gutter="15" style="margin-bottom: 80px">
        <el-form ref="form" :model="form" label-width="100px">
          <el-col :span="24">
            <el-form-item label="上级分类id" prop="pid">
              <el-input v-model="form.pid" placeholder="请输入上级分类id" />

            </el-form-item>
            <el-form-item label="分类名称" prop="title">
              <el-input v-model="form.title" placeholder="请输入配置分类名称" maxlength="255" show-word-limit />

            </el-form-item>
            <el-form-item label="英文名称" prop="engTitle">
              <el-input v-model="form.engTitle" placeholder="请输入配置分类英文名称" maxlength="255" show-word-limit />
            </el-form-item>
            <el-form-item label="排序" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排序" />

            </el-form-item>
            <el-form-item label="状态" >
              <template>
                <el-radio-group v-model="form.status">
                  <el-radio-button label="1">显示</el-radio-button>
                  <el-radio-button label="0">不显示</el-radio-button>
                </el-radio-group>
              </template>
            </el-form-item>
          </el-col>
        </el-form>
      </el-row>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" style="margin-left: 5%">提交</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import { classListApi, classAddApi, classEditApi, setStatusApi } from "@/api/system/setting";
import { listConfigTab, getConfigTab, delConfigTab, addConfigTab, updateConfigTab, updateConfigTabStatus } from "@/api/system/configTab";
export default {
  name: 'configTab',
  data() {
    return {
      tabconfig: { children: 'children', reserve: true, accordion: true },
      grid: {
        xl: 7,
        lg: 7,
        md: 12,
        sm: 24,
        xs: 24,
      },
      // 表单参数
      form: {},
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      loading: false,
      formValidate: {
        status: '',
        pageNum: 1,
        pageSize: 10,
        title: '',
      },
      total: 0,
      FromData: null,
      classId: 0,
      classList: [],
      config_name: ''
    };
  },
  computed: {
    ...mapState('media', ['isMobile']),
    labelWidth() {
      return this.isMobile ? undefined : '80px';
    },
    labelPosition() {
      return this.isMobile ? 'top' : 'right';
    },
  },
  mounted() {
    this.getList();
  },
  methods: {
    // 跳转到配置列表页面
    goList(row) {
      this.$router.push({
        path: '/tool/configList?tabId=' + row.id,
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    // 添加配置分类
    classAdd() {
      this.open = true;
      this.title = "添加配置分类";
    },
    // 编辑
    edit(row) {
      classEditApi(row.id)
        .then(async (res) => {
          if (res.data.status === false) {
            return this.$authLapse(res.data);
          }
          this.FromData = res.data;
          this.$refs.edits.modals = true;
        })
        .catch((res) => {
          this.$message.error(res.msg);
        });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getConfigTab(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改配置分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConfigTab(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addConfigTab(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除配置分类编号为"' + ids + '"的数据项？').then(function () {
        return delConfigTab(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    // 删除
    del(row, tit, num) {
      let delfromData = {
        title: tit,
        num: num,
        url: `setting/config_class/${row.id}`,
        method: 'DELETE',
        ids: '',
      };
      this.$modalSure(delfromData)
        .then((res) => {
          this.$message.success(res.msg);
          // this.classList.splice(num, 1);
          this.getList();
        })
        .catch((res) => {
          this.$message.error(res.msg);
        });
    },
    // 列表
    getList() {
      this.loading = true;
      this.formValidate.status = this.formValidate.status || '';
      classListApi(this.formValidate)
        .then(async (res) => {
          this.classList = res.rows;
          this.total = res.total;
          this.loading = false;
        })
        .catch((res) => {
          this.loading = false;
          this.$message.error(res.msg);
        });
    },
    // 表格搜索
    userSearchs() {
      this.formValidate.pageNum = 1;
      this.getList();
    },
    //搜索配置项
    searchConfig() {
      if (this.config_name == '') {
        return this.$message.error('请输入要搜索的配置名称');
      }
      this.$router.push({
        path: this.$routeProStr + '/system/config/system_config_tab/list/0?config_name=' + this.config_name,
      });
    },
    // 修改成功
    submitFail() {
      //   this.getList();
    },
    // 修改是否显示
    onchangeIsShow(row) {
      let data = {
        id: row.id,
        status: row.status,
      };
      setStatusApi(data)
        .then(async (res) => {
          this.$message.success(res.msg);
        })
        .catch((res) => {
          this.$message.error(res.msg);
        });
    },
  },
};
</script>

<style scoped></style>
