<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="标题" prop="title" clearable size="small">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="模板类型" prop="tempType">
        <el-select v-model="queryParams.tempType" placeholder="请选择模板类型" clearable size="small">
          <el-option
            v-for="dict in tempTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </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="['his:inquiryTemp: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="['his:inquiryTemp: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="['his:inquiryTemp:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['his:inquiryTemp:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-tabs type="card" v-model="actName" @tab-click="handleClickX">
      <el-tab-pane label="全部" name="10"></el-tab-pane>
      <el-tab-pane v-for="(item,index) in tempTypeOptions" :label="item.dictLabel" :name="item.dictValue"></el-tab-pane>
    </el-tabs>
    <el-table v-loading="loading" border :data="inquiryTempList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />


      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="模板类型" align="center" prop="tempType">
        <template slot-scope="scope">
          <dict-tag :options="tempTypeOptions" :value="scope.row.tempType"/>
        </template>
      </el-table-column>
      <el-table-column label="排序号" align="center" prop="sort" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="修改时间" align="center" prop="updateTime" />
      <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="['his:inquiryTemp:edit']"
          >修改</el-button>
           <el-button
                      size="mini"
                      type="text"
                      @click="handledetails(scope.row)"
                    >详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['his:inquiryTemp: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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="模板类型" prop="tempType">
          <el-select v-model="form.tempType" placeholder="请选择模板类型">
            <el-option
              v-for="dict in tempTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="排序号" prop="sort">
          <el-input-number v-model="form.sort" :min="0"  label="请输入排序号"></el-input-number>
        </el-form-item>
        <el-row>
           <el-col :span="3"><div style="margin-top: 9px;font-weight: bold;float: right; margin-right: 15px;">选项</div></el-col>
          <el-col :span="21">
          <el-table :data="itemJson"    :cell-style="{ textAlign: 'center' }"		 :header-cell-style="{textAlign: 'center'}"   >
            <el-table-column label="标题" prop="name" >
              <template slot-scope="scope">
                <el-input v-model="scope.row.name" ></el-input>
              </template>
            </el-table-column>
            <el-table-column label="分值" prop="score" >
              <template slot-scope="scope">
                 <el-input-number v-model="scope.row.score" controls-position="right"  :min="0" size="small"></el-input-number>
               </template>
            </el-table-column>
            <el-table-column label="操作">
              <template slot-scope="scope">
                 <el-button @click="deleteRow(scope.$index)"   size="mini" type="text" v-if="itemJson.length>1">删除</el-button>
                  <el-button @click="addRow" size="mini" type="text" >新增</el-button>
              </template>
            </el-table-column>
          </el-table>
        </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>
       <el-drawer
    	:with-header="false"
            size="75%"
             :title="show.title" :visible.sync="show.open">
         <inquiryTempDetails  ref="Details" />
       </el-drawer>
  </div>
</template>

<script>
import { listInquiryTemp, getInquiryTemp, delInquiryTemp, addInquiryTemp, updateInquiryTemp, exportInquiryTemp } from "@/api/his/inquiryTemp";
import inquiryTempDetails from '../../components/his/inquiryTempDetails.vue';
export default {
  name: "InquiryTemp",
  components: { inquiryTempDetails },
  data() {
    return {
      show:{
              open:false,
            },
      itemJson: [
        { name: "套餐1", score: "0"},
      ],
      actName:"10",
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模板表格数据
      inquiryTempList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 模板类型字典
      tempTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tempType: null,
        title: null,
        createTime: null,
        sort: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        tempType: [
          { required: true, message: "模板类型不能为空", trigger: "change" }
        ],
        title: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        sort: [
          { required: true, message: "排序号不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_inquiry_temp_type").then(response => {
      this.tempTypeOptions = response.data;
    });
  },
  methods: {
    addRow() {
         this.itemJson.push({ name: "标题", score: "0"});
       },
      deleteRow(index) {
        this.itemJson.splice(index, 1);
      },
    /** 查询模板列表 */
    getList() {
      this.loading = true;
      listInquiryTemp(this.queryParams).then(response => {
        this.inquiryTempList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleClickX(tab, event) {
     if(tab.name=="10"){
       this.queryParams.tempType=null;
     }else{
       this.queryParams.tempType=tab.name;
     }
      this.handleQuery();
    },
    // 表单重置
    reset() {
      this.form = {
        tempId: null,
        tempType: null,
        title: null,
        itemJson: null,
        createTime: null,
        updateTime: null,
        sort: 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.tempId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模板";
      this.priceJson=[
        { name: "标题", score: "0"},
      ];
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const tempId = row.tempId || this.ids
      getInquiryTemp(tempId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改模板";
        this.itemJson=JSON.parse(this.form.itemJson)
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          for (var i = 0; i < this.itemJson.length; i++) {
            if (!this.itemJson[i].name) {
              this.$message("标题不能为空")
              return;
            }
          }
          this.form.itemJson=JSON.stringify(this.itemJson )
          if (this.form.tempId != null) {
            updateInquiryTemp(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInquiryTemp(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
        handledetails(row){
            this.show.open=true;
            setTimeout(() => {
                 this.$refs.Details.getDetails(row.tempId);
            }, 1);
        },
    /** 删除按钮操作 */
    handleDelete(row) {
      const tempIds = row.tempId || this.ids;
      this.$confirm('是否确认删除模板编号为"' + tempIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delInquiryTemp(tempIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有模板数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportInquiryTemp(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
