<template>
  <div class="aaa">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>
    <el-table v-loading="loading" border :data="testTempItemList" @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="name" />
      <el-table-column label="项目类型" align="center" prop="itemType" />
      <el-table-column label="排序号" align="center" prop="sort" />
      <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)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</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>
      <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-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="scoreJson"    :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"   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="scoreJson.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-item label="排序" prop="sort">
            <el-input-number v-model="form.sort"  :min="0" label="排序号"></el-input-number>
          </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 { listTestTempItem, getTestTempItem, delTestTempItem, addTestTempItem, updateTestTempItem, exportTestTempItem, } from "@/api/his/testTempItem";
import { getTempType} from "@/api/his/testTemp";


import { getAllTestTemp } from "@/api/his/testTemp";
import testTempItemDetails from '../../components/his/testTempItemDetails.vue';
export default {
  name: "TestTempItem",
  components: { testTempItemDetails },
  data() {
    return {
      show:{
              open:false,
            },
      scoreJson: [
        { name: "套餐1", score: "0"},
      ],
      itemTypeList:[],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 模板项目表格数据
      testTempItemList: [],
      TempList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 项目类型 数据字典字典
      itemTypeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        tempId: null,
        itemType: null,
        title: null,
        sort: null,
        name: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        title: [
          { required: true, message: "标题不能为空", trigger: "change" }
        ],
        tempId: [
          { required: true, message: "模板不能为空", trigger: "blur" }
        ],
        itemType: [
          { required: true, message: "项目类型不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getDicts("sys_test_temp_item_type").then(response => {
      this.itemTypeOptions = response.data;
    });
    this.getTestList();

  },
  methods: {
    getDetails(name,id){
      this.queryParams.itemType=name;
      this.queryParams.tempId=id;
      this.getList();
    },
    tempIdChange(){
      getTempType(this.form.tempId).then(response => {
        this.itemTypeList = response.data;
      });
    },
    /** 查询模板项目列表 */
    getList() {
      this.loading = true;
      listTestTempItem(this.queryParams).then(response => {
        this.testTempItemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handledetails(row){
            this.show.open=true;
            setTimeout(() => {
                 this.$refs.Details.getDetails(row.itemId,row.name);
            }, 1);
        },
    getTestList(){
      getAllTestTemp().then(response => {
        this.TempList = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        itemId: null,
        tempId: null,
        itemType: null,
        title: null,
        scoreJson: 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.itemId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    addRow() {
         this.scoreJson.push({ name: "标题", score: "0"});
       },
      deleteRow(index) {
        this.scoreJson.splice(index, 1);
      },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加模板项目";
      this.form.tempId=this.queryParams.tempId;
      this.form.itemType=this.queryParams.itemType
      this.scoreJson=[
        { name: "标题", score: "0"},
      ];
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const itemId = row.itemId || this.ids
      getTestTempItem(itemId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改模板项目";
        this.tempIdChange()
         this.scoreJson=JSON.parse(this.form.scoreJson)
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          for (var i = 0; i < this.scoreJson.length; i++) {
            if (!this.scoreJson[i].name) {
              this.$message("标题不能为空")
              return;
            }
          }
          this.form.scoreJson=JSON.stringify(this.scoreJson )
          if (this.form.itemId != null) {
            updateTestTempItem(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTestTempItem(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const itemIds = row.itemId || this.ids;
      this.$confirm('是否确认删除模板项目编号为"' + itemIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delTestTempItem(itemIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有模板项目数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportTestTempItem(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
