<template>
  <div>
    <el-card>
      <el-form :inline="true" :model="query" class="query-form" size="mini">
        <el-form-item class="query-form-item" label="名称:">
          <el-input
            v-model="query.name"
            clearable
            placeholder="请输入名称"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button-group style="margin-left: 10px;display:flex;">
            <el-button type="primary" icon="el-icon-refresh" @click="onReset">重置</el-button>
            <el-button type="primary" icon="el-icon-search" @click="onSubmit">查询</el-button>
            <el-button type="primary" icon="el-icon-plus" @click="handleSubForm( null, 'add')">新增</el-button>
          </el-button-group>
        </el-form-item>
      </el-form>
      <el-table
        :data="list"
        border
        highlight-current-row
        row-key="id"
        :default-expand-all="isExpandAll"
        :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      >
        <el-table-column label="序号" type="index" ref="dataForm"></el-table-column>
        <el-table-column prop="name" label="名称"></el-table-column>
        <el-table-column prop="code" label="编码"></el-table-column>
        <el-table-column label="操作" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="text" v-if="scope.row.code!=10000" @click.native.stop="handleSubForm( scope.row, 'edit')">编辑</el-button>
            <el-button type="text" v-if="scope.row.code!=10000" @click.native.stop="handleDelInv(scope.row)">删除</el-button>
            <el-button type="text" v-if="scope.row.code!=10000"  @click.native.stop="addHospProduct( scope.row)">绑定</el-button>
            <el-button type="text" v-if="scope.row.code!=10000"   @click.native.stop="removeHospProduct(scope.row)">解绑</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog
        :title="formMap[formName]"
        append-to-body
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="subFormVisible"
        width="60%"
        top="5vh"
      >
        <el-form :model="subData" ref="dataForm" :rules="formRules" label-width="80px">
          <el-row :gutter="20" class="el-row" type="flex">
            <el-col :span="12" class="el-col">
              <el-form-item label="上级菜单" prop="parentCode">
                <treeselect
                  v-model="subData.parentCode"
                  :options="fromDeptOptions"
                  :normalizer="normalizer"
                  :show-count="true"
                  placeholder="选择上级菜单"
                />
              </el-form-item>
            </el-col>
            <el-col :span="12" class="el-col">
            <el-form-item label="名称" prop="name" class="query-form-item">
              <el-input
                v-model="subData.name" style="width: 80%"
                placeholder="请输入名称"
                auto-complete="off"
              ></el-input>
            </el-form-item>
            </el-col>
          </el-row>
          <el-row class="el-row" type="flex">
            <el-col class="el-col">
              <el-form-item label="备注" prop="remark" class="query-form-item">
                <el-input
                  v-model="subData.remark"
                  type="textarea"
                  style="width: 90%"
                  auto-complete="off"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click.native="hideForm">取消</el-button>
          <el-button
            type="primary"
            @click.native="forInvSubmit()"
          >提交
          </el-button
          >
        </div>
      </el-dialog>
    </el-card>
    <el-dialog
      title="绑定-耗材产品"
      :visible.sync="selectHospProductVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="80%"
      append-to-body
      v-if="selectHospProductVisible"
    >
      <selectHospProduct
        :closeDialog="closeDialog"
        :rId="rId"
      ></selectHospProduct>
    </el-dialog>

    <el-dialog
      title="解绑-耗材产品"
      :visible.sync="selectUnBindHospProductVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      width="80%"
      append-to-body
      v-if="selectUnBindHospProductVisible"
    >
      <selectUnBindHospProduct
        :closeDialog="closeDialog"
        :rId="rId"
      ></selectUnBindHospProduct>
    </el-dialog>

  </div>
</template>

<script>
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import Treeselect from "@riophae/vue-treeselect";
import {getBasicHospType, getListMenu, saveBasicHospType,uodateBasicHospType,deleteBasicHospType} from "@/api/basic/basicHospType";

import selectHospProduct from "@/views/basic/product/bindHospProduct";
import selectUnBindHospProduct from "@/views/basic/product/unbindHospProduct";


