<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="类型编码" prop="conditionTypeCode" >
        <el-input
          v-model="queryParams.conditionTypeCode"
          placeholder="请输入类型编码"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型名称" prop="conditionTypeName" >
        <el-input
          v-model="queryParams.conditionTypeName"
          placeholder="请输入类型名称"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否重复" prop="conditionTypeRepeat">
        <el-select
          size="mini"
          v-model="queryParams.conditionTypeRepeat"
          clearable
          placeholder="请选择是否重复">
          <el-option
            v-for="item in isNOtOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <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="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sfa:aiSceneConditionType:add']"
        >新增</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="['sfa:aiSceneConditionType:edit']"-->
<!--        >修改</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="['sfa:aiSceneConditionType:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sfa:aiSceneConditionType:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="aiSceneConditionTypeList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="AI条件类型表主键" align="center" prop="conditionTypeId" />-->
      <el-table-column label="条件类型编码" align="center" prop="conditionTypeCode" />
      <el-table-column label="条件类型名称" align="center" prop="conditionTypeName" />
      <el-table-column label="条件类型备注" align="center" prop="conditionTypeRemarks" />
      <el-table-column label="是否可重复" align="center" prop="conditionTypeRepeat" >
        <template slot-scope="scope">
          <span v-if="scope.row.conditionTypeRepeat==1">否</span>
          <span v-else>是</span>
        </template>
      </el-table-column>
      <el-table-column label="条件数据来源" align="center" prop="conditionDataSources" >
        <template slot-scope="scope">
          <span v-if="scope.row.conditionDataSources==1">表</span>
          <span v-else>配置</span>
        </template>
      </el-table-column>
      <el-table-column label="数据来源表名称" align="center" prop="dataSourcesTable" :show-overflow-tooltip="true"/>
      <el-table-column label="数据来源表字段" align="center" prop="dataSourcesTableFields"  :show-overflow-tooltip="true"/>
      <el-table-column label="数据来源执行SQL" align="center" prop="dataSourcesSql" :show-overflow-tooltip="true"/>
      <el-table-column label="操作" 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="['sfa:aiSceneConditionType:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sfa:aiSceneConditionType:remove']"
          >删除</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"
    />

    <!-- 添加或修改AI适配场景条件类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item v-if="title!='添加AI适配场景条件类型'" label="条件类型编码" prop="conditionTypeCode">
          <el-input  disabled v-model="form.conditionTypeCode" placeholder="请输入条件类型编码" />
        </el-form-item>
        <el-form-item label="条件类型名称" prop="conditionTypeName" >
          <el-input v-model="form.conditionTypeName" placeholder="请输入条件类型名称" maxlength="100" show-word-limit/>
        </el-form-item>
        <el-form-item label="适配条件类型备注" prop="conditionTypeRemarks">
          <el-input v-model="form.conditionTypeRemarks" type="textarea" placeholder="请输入适配条件类型备注" maxlength="500" show-word-limit/>
        </el-form-item>
        <el-form-item label="是否可重复" prop="conditionTypeRepeat">
          <el-select
            v-model="form.conditionTypeRepeat"
            clearable
            style="width: 100%"
            placeholder="请选择是否可重复">
            <el-option
              v-for="item in isNOtOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="条件数据来源" prop="conditionDataSources">
          <el-select
            v-model="form.conditionDataSources"
            clearable
            style="width: 100%"
            placeholder="请选择是条件数据来源">
            <el-option
              label="配置"
              value="0">
            </el-option>
              <el-option
                label="表"
                value="1">
              </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数据来源表名称" prop="dataSourcesTable">
          <el-input v-model="form.dataSourcesTable" placeholder="请输入数据来源表名称" maxlength="100" show-word-limit/>
        </el-form-item>
        <el-form-item label="数据来源表字段" prop="dataSourcesTableFields">
          <el-input v-model="form.dataSourcesTableFields" placeholder="请输入数据来源表字段  条件编码、条件名称、条件别名" maxlength="300" show-word-limit/>
        </el-form-item>
        <el-form-item label="数据来源执行SQL" prop="dataSourcesSql">
          <el-input v-model="form.dataSourcesSql" type="textarea" placeholder="请输入内容" maxlength="500" show-word-limit/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAiSceneConditionType, getAiSceneConditionType, delAiSceneConditionType, addAiSceneConditionType, updateAiSceneConditionType } from "@/api/sfa/aiSceneConditionType";

export default {
  name: "AiSceneConditionType",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // AI适配场景条件类型表格数据
      aiSceneConditionTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        conditionTypeCode: null,
        conditionTypeName: null,
        conditionTypeRemarks: null,
        conditionTypeRepeat: null,
        conditionDataSources: null,
        dataSourcesTable: null,
        dataSourcesTableFields: null,
        dataSourcesSql: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        conditionTypeRepeat:[ { required: true, message: '请选择条件数据来源', trigger: 'change' }],
        conditionDataSources:[ { required: true, message: '请选择是否可重复', trigger: 'change' }],
        conditionTypeName:[ { required: true, message: '请输入类型名称', trigger: 'blur' }],
      },
      isNOtOptions:[],
    };
  },
  created() {
    this.getDicts("is_not").then(response => {
      this.isNOtOptions = response.data;
    });
    this.getList();
  },
  methods: {
    /** 查询AI适配场景条件类型列表 */
    getList() {
      this.loading = true;
      listAiSceneConditionType(this.queryParams).then(response => {
        this.aiSceneConditionTypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        conditionTypeId: null,
        conditionTypeCode: null,
        conditionTypeName: null,
        conditionTypeRemarks: null,
        conditionTypeRepeat: null,
        delFlag: null,
        conditionDataSources: null,
        createTime: null,
        dataSourcesTable: null,
        createBy: null,
        dataSourcesTableFields: null,
        updateTime: null,
        dataSourcesSql: null,
        updateBy: 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.conditionTypeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加AI适配场景条件类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const conditionTypeId = row.conditionTypeId || this.ids
      getAiSceneConditionType(conditionTypeId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改AI适配场景条件类型";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.conditionTypeId != null) {
            updateAiSceneConditionType(this.form).then(response => {
              if(response.code==200){
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              }
            });
          } else {
            addAiSceneConditionType(this.form).then(response => {
              if(response.code==200){
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const conditionTypeIds = row.conditionTypeId || this.ids;
      this.$modal.confirm('是否确认删除AI适配场景条件类型编号为"' + row.conditionTypeCode + '"的数据项？').then(function() {
        return delAiSceneConditionType(conditionTypeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sfa/aiSceneConditionType/export', {
        ...this.queryParams
      }, `aiSceneConditionType_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
