<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
      <el-form-item v-if="$t('message.language')=='语言'" :label="$t('fenlei.分类名')" prop="name">
        <el-input
          v-model="queryParams.name"
          :placeholder="$t('fenlei.请输入分类名')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-else-if="$t('message.language')=='language'" :label="$t('fenlei.分类名')" prop="nameEn">
        <el-input
          v-model="queryParams.nameEn"
          :placeholder="$t('fenlei.请输入分类名')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-else-if="$t('message.language')=='語言'" :label="$t('fenlei.分类名')" prop="nameTw">
        <el-input
          v-model="queryParams.nameTw"
          :placeholder="$t('fenlei.请输入分类名')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-else :label="$t('fenlei.分类名')" prop="nameVi">
        <el-input
          v-model="queryParams.nameVi"
          :placeholder="$t('fenlei.请输入分类名')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('fenlei.搜索')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('fenlei.重置')}}</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"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['fenlei:type:add']"-->
<!--          v-hasRole="['admin','oadmin']"-->
<!--        >{{$t('fenlei.新增')}}</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-sort"
          size="mini"
          @click="toggleExpandAll"
        >{{$t('fenlei.展开/折叠')}}</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['fenlei:type:export']"-->
<!--          v-hasRole="['admin','oadmin']"-->
<!--        >{{$t('fenlei.导出')}}</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="typeList"
      row-key="id"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column v-if="$t('message.language')=='语言'" :label="$t('fenlei.分类名')" align="center" prop="name" width="260"></el-table-column>
      <el-table-column v-if="$t('message.language')=='language'" :label="$t('fenlei.分类名')" align="center" prop="nameEn" width="260"></el-table-column>
      <el-table-column v-if="$t('message.language')=='語言'" :label="$t('fenlei.分类名')" align="center" prop="nameTw" width="260"></el-table-column>
      <el-table-column v-if="$t('message.language')=='Ngôn ngữ'" :label="$t('fenlei.分类名')" align="center" prop="nameVi" width="260"></el-table-column>
      <el-table-column :label="$t('fenlei.排序')" align="center" prop="sort" width="200"></el-table-column>
      <el-table-column :label="$t('fenlei.分类图片')" align="center" prop="image" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.image" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column :label="$t('fenlei.标识')" align="center" prop="tag" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.tag" type="success" effect="dark">{{scope.row.tag}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('fenlei.操作')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['fenlei:type:edit']"
            v-hasRole="['admin','oadmin']"
          >{{$t('fenlei.修改')}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-plus"
            @click="handleAdd(scope.row)"
            v-hasPermi="['fenlei:type:add']"
            v-hasRole="['admin','oadmin']"
          >{{$t('fenlei.新增')}}</el-button>
          <el-button
            v-if="scope.row.parentId != 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['fenlei:type:remove']"
            v-hasRole="['admin','oadmin']"
          >{{$t('fenlei.删除')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 添加或修改门店分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="760px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-row>
          <el-col :span="24" v-if="form.parentId !== 0">
            <el-form-item :label="$t('fenlei.上级分类')" prop="parentId">
              <treeselect v-model="form.parentId" :options="typeOptions" :normalizer="normalizer" :placeholder="$t('fenlei.选择上级分类')" />
            </el-form-item>
          </el-col>
        </el-row>
                 <el-row>
           <el-col :span="12">
             <el-form-item v-if="$t('message.language')=='语言'" :label="$t('fenlei.简体中文名')" prop="name">
               <el-input v-model="form.name" placeholder="请输入中文分类名" />
             </el-form-item>
             <el-form-item v-else :label="$t('fenlei.简体中文名')" prop="name">
               <el-input v-model="form.name" placeholder="请输入中文分类名" />
             </el-form-item>
           </el-col>
           <el-col :span="12">
             <el-form-item v-if="$t('message.language')=='語言'" :label="$t('fenlei.繁体体中文名')" prop="nameTw">
               <el-input v-model="form.nameTw" placeholder="请输入繁体体中文名" />
             </el-form-item>
             <el-form-item v-else :label="$t('fenlei.繁体体中文名')" prop="nameTw">
               <el-input v-model="form.nameTw" placeholder="请输入繁体体中文名" />
             </el-form-item>
           </el-col>
         </el-row>
                 <el-row>

           <el-col :span="12">
             <el-form-item v-if="$t('message.language')=='language'" :label="$t('fenlei.英语分类名')" prop="nameEn">
               <el-input v-model="form.nameEn" placeholder="Please enter a classification name" />
             </el-form-item>
             <el-form-item v-else :label="$t('fenlei.英语分类名')" prop="nameEn">
               <el-input v-model="form.nameEn" placeholder="Please enter a classification name" />
             </el-form-item>
           </el-col>
                   <el-col :span="12">
                     <el-form-item :label="$t('fenlei.排序')" prop="sort">
                       <el-input v-model="form.sort" :placeholder="$t('fenlei.请输入序号')" />
                     </el-form-item>
                   </el-col>
         </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('fenlei.分类图片')" prop="image">
              <image-upload :limit="1" v-model="form.image"/>
            </el-form-item>
          </el-col>

        </el-row>
<!--        <el-row>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item :label="$t('fenlei.标识')" prop="tag">-->
<!--              <el-input v-model="form.tag" :placeholder="$t('fenlei.请输入标识')" />-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t('fenlei.确定')}}</el-button>
        <el-button @click="cancel">{{$t('fenlei.取消')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listType, getType, delType, addType, updateType,treeList } from "@/api/fenlei/type";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Bus from '../../../utils/Bus.js'
export default {
  name: "Type",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 表格树数据
      typeList: [],
      // 分类树选项
      typeOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 查询参数
      queryParams: {
        name: undefined,
        nameVi: undefined,
        nameEn: undefined,
        nameTw: undefined,
        image: undefined,
        sort: undefined,
        parentId: undefined,
        tag: undefined,
        ancestors: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: this.$t('fenlei.分类名不能为空'), trigger: "blur" }
        ],
        nameVi: [
          { required: true, message: this.$t('fenlei.分类名不能为空'), trigger: "blur" }
        ],
        nameEn: [
          { required: true, message: this.$t('fenlei.分类名不能为空'), trigger: "blur" }
        ],
        nameTw: [
          { required: true, message: this.$t('fenlei.分类名不能为空'), trigger: "blur" }
        ],
        image: [
          { required: true, message: this.$t('fenlei.分类图片不能为空'), trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  mounted () {
    var that = this;
    Bus.$on('l18zh', content => {
        that.queryParams = {
          name: null,
          nameVi: null,
          nameEn: null,
          nameTw: null,
        }
    })
  },
  methods: {
    /** 查询门店分类列表 */
    getList() {
      this.loading = true;
      treeList(this.queryParams).then(response => {
        this.typeList = this.handleTree(response.data, "id");
        this.loading = false;
      });
    },
    /** 转换分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.name || node.nameVi || node.nameEn || node.nameTw,
        children: node.children
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentId: 0,
        name: null,
        nameVi: null,
        nameEn: null,
        nameTw: null,
        image: null,
        sort: null,
        tag: null,
        ancestors: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row != undefined) {
        this.form.parentId = row.id;
      }
      this.open = true;
      this.title = this.$t('fenlei.添加门店分类');
      listType().then(response => {
        this.typeOptions = this.handleTree(response.data, "id");
      });
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getType(row.id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('fenlei.修改门店分类');
        treeList().then(response => {
          this.typeOptions = this.handleTree(response.data, "id");
        });
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateType(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('fenlei.修改成功'));
              this.open = false;
              this.getList();
            });
          } else {
            addType(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('fenlei.新增成功'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm(this.$t('fenlei.是否确认删除门店分类编号为')+'"' + row.name + '"'+this.$t('fenlei.的数据项')).then(function() {
        return delType(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('fenlei.删除成功'));
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('fenlei/type/export', {
        ...this.queryParams
      }, `type_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