export default {
  data() {
    return {
      //---------------仓库相关-------------------
      query: {
        name: null,
        code: null,
        status: "1",
        // page: 1,
        // limit: 10,
      },
      isShow: null,
      loading: true,
      list: [],
      isExpandAll: true,
      total: 0,
      formMap: {
        add: "新增",
        edit: "编辑"
      },
      formName: null,
      configParms: {},
      currentCode: null,
      currentRow: {name: "仓库"},
      mutiInvMode: true,
      subFormVisible: false,
      rId:null,
      subData: {},
      fromDeptOptions: [],
      thirdSubQuery: {
        parentId: null,
        name: null,
        page: 1,
        limit: 10,
        thirdSysFk: "thirdId",
      },
      thrSubWareHouseVisible: false,
      thrSubWarehouseData: [],
      checkSubThrWarehouseRow: null,
      selectHospProductVisible: false,
      selectUnBindHospProductVisible:false,

      subTotal: 0,
      sysList: [],
      sysSubList: [],
    };
  },
  methods: {
    //----------------仓库相关-----------------
    onReset() {   //重置
      this.$router.push({
        path: "",
      });
      this.query = {
        name: "",
        status: "",
      };
      this.getList();
    },

    onSubmit() { //提交查询
      this.getList();
    },

    getList() {
      this.loading = true;
      getBasicHospType(this.query)
        .then((response) => {
          this.loading = false;
          let invlist = response.data.list || [];
          this.list = this.handleTree(invlist, "code", "parentCode");
        })
        .catch(() => {
          this.loading = false;
          this.total = 0;
          this.list = [];
        });
    },
    handleSubForm(data, formName) {   //新增，编辑
      this.formName = formName;
      if (formName === "edit") {
        this.subData = JSON.parse(JSON.stringify(data));
      } else if (formName === "add") {
        this.subData = {advanceType: false,};
      }
      this.getTreeselect();
      this.subFormVisible = true;
    },

    hideForm() {    // 新增，编辑---取消
      this.formVisible = false;
      this.subFormVisible = false;
      this.resetForm();
      return true;
    },
    // 刷新表单
    resetForm() {
      if (this.$refs["dataForm"]) {
        // 清空验证信息表单
        this.$refs["dataForm"].clearValidate();
        // 刷新表单
        this.$refs["dataForm"].resetFields();
        this.getList();
      }
    },
    forInvSubmit() {    // 新增，编辑---提交

      if(this.subData.parentCode==null){
        this.$message.error("上级菜单不能为空");
        return
      }
      if(this.subData.name==null){
        this.$message.error("名称不能为空！");
        return
      }
          if(this.subData.id!=null){
            uodateBasicHospType(this.subData).then((response) => {
              if (response.code == 20000) {
                this.subFormVisible = false;
                this.getList();
              } else {
                this.$message.error(response.message);
                this.subFormVisible = false;
              }
            }).catch(() => {
              this.subFormVisible = false;
            });
          }else{
            saveBasicHospType(this.subData).then((response) => {
              if (response.code == 20000) {
                this.subFormVisible = false;
                this.getList();
              } else {
                this.$message.error(response.message);
                this.subFormVisible = false;
              }
            }).catch(() => {
              this.subFormVisible = false;
            });
          }
    },

    /** 查询菜单下拉树结构 */
    getTreeselect() {
      getListMenu().then(response => {
        this.fromDeptOptions = [];
        const menu = {id: 0, name: '主类目', children: []};
        menu.children = this.handleTree(response.data,"code", "parentCode");
        this.fromDeptOptions.push(menu);
      });
    },
    /** 转换菜单数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.code,
        label: node.name,
        children: node.children
      };
    },
    handleDelInv(row) {
      if (row.id) {
        this.$confirm("确认删除该节点吗?", "提示", {
          type: "warning",
        })
          .then(() => {
            this.deleteLoading = true;
            let para = {id: row.id,code:row.code};
            deleteBasicHospType(para)
              .then((response) => {
                this.deleteLoading = false;
                if (response.code !== 20000) {
                  this.$message.error(response.message);
                  return false;
                }
                this.getList();
                this.$message.success("删除成功");
              })
              .catch(() => {
                this.deleteLoading = false;
              });
          })
          .catch(() => {
            this.$message.info("取消删除");
          });
      }
    },
    addHospProduct(data) {
      this.rId = data.code
      this.selectHospProductVisible = true;
    },
    removeHospProduct(data){
      this.rId = data.code
      this.selectUnBindHospProductVisible = true;
    },
    closeDialog() {
      this.selectErpDialogVisible = false;
      this.selectLocalVisible = false;
      this.selectVersionVisible = false;
      this.selectHospProductVisible=false;
      this.selectUnBindHospProductVisible=false;
    },
  },
  mounted() {
  },
  components: {
    Treeselect,
    selectHospProduct,
    selectUnBindHospProduct
  },
  created() {
    this.getList();
  }
};
</script>

<style scoped>

.vue-treeselect >>> .vue-treeselect__placeholder {
  font-size: 12px;
}

.vue-treeselect >>> .vue-treeselect__value-container {
  font-size: 12px;
  height: 30px;
}

.vue-treeselect >>> .vue-treeselect__control {
  height: 20px;
}

</style>
