<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="属性名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入属性名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="属性类型" prop="valueType">
        <el-select v-model="queryParams.valueType" placeholder="请选择属性类型" clearable size="small">
          <el-option label="文本" value="text" />
          <el-option label="数字" value="number" />
          <el-option label="日期" value="date" />
          <el-option label="单选" value="select" />
          <el-option label="多选" value="multiSelect" />
        </el-select>
      </el-form-item>
      <el-form-item label="行业类型" prop="tradeType">
        <el-select v-model="queryParams.tradeType" placeholder="请选择行业类型" clearable size="small">
          <el-option
            v-for="dict in tradeTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" 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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:customerPropertyTemplate:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crm:customerPropertyTemplate:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="templateList" @selection-change="handleSelectionChange" border stripe>
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="60" align="center" />
      <el-table-column label="属性名称" align="center" prop="name" min-width="120">
        <template slot-scope="scope">
          <span class="property-name">{{ scope.row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="属性类型" align="center" prop="valueType" width="100">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.valueType === 'text'" type="info" size="small">文本</el-tag>
          <el-tag v-else-if="scope.row.valueType === 'number'" type="warning" size="small">数字</el-tag>
          <el-tag v-else-if="scope.row.valueType === 'date'" type="success" size="small">日期</el-tag>
          <el-tag v-else-if="scope.row.valueType === 'select'" type="primary" size="small">单选</el-tag>
          <el-tag v-else-if="scope.row.valueType === 'multiSelect'" type="danger" size="small">多选</el-tag>
          <span v-else>{{ scope.row.valueType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="行业类型" align="center" prop="tradeType" width="120">
        <template slot-scope="scope">
          <dict-tag v-if="scope.row.tradeType" :options="tradeTypeOptions" :value="scope.row.tradeType"/>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="AI提取提示" align="left" prop="aiHint" min-width="200" show-overflow-tooltip>
        <template slot-scope="scope">
          <span class="ai-hint-text">{{ scope.row.aiHint || '-' }}</span>
        </template>
      </el-table-column>
      <el-table-column label="意向开关" align="center" prop="intention" width="120">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.intention === 1 || scope.row.intention === '1'" type="success" size="small">开启</el-tag>
          <el-tag v-else-if="scope.row.intention === 0 || scope.row.intention === '0'" type="info" size="small">关闭</el-tag>
          <span v-else>-</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="160">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span style="margin-left: 5px">{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['crm:customerPropertyTemplate:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            style="color: #f56c6c"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crm:customerPropertyTemplate: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"
    />

    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" class="property-form">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="属性名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入属性名称" maxlength="50" show-word-limit />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="属性类型" prop="valueType">
              <el-select v-model="form.valueType" placeholder="请选择属性类型" style="width: 100%">
                <el-option label="文本" value="text" />
                <el-option label="数字" value="number" />
                <el-option label="日期" value="date" />
                <el-option label="单选" value="select" />
                <el-option label="多选" value="multiSelect" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="行业类型" prop="tradeType">
              <el-select v-model="form.tradeType" placeholder="请选择行业类型" style="width: 100%" clearable>
                <el-option
                  v-for="dict in tradeTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">AI配置</el-divider>
        <el-row>
          <el-col :span="24">
            <el-form-item label="AI提取提示" prop="aiHint">
              <el-input 
                v-model="form.aiHint" 
                type="textarea" 
                :rows="4" 
                placeholder="请输入AI属性提取提示，用于指导AI从对话中提取该属性的值。例如：请从对话中提取患者的年龄信息，注意识别数字和单位" 
                maxlength="500"
                show-word-limit
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider content-position="left">其他信息</el-divider>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="意向开关" prop="intention">
              <el-switch v-model="form.intention" active-value="1" inactive-value="0" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" :rows="2" placeholder="请输入备注信息" maxlength="200" show-word-limit />
            </el-form-item>
          </el-col>
        </el-row>
      </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 { listPropertyTemplate, getPropertyTemplate, delPropertyTemplate, addPropertyTemplate, updatePropertyTemplate, exportPropertyTemplate } from "@/api/crm/propertyTemplate";

export default {
  name: "PropertyTemplate",
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      templateList: [],
      title: "",
      open: false,
      tradeTypeOptions: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        valueType: null,
        tradeType: null
      },
      form: {},
      rules: {
        name: [
          { required: true, message: "属性名称不能为空", trigger: "blur" }
        ],
        valueType: [
          { required: true, message: "属性类型不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("trade_type").then(response => {
      this.tradeTypeOptions = response.data;
    });
  },
  methods: {
    getList() {
      this.loading = true;
      listPropertyTemplate(this.queryParams).then(response => {
        this.templateList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    reset() {
      this.form = {
        id: null,
        name: null,
        valueType: null,
        aiHint: null,
        tradeType: null,
        intention: null,
        remark: 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 = "添加客户属性模板";
    },
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPropertyTemplate(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) {
            updatePropertyTemplate(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addPropertyTemplate(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除客户属性模板编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPropertyTemplate(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有客户属性模板数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPropertyTemplate(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 20px;
}

.property-form {
  padding: 10px 20px;
}

.property-form .el-divider {
  margin: 15px 0;
}

.property-form .el-divider__text {
  font-weight: 500;
  color: #606266;
}

.property-form .el-form-item {
  margin-bottom: 18px;
}

.property-form .el-textarea ::v-deep .el-textarea__inner {
  resize: none;
}

.property-name {
  font-weight: 500;
  color: #303133;
}

.ai-hint-text {
  color: #606266;
  font-size: 13px;
  line-height: 1.5;
}

.el-table ::v-deep .el-table__row {
  height: 50px;
}

.el-table ::v-deep .el-tag {
  border-radius: 4px;
}
</style>
