<template>
  <el-dialog
    v-loading="loading"
    :title="title"
    :center="true"
    align="left"
    top="10vh"
    width="750px"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    @close="showModelThis = false"
  >
    <el-card style="margin-top: -20px">
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form
            ref="ruleForm"
            :model="ruleForm"
            status-icon
            :size="'mini'"
            :rules="rules"
            label-width="80px"
            class="demo-ruleForm"
          >
            <el-row style="margin-bottom:20px">
              <el-col :span="24">
                <el-form-item label="功能编号" prop="action_code">
                  <el-input
                    v-model="ruleForm.action_code"
                    :placeholder="'功能编号'"
                    maxlength="11"
                    :disabled="isDetail && id !== 'add'"
                  />
                </el-form-item>
              </el-col>

              <el-col :span="24">
                <el-form-item label="功能名称" prop="action_name">
                  <el-input
                    v-model="ruleForm.action_name"
                    :placeholder="'功能名称'"
                    :disabled="isDetail && id !== 'add'"
                    maxlength="100"
                  />
                </el-form-item>
              </el-col>


              <el-col :span="24">
                <el-form-item label="功能描述" prop="action_desc">
                  <el-input 
                    size="mini"
                    style="width:100%" 
                    type="textarea"
                    :disabled="isDetail &&id !== 'add'" 
                    v-model="ruleForm.action_desc" 
                    placeholder="功能描述"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-col>
        <el-col :span="6" class="bottom-btn">
          <el-button v-if="!isDetail ||  id === 'add'" type="primary" @click="submitForm" :size="'mini'"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false" :size="'mini'">{{
            isDetail && id !== 'add' ? "关 闭" : "取 消"
          }}</el-button>
        </el-col>
      </el-row>
    </el-card>
  </el-dialog>
</template>
<script>
import asyncRequest from "@/apis/service/system/button";
import resToken from "@/mixins/resToken";
/**
 * @props 
 *  action_name 功能名称
 *  action_code 功能编号
 *  action_desc 功能描述
*/

export default {
  name: "Account",
  props: ["showModel", "sitem" ,"id", "isDetail"],
  mixins: [resToken],
  data() {
    return {
      roleList: [],
      loading: false,
      title: "添加账号",
      organizeList: [],
      showModelThis: this.showModel,
      coptions: [],
      is_mainoptions: [],
      isIndeterminate: false,
      ruleForm: {
        action_name: "", // 功能名称
        action_code: "", //功能编号
        action_desc:"",
        role_id: "", //角色id
      },
      platformoptions: [],
      rules: {
        action_name: [
          {
            required: true,
            trigger: "blur",
            message:"请输入按钮编号"
          },
        ],
        action_code: [
          {
            required: true,
            trigger: "blur",
            message:"请输入按钮名称"
          },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  mounted() {},
  methods: {
    itemidChange(e) {
      this.ruleForm.itemid = e;
      this.$refs.ruleForm.validateField("itemid");
    },
    async initForm() {
      this.loading = true;

      await this.resetForm();
      if (this.id === "add") {
        this.title = "添加功能";
        this.rulesThis = this.rules;
      } else {
        if (this.isDetail) {
          this.title = "功能详情";
          this.rulesThis = {};
        } else {
          this.title = "修改功能";
          this.rulesThis = this.rules;
        }
      }
      this.loading = false;
    },

    async getClist() {
      this.organizeList = [];
      const { code, data } = await asyncRequest.getClist({
        size: 10000,
      });
      if (code === 1) {
        this.organizeList = data;
        this.recursion(this.organizeList);
      }
    },
    handleCheckedCitiesChange(value) {
      console.log(value);
      let checkedCount = value.length;
      this.ruleForm.is_all = checkedCount === this.coptions.length;
      this.isIndeterminate = checkedCount > 0 && checkedCount < this.coptions.length;
    },
    recursion(list) {
      list.map((v) => {
        if (v && Array.isArray(v.child)) {
          v.value = v.id + "";
          v.label = v.name;
          if (v.child.length === 0) {
            delete v["child"];
          } else {
            this.recursion(v.child);
          }
        }
        return v;
      });
    },
    async getRole() {
      const model = {
        status: "", // 状态
        action_desc: "", // 姓名
        role_name: "",
      };
      const res = await asyncRequest.getRole(model);
      if (res && res.code === 0 && res.data) {
        this.roleList = res.data;
        this.roleList.map((v1) => {
          v1.id += "";
          v1.status += "";
          return v1;
        });
      }
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          if (this.sitem) {
            const {
              action_name,
              action_code,
              action_desc,
              id
            } = this.sitem;

            this.ruleForm = {
              id,
              action_name: action_name || "",
              action_code: action_code || "",
              action_desc: action_desc || ""
            };

          } else {
            this.ruleForm = {
              action_desc:"",
              action_name: "", // 功能名称
              action_code: ""
            };
          }
        }
      });
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (!this.loading) {
            this.loading = true;
            const {
              action_name,
              action_code,
              action_desc,
              id
            } = JSON.parse(JSON.stringify(this.ruleForm));


            const model = {
              action_name,
              action_code,
              action_desc,
              id
            };

            let res = {};
            if (this.id === "add") {
              delete model["id"];
              res = await asyncRequest.add(model);
            } else {
              res = await asyncRequest.update(model);
            }
            this.loading = false;
            if (res && res.code === 1) {
              const title = this.id === "add" ? "添加成功" : "修改成功";
              this.$notify.success({
                title,
                message: "",
              });
              this.showModelThis = false;
              // 刷新
              this.$emit("refresh");
            }
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.account {
  .bottom-btn {
    position: absolute;
    bottom: 0px;
    right: 0;
    text-align: right;
    z-index: 2;
  }
}
</style>
