
<style scoped>
.user-panel {
  margin: 10px auto;
}
</style>
<template>
  <el-dialog
    :visible.sync="showDialog"
    :title="title"
    :modal-append-to-body="false"
    style="text-align: left"
    @close="closeDialog"
    :close-on-click-modal="false"
  >
    <div class="user-panel" v-loading="loading">
      <el-form
        ref="form"
        :model="formModel"
        :rules="ruleValidate"
        :label-width="'150px'"
      >
        <el-form-item label="公司" prop="companyId">
          <el-select-tree
          :props="props"
          :options="companyResult"
          v-model="formModel.companyId"
          size=""
          height="200"
        ></el-select-tree>
        </el-form-item>
        <el-form-item
          label="类型"
          prop="type"
        >
          <el-select
          v-model="formModel.type"
          filterable
          placeholder="请选择"
          style="width: 200px"
        >
          <el-option
            v-for="type in typeResult"
            :key="type.value"
            :label="type.name"
            :value="type.value"
          ></el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="审批层级" prop="level">
          <el-input-number
            v-model="formModel.level"
            placeholder="请输入审批需要的层级"
            style="width: 200px"
            :min="0" :max="99"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="申请对应天数" prop="days">
          <el-input-number
            v-model="formModel.days"
            placeholder="请输入申请时对应的天数"
            style="width: 200px"
            :min="0" :max="99"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark">
          <el-input
            v-model="formModel.remark"
            placeholder="请输入备注信息"
            style="width: 400px"
          ></el-input>
        </el-form-item>
      </el-form>
    </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 approvalConfigApi from "@/api/base/approvalConfig";
import companyInfoApi from "@/api/base/companyInfo";
import dataDictionaryApi from "@/api/sys/dataDictionary";
import SelectTree from "@/components/SelectTree";

export default {
  props: ["businessKey", "title"],
  components: {
    "el-select-tree": SelectTree
  },
  data() {
    return {
      formModel: {},
      ruleValidate: {
        companyId: [
          { required: true, message: "请选择单位", trigger: "blur" },
        ],
        type: [
          {
            required: true,
            message:
              "请选择类型",
            trigger: "blur",
          },
        ],
        level: [
          {
            required: true,
            message: "审批需要的层级不能为空",
            trigger: "blur",
          },
        ],
        days: [
          {
            required: true,
            message: "申请时对应的天数不能为空",
            trigger: "blur",
          },
        ],
      },
      showDialog: true,
      loading: false,
      submitting: false,
      companyResult: [],
      treeData: [],
      props: {
        // 配置项（必选）
        value: "id",
        label: "name",
        children: "children",
      },
      typeResult: [],
    };
  },
  created() {
    var self = this;
    companyInfoApi.list().then(function (response) {
      var jsonData = response.data;
      if (jsonData.result) {
        if (jsonData.data != null && jsonData.data != "") {
          self.companyResult = jsonData.data;
        }
      }
    });

    dataDictionaryApi
      .findByCatalogName({
        catalogName: "审批层级类型",
      })
      .then((response) => {
        var jsonData = response.data;
        this.typeResult = 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;

            if (id == null || id.length == 0) {
              return approvalConfigApi.add(self.formModel);
            } else {
              return approvalConfigApi.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);
            }
          });
        }
      });
    },
  },
  mounted: function () {
    var self = this;

    (function () {
      if (self.businessKey.length == 0) {
        return approvalConfigApi.create();
      } else {
        return approvalConfigApi.edit(self.businessKey);
      }
    })()
      .then((response) => {
        var jsonData = response.data;
        self.loading = false;

        if (jsonData.result) {
          self.formModel = jsonData.data;
        } else {
          self.$message.error(jsonData.message + "");
        }
      })
      .catch((error) => {
        self.$message.error(error + "");
      });
  },
};
</script>