<template>
  <div>
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="模块名称" prop="functionName">
        <el-input
          v-model="queryParams.functionName"
          placeholder="请输入模块名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="系统" prop="system">
        <el-select
          v-model="queryParams.system"
          placeholder="请选择系统"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in systemList"
            :key="dict.system"
            :label="dict.systemName"
            :value="dict.system"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="客户端" prop="client">
        <el-select
          v-model="queryParams.client"
          placeholder="请选择客户端"
          clearable
          size="small"
        >
          <el-option label="全部" value="0" />
          <el-option label="app" value="1" />
          <el-option label="web" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item style="float: right">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <!-- <el-row :gutter="10" class="marginBottom10">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
    </el-row> -->
    <!-- <el-alert
    v-show="!multiple"
      :title="'已选择'+ delList.length + '项'"
      class="marginBottom10"
      :closable="false"
      show-icon
      type="warning">
    </el-alert> -->
    <el-table
      v-loading="loading"
      :data="tableList"
      border
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <!-- <el-table-column type="selection" width="55" /> -->
      <el-table-column type="index" width="50" label="序号" />
      <el-table-column label="模块名称" align="center" prop="functionName" />
      <el-table-column label="系统" align="center" prop="systemName" />
      <el-table-column label="客户端" align="center" prop="clientName" />
      <el-table-column label="介绍" align="center" prop="functionDescription" :show-overflow-tooltip="true"/>
      <!-- <el-table-column label="app优先级" align="center" prop="appPriority" />
      <el-table-column label="web优先级" align="center" prop="webPriority" />
      <el-table-column label="app链接" align="center" prop="appUrl" />
      <el-table-column label="web链接" align="center" prop="webUrl" />
      <el-table-column label="web图标链接" align="center" prop="webIcon" />
      <el-table-column label="app图标链接" align="center" prop="appIcon" />
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete fontRed"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column> -->
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加和编辑弹出框 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogFormVisible"
      append-to-body
      width="600px"
    >
      <el-form
        :model="form"
        ref="form"
        :rules="rules"
        label-position="right"
        label-width="100px"
      >
        <el-form-item label="模板名称" prop="functionName">
          <el-input v-model="form.functionName" placeholder="请输入模板名称"></el-input>
        </el-form-item>
        <el-form-item label="系统" prop="system">
          <el-select
            v-model="form.system"
            placeholder="请选择系统"
            style="width: 100%"
          >
            <el-option
              v-for="dict in systemList"
              :key="dict.system"
              :label="dict.systemName"
              :value="dict.system"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="客户端" prop="client">
          <el-select
            v-model="form.client"
            placeholder="请选择客户端"
            style="width: 100%"
          >
            <el-option label="全部" value="0" />
            <el-option label="web" value="web" />
            <el-option label="app" value="app" />
          </el-select>
        </el-form-item>
        <el-form-item label="app优先级" prop="appPriority">
          <el-input v-model="form.appPriority" placeholder="请输入app优先级"></el-input>
        </el-form-item>
        <el-form-item label="web优先级" prop="webPriority">
          <el-input v-model="form.webPriority" placeholder="请输入web优先级"></el-input>
        </el-form-item>
        <el-form-item label="app链接" prop="appUrl">
          <el-input v-model="form.appUrl" placeholder="请输入app链接"></el-input>
        </el-form-item>
        <el-form-item label="web链接" prop="webUrl">
          <el-input v-model="form.webUrl" placeholder="请输入web链接"></el-input>
        </el-form-item>
        <el-form-item label="web图标链接" prop="webIcon">
          <el-input v-model="form.webIcon" placeholder="请输入web图标链接"></el-input>
        </el-form-item>
        <el-form-item label="app图标链接" prop="appIcon">
          <el-input v-model="form.appIcon" placeholder="请输入app图标链接"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitForm"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getSystemList,
  getFunctionList,
  getFunctionAdd,
  getFunctionUpdate,
  getFunctionDel
} from "../../../common/js/home";
export default {
  name: "Module",
  components: {},
  data() {
    return {
      loading: false,
      systemList: [],
      queryParams: {
        current: 1,
        pageSize: 10,
        functionName: null,
        system: null,
        client: '0',
      },
      tableList: [],
      total: 0,
      multiple: true,
      delList:[],
      dialogFormVisible: false,
      dialogTitle: "",
      form: {},
      // 表单校验
      rules: {
        functionName: [
          { required: true, message: "模板名称不能为空", trigger: "blur" },
        ],
        system: [
          { required: true, message: "请选择模板名称", trigger: "change" },
        ],
        client: [
          { required: true, message: "请选择客户端", trigger: "change" },
        ],
      },
    };
  },
  computed: {},
  watch: {},
  created() {
    console.log('模块')
    this.init();
    this.getList();
  },
  methods: {
    init() {
      getSystemList().then(res=>{
        console.log('res: ', res);
        this.systemList = res.data
      })
    },
    getList() {
      this.loading = true;
      getFunctionList(this.queryParams).then(res=>{
        this.loading = false;
        this.tableList = res.data.list;
        this.total = res.data.total
      })
    },
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    // 重置
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.current = 1;
      this.getList();
    },
    handleSelectionChange(val) {
      console.log("val: ", val);
      this.delList = val
      if(val.length != 0){
        this.multiple = false
      }else{
        this.multiple = true
      }
    },
    // 添加
    handleAdd() {
      this.dialogTitle = "模板添加";
      this.dialogFormVisible = true;
    },
    // 编辑
    handleEdit(val) {
      console.log("val: ", val);
      this.dialogTitle = "模板编辑";
      this.dialogFormVisible = true;
    },
    //删除
    handleDelete(val) {
      console.log("val: ", val);
      this.$confirm('是否确认删除', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.functionId != undefined) {
            getFunctionUpdate(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            getFunctionAdd(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    }
  },
};
</script>

<style lang="less" scoped>
</style>
