<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('servicetype.服务类型')" prop="nameCn">
        <el-input
          v-model="queryParams.nameCn"
          :placeholder="$t('servicetype.请输入服务类型名称')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item  v-else-if="$t('message.language')=='language'" :label="$t('servicetype.服务类型')" prop="nameEn">
        <el-input
          v-model="queryParams.nameEn"
          :placeholder="$t('servicetype.请输入服务类型名称')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-else-if="$t('message.language')=='語言'" :label="$t('servicetype.服务类型')" prop="nameTw">
        <el-input
          v-model="queryParams.nameTw"
          :placeholder="$t('servicetype.请输入服务类型名称')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
	  <el-form-item v-else :label="$t('servicetype.服务类型')" prop="nameVi">
	    <el-input
	      v-model="queryParams.nameVi"
	      :placeholder="$t('servicetype.请输入服务类型名称')"
	      clearable
	      @keyup.enter.native="handleQuery"
	    />
	  </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('servicetype.搜索')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('servicetype.重置')}}</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="['system:ServiceType:add']"
        >{{$t('servicetype.新增')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:ServiceType:edit']"
        >{{$t('servicetype.修改')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:ServiceType:remove']"
        >{{$t('servicetype.删除')}}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:ServiceType:export']"
        >{{$t('servicetype.导出')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ServiceTypeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column :label="$t('servicetype.服务类型图片')" 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 v-if="$t('message.language')=='语言'" :label="$t('servicetype.服务类型')" align="center" prop="nameCn" />
      <el-table-column v-if="$t('message.language')=='语言'" :label="$t('servicetype.服务类型说明')" align="center" prop="illustrateCn" />
      <el-table-column v-if="$t('message.language')=='ngôn ngữ'" :label="$t('servicetype.服务类型')" align="center" prop="nameVi" />
      <el-table-column v-if="$t('message.language')=='ngôn ngữ'" :label="$t('servicetype.服务类型说明')" align="center" prop="illustrateVi" />
      <el-table-column v-if="$t('message.language')=='language'" :label="$t('servicetype.服务类型')" align="center" prop="nameEn" />
      <el-table-column v-if="$t('message.language')=='language'" :label="$t('servicetype.服务类型说明')" align="center" prop="illustrateEn" />
      <el-table-column v-if="$t('message.language')=='語言'" :label="$t('servicetype.服务类型')" align="center" prop="nameTw" />
      <el-table-column v-if="$t('message.language')=='語言'" :label="$t('servicetype.服务类型说明')" align="center" prop="illustrateTw" />
      <el-table-column :label="$t('servicetype.宽度')" align="center" prop="width" />
      <el-table-column :label="$t('servicetype.高度')" align="center" prop="height" />
      <el-table-column :label="$t('servicetype.操作')" 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="['system:ServiceType:edit']"
          >{{$t('servicetype.修改')}}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:ServiceType:remove']"
          >{{$t('servicetype.删除')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改ServiceType对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item :label="$t('servicetype.服务类型图片')" prop="image">
          <image-upload v-model="form.image"/>
        </el-form-item>
        <el-form-item label="服务类型" prop="nameCn">
          <el-input v-model="form.nameCn" placeholder="请输入服务类型" />
        </el-form-item>
        <el-form-item label="说明" prop="illustrateCn">
          <el-input v-model="form.illustrateCn" placeholder="请输入说明" />
        </el-form-item>
        <el-form-item label="Loại dịch vụ" prop="nameVi">
          <el-input v-model="form.nameVi" placeholder="Vui lòng nhập loại dịch vụ" />
        </el-form-item>
        <el-form-item label="Mô tả" prop="illustrateVi">
          <el-input v-model="form.illustrateVi" placeholder="Vui lòng nhập mô tả" />
        </el-form-item>
        <el-form-item label="Service Type" prop="nameEn">
          <el-input v-model="form.nameEn" placeholder="Please enter the service type name" />
        </el-form-item>
        <el-form-item label="illustrate" prop="illustrateEn">
          <el-input v-model="form.illustrateEn" placeholder="Please enter a description" />
        </el-form-item>
        <el-form-item label="服務類型" prop="nameTw">
          <el-input v-model="form.nameTw" placeholder="請輸入服務類型" />
        </el-form-item>
        <el-form-item label="說明" prop="illustrateTw">
          <el-input v-model="form.illustrateTw" placeholder="請輸入說明" />
        </el-form-item>
        <el-form-item :label="$t('servicetype.宽度')" prop="width">
          <el-input v-model="form.width" :placeholder="$t('servicetype.请输入宽度')" />
        </el-form-item>
        <el-form-item :label="$t('servicetype.高度')" prop="height">
          <el-input v-model="form.height" :placeholder="$t('servicetype.请输入高度')" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t('servicetype.确定')}}</el-button>
        <el-button @click="cancel">{{$t('servicetype.取消')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listServiceType, getServiceType, delServiceType, addServiceType, updateServiceType } from "@/api/system/ServiceType";
import Bus from '../../../utils/Bus.js'
export default {
  name: "ServiceType",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // ServiceType表格数据
      ServiceTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nameCn: null,
        nameVi: null,
        nameEn: null,
        nameTw: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
		  nameCn: [
		    { required: true, message: this.$t('servicetype.服务类型名不能为空'), trigger: "blur" }
		  ],
		  nameVi: [
		    { required: true, message: this.$t('servicetype.服务类型名不能为空'), trigger: "blur" }
		  ],
		  nameEn: [
		    { required: true, message: this.$t('servicetype.服务类型名不能为空'), trigger: "blur" }
		  ],
		  nameTw: [
		    { required: true, message: this.$t('servicetype.服务类型名不能为空'), trigger: "blur" }
		  ],
		  image: [
		    { required: true, message: this.$t('servicetype.服务类型图片不能为空'), trigger: "blur" }
		  ]
      }
    };
  },
  created() {
    this.getList();
  },
  mounted () {
    var that = this;
    Bus.$on('l18zh', content => {
        that.queryParams = {
          nameCn: null,
          nameVi: null,
          nameEn: null,
          nameTw: null
        }
    })
  },
  methods: {
    /** 查询ServiceType列表 */
    getList() {
      this.loading = true;
      listServiceType(this.queryParams).then(response => {
        this.ServiceTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        image: null,
        nameCn: null,
        illustrateCn: null,
        nameVi: null,
        illustrateVi: null,
        nameEn: null,
        illustrateEn: null,
        nameTw: null,
        illustrateTw: null,
        width: null,
        height: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t('servicetype.添加服务类型');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getServiceType(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('servicetype.修改服务类型');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateServiceType(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('servicetype.修改成功'));
              this.open = false;
              this.getList();
            });
          } else {
            addServiceType(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('servicetype.新增成功'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t('servicetype.是否确认删除编号为') + ids + this.$t('servicetype.的数据项')).then(function() {
        return delServiceType(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('servicetype.删除成功'));
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/ServiceType/export', {
        ...this.queryParams
      }, `ServiceType_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
