
<template>
    <el-dialog
      :visible.sync="showDialog"
      :title="title"
      :modal-append-to-body="false"
      style="text-align: left;"
      @close="closeDialog"
      :close-on-click-modal="false"
      width="1200px"
    >
      <div class="user-panel" v-loading="loading">
        <el-form
          ref="form"
          :model="formModel"
          :rules="ruleValidate"
          :label-width="'100px'"
          :inline="true"
        >
            <el-table 
                class="compact-table"
                :data="formModel.optionList" >
                <el-table-column prop="serialNum" label="序号" width="150">
                    <template slot-scope="scope">
                        <el-form-item
                        :prop="'optionList.' + scope.$index + '.serialNum'"
                        :rules="ruleValidate['serialNum']"
                        >
                        <el-input placeholder="序号"  v-model="scope.row.serialNum"  size="mini"> 
                        </el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="名称" width="250">
                    <template slot-scope="scope">
                        <el-form-item
                        :prop="'optionList.' + scope.$index + '.name'"
                        :rules="ruleValidate['name']"
                        >
                        <el-input placeholder="名称"  v-model="scope.row.name"  size="mini" style="width:200px"> 
                        </el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column prop="isSum" label="是否合计" width="150">
                    <template slot-scope="scope">
                        <el-form-item
                        :prop="'optionList.' + scope.$index + '.isSum'"
                        :rules="ruleValidate['isSum']"
                        >
                        <el-select v-model="scope.row.isSum" placeholder="请选择" size="mini" style="width:130px">
                          <el-option
                            v-for="item in isSumList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value">
                          </el-option>
                        </el-select>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column prop="budgetRevenue" label="预算收入" width="180">
                    <template slot-scope="scope">
                        <el-form-item
                        :prop="'optionList.' + scope.$index + '.budgetRevenue'"
                        :rules="ruleValidate['budgetRevenue']"
                        >
                        <el-input-number v-if="!scope.row.isSum" placeholder="预算收入"  v-model="scope.row.budgetRevenue"  size="mini" style="width:150px"> 
                        </el-input-number>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column prop="departmentIdArr" label="部门" width="180">
                    <template slot-scope="scope">
                        <el-form-item
                        :prop="'optionList.' + scope.$index + '.departmentIdArr'"
                        :rules="ruleValidate['departmentIdArr']"
                        >
                        <el-select v-if="!scope.row.isSum" v-model="scope.row.departmentIdArr" placeholder="请选择" multiple size="mini" style="width:150px">
                          <el-option
                            v-for="item in departmentList"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id">
                          </el-option>
                        </el-select>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column prop="index" label="排序号" width="120">
                    <template slot-scope="scope">
                        <el-form-item
                        :prop="'optionList.' + scope.$index + '.index'"
                        :rules="ruleValidate['index']"
                        >
                        <el-input placeholder="排序号"  v-model="scope.row.index"  size="mini" style="width:100px"> 
                        </el-input>
                        </el-form-item>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="100" fixed="right">
                <template slot-scope="{row}">
                    <el-form-item>
                    <i class="el-icon-delete my-font" @click="handleDelete(row)"></i>
                    <i class="el-icon-document-add my-font" @click="handleInsert(row)"></i>
                    </el-form-item>
                </template>
                </el-table-column>
            </el-table>
        </el-form>
      </div>
      <div class="el-table__empty-block">
    <el-button icon="el-icon-circle-plus-outline" @click="handleAdd()">新增</el-button>
    </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button type="primary" @click="handleSubmit" :loading="submitting"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </template>
  <script>
  import Constant from "@/constant";
  import { getToken } from "@/utils/auth"; // get token from cookie
  import templateOptionApi from "@/api/base/templateOption";
  import departmentInfoApi from "@/api/base/departmentInfo";
  
  export default {
    props: ["businessKey", "title"],
    data() {
      return {
          ruleValidate: {
              userName: [
              { required: true, message: "用户名不能为空", trigger: "blur" },
              ],
              realName: [
              { required: true, message: "真实名称不能为空", trigger: "blur" },
              ],
          },
          roleList: [],
          formModel: {
            optionList:[],
          },
          showDialog: true,
          loading: false,
          submitting: false,
          enterpriseList:[],
          optionList:[],
          departmentList:[],
          isSumList:[
            {
              "label":"否",
              "value":false
            },
            {
              "label":"是",
              "value":true
            }]
      };
    },
    created() {
      var self = this;
    
      departmentInfoApi.list().then(function (response) {
        var jsonData = response.data;
        if (jsonData.result) {
          if (jsonData.data != null && jsonData.data != "") {
            self.departmentList = jsonData.data;
          }
        }
      });
    },
    methods: {
      closeDialog() {
        this.$emit("close", false);
      },
      handleSubmit() {
        var self = this;
  
        this.$refs["form"].validate((valid) => {
          if (valid) {
            (function () {
                var id = self.formModel.id;
                self.formModel.templateOptionList = self.formModel.optionList;
                return templateOptionApi.update(self.formModel);
            })().then(function (response) {
              var jsonData = response.data;
  
              if (jsonData.result) {
                self.$message({
                  message: "保存成功!",
                  type: "success",
                });
  
                self.$emit("close", true);
              } else {
                self.$message({
                  message: jsonData.message + "",
                  type: "warning",
                });
  
                self.$emit("close", false);
              }
            });
          }
        });
      },
      handleAdd() {
        this.formModel.optionList.push({
            serialNumber: "",
            name: "",
        });
      },
      handleInsert(row) {
        this.formModel.optionList.push({
            serialNumber: "",
            name: "",
          });
      },
      handleDelete(row) {
        var optionList = this.formModel.optionList;

        optionList.splice(optionList.indexOf(row), 1);
      },
    },
    async mounted() {
      var self = this;
      self.loading = true;
  
      (function () {
        if (self.businessKey != null && self.businessKey.length > 0) {
          return templateOptionApi.edit(self.businessKey);
        } else {
          return templateOptionApi.create();
        }
      })()
        .then((response) => {
          var jsonData = response.data;
  
          if (jsonData.result) {
            self.formModel.templateId = jsonData.data.templateId;
            self.formModel.optionList = jsonData.data.templateOptionList;
  
              self.showModal = true;
          } else {
            self.$message.error(jsonData.message + "");
          }
  
          self.loading = false;
        })
        .catch((error) => {
          self.$message.error(error + "");
          self.loading = false;
        });
    },
    components: {
    },
  };
  </script>
  <style scoped>
  .user-panel {
    margin: 10px auto;
  }
  .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409EFF;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
  </style>