<template>
  <el-dialog
    title="详情"
    :visible.sync="open"
    width="90%"
    :close-on-click-modal="false"
    @close="handleClose"
  >
    <div class="formContent">
      <com-form ref="myForm" :form="form" :rules="rules" :formList="formList">
        <template #orgNo>
          <treeselect
            v-model="form.orgNo"
            :options="deptOptions"
            :show-count="true"
            placeholder="请选择所属单位"
            :normalizer="unitNamenormalizer"
          />
        </template>
        <template #autogenerationSwitch>
          <el-switch
            :active-value="0"
            :inactive-value="1"
            v-model="form.autogenerationSwitch"
            active-text="开启"
            inactive-text="关闭"
          >
          </el-switch>
        </template>
      </com-form>
    </div>
    <span slot="footer">
      <el-button @click="open = false">取 消</el-button>
      <el-button type="primary" :loading="btnLoading" @click="handleSubmit"
        >提交</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import { deptTreeSelect } from "@/api/system/user";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {
  formList,
  addRules,
  editFormList,
  editRules,
} from "../utils/index.json";
import { adsCzxtReplenishmentPeriodDsave } from "@/api/home";
export default {
  props: {
    getData: Function,
  },
  components: { Treeselect },
  data() {
    return {
      deptOptions: [],
      orgNo: undefined,
      form: {
        orgNo: undefined,
        orgName: "",
        periodDay: "",
        autogenerationSwitch: 0,
      },
      formList: [],
      open: false,
      btnLoading: false,
      rules: {},
      type: "",
    };
  },
  methods: {
    getOneArr(data) {
      let newData = [];
      const callback = (item) => {
        (item.children || (item.children = [])).map((v) => {
          callback(v);
        });
        delete item.children;
        newData.push(item);
      };
      data.map((v) => callback(v));
      return newData;
    },
    unitNamenormalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.orgNo,
        label: node.label,
      };
    },
    /** 查询部门下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then((response) => {
        this.deptOptions = response.data;
      });
    },
    handleClick({ type }) {
      this.rules = type === "add" ? addRules : editRules;
      this.formList = type === "add" ? formList : editFormList;
      this.type = type;
      this.getDeptTree();
      this.open = true;
    },
    handleClose() {
      this.open = false;
    },
    handleSubmit() {
      this.$refs.myForm.$refs.form.validate((valid) => {
        if (valid) {
          if (this.type === "add") {
            let arr = JSON.parse(JSON.stringify(this.deptOptions));
            let temp = this.getOneArr(arr).find(
              (o) => o.orgNo === this.form.orgNo
            );
            this.form.orgName = (temp && temp.label) || "";
            this.btnLoading = true;
            adsCzxtReplenishmentPeriodDsave(this.form).then((res) => {
              if (res && res.code == 200) {
                this.open = false;
                this.btnLoading = false;
                this.$message.success("操作成功");
                this.getData();
              } else {
                this.btnLoading = false;
                this.$message.warning(res.msg || "操作失败");
              }
            });
          }
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
// .formContent {
//   height: 600px;
// }
::v-deep {
  .el-dialog__footer {
    text-align: center;
  }
}
</style>
