<!--
 * @Author: Do not edit
 * @Date: 2023-06-20 11:16:51
 * @LastEditTime: 2023-06-27 14:46:10
 * @LastEditors: DY
 * @Description: 要求清单
-->
<template>
  <el-card shadow="never" class="aui-card--fill">
    <div style="padding: 20px 30px">
      <el-row :gutter="20">
        <el-col :span="6">
          <h2>要求分组</h2>
          <div v-for="(item, index) in groupList" :key="index">
            <el-button style="width: 100px; text-align: left" @click="searchByGroup(item)">{{ item.name }}</el-button>
          </div>
        </el-col>
        <el-col :span="18">
          <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item>
              <el-input v-model="formInline.requirementName" placeholder="标题"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSubmit">查询</el-button>
            </el-form-item>
          </el-form>
        <div class="bigDiv">
          <div class="detailDiv" v-for="(item, index) in dataList" :key="index">
            <p>{{ item.title }}</p>
            <el-row :gutter="20">
              <el-col :span="4" :offset="20">
                <i class="el-icon-delete" @click="deleteRequire(item.id, item.title)"></i>
              </el-col>
            </el-row>
          </div>
        </div>
        <div>
          <el-button class="carcleButton" @click="addRequire">+</el-button>
        </div>
        </el-col>
      </el-row>
      <div v-if="addtModelVisible" style="height: 400px; width: 40%; margin: 20px auto; border: 1px solid grey; padding: 20px">
        <h2>要求编辑</h2>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="80px" class="demo-ruleForm">
            <el-form-item label="标题" prop="title">
              <el-input v-model="ruleForm.title"></el-input>
            </el-form-item>
            <el-form-item label="描述" prop="description">
              <el-input type="textarea" v-model="ruleForm.description"></el-input>
            </el-form-item>
            <el-form-item label="所属分组" prop="requirementListGroupId">
              <el-select v-model="ruleForm.requirementListGroupId" placeholder="请选择">
                <el-option
                  v-for="item in groupList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item>
              <!-- <el-button type="primary" @click="submitForm('ruleForm')">保存</el-button> -->
              <el-button @click="dialogVisible = false">取 消</el-button>
              <el-button type="primary" @click="saveRequirement">保 存</el-button>
            </el-form-item>
          </el-form>
      </div>
    </div>
  </el-card>
</template>

<script>
export default {
  data() {
    return {
      urlOptions: {
        addtURL: "/supplier/qmsSupplierRequirementList",
        getGourpListRUL: '/supplier/qmsSupplierRequirementListGroup/page',
        getRequirementListURL: '/supplier/qmsSupplierRequirementList/page',
        deleteURL: 'supplier/qmsSupplierRequirementList'
      },
      formInline: {
        groupId: '',
        limit: 999,
        page: 1,
        requirementName: ''
      },
      addtModelVisible: false,
      dataListLoading: false,
      dataList: [],
      ruleForm: {
        title: '',
        description: '',
        requirementListGroupId: ''
      },
      options: [],
      groupList: [],
      rules: {
        title: [
          { required: true, message: '请输入code', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.getGroup()
  },
  methods: {
    deleteRequire(id, name) {
      this.$confirm(`确定对[标题=${name}]进行删除操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.delete(this.urlOptions.deleteURL, { data: [id] }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.onSubmit();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => { });
    },
    saveRequirement() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          this.$http["post"](this.urlOptions.addtURL, this.ruleForm)
          .then(({ data: res }) => {
            if (res.code !== 0) {
              return this.$message.error(res.msg);
            }
            this.$message({
              message: this.$t("prompt.success"),
              type: "success",
              duration: 500,
              onClose: () => {
                console.log(1111);
                this.formInline.groupId = this.ruleForm.requirementListGroupId
                this.addtModelVisible = false
                this.onSubmit()
              },
            })
          })
        } else {
          console.log('error submit!!')
          return false;
        }
      });
    },
    searchByGroup(group) {
      console.log('查询', group)
      this.formInline.groupId = group.id
    },
    getGroup() {
      this.$http
        .get(this.urlOptions.getGourpListRUL, {
          params: {
            limit: 999,
            page: 1
          }
        })
        .then(({ data: res }) => {
          this.dataListLoading = false;
          if (res.code !== 0) {
            this.groupList = [];
            return this.$message.error(res.msg);
          }
          this.groupList = res.data.list;
        })
        .catch(() => {
          this.dataListLoading = false;
        });
    },
    addRequire() {
      this.addtModelVisible = true
      this.ruleForm = {
        title: '',
        description: '',
        requirementListGroupId: ''
      }
    },
    onSubmit() {
      console.log('submit!');
      if (this.formInline.groupId === '') {
        this.$message.error('请选择要求分组')
      } else {
        this.$http
          .get(this.urlOptions.getRequirementListURL, {
            params: this.formInline
          })
          .then(({ data: res }) => {
            this.dataListLoading = false;
            if (res.code !== 0) {
              this.dataList = [];
              return this.$message.error(res.msg);
            }
            this.dataList = res.data.list;
          })
          .catch(() => {
            this.dataListLoading = false;
          });
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .bigDiv {
    border: 1px solid grey;
    padding: 20px;
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
  }
  .detailDiv {
    border: 1px solid grey;
    padding: 30px;
    width: 40%;
    height: 120px;
    margin-bottom: 10px;
  }
  .carcleButton {
    border-radius: 50%;
    width: 50px;
    height: 50px;
    margin: 5px;
    background-color: rgb(196, 193, 193);
  }
</style>
