<template>
  <div class="box-center">
    <!-- 头 -->
    <div class="app-top">
      <div class="app-titel">
        <div
          v-if="orgType != 3"
          class="label"
          style="color: #32363d; font-weight: 400; font-size: 16px"
        >
          楼盘选择：
        </div>
        <div v-if="orgType != 3">
          <el-select
            v-model="houseId"
            @change="houseChange"
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="item in houseList"
              :key="item.id"
              :label="item.propertyName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
        <div style="margin-left: 20px">
          <el-button
            v-if="cus_sal_add"
            @click="infoadd()"
            type="primary"
            >新增</el-button
          >
        </div>
      </div>
    </div>
    <!-- 表格 -->
    <div class="cen-tab">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column prop="houseName" label="楼盘名称" align="center">
        </el-table-column>
        <el-table-column prop="stageName" label="阶段名称" align="center">
        </el-table-column>
        <el-table-column prop="sort" label="顺序" align="center">
        </el-table-column>
        <el-table-column prop="remarks" label="备注" align="center">
        </el-table-column>

        <el-table-column label="操作" align="center" width="150">
          <template slot-scope="{ row }">
            <el-button type="text" v-if="cus_sal_edit" @click="edit(row)">编辑</el-button>
            <el-button type="text" v-if="cus_sal_del" @click="toinifodelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog :title="editFlag==true?'编辑阶段':'新增阶段'" :visible.sync="dialogVisible" @close="clear">
      <el-form
        :model="ruleForm"
        label-position="labelPosition"
        :rules="rules"
        ref="ruleForm"
        label-width="140px"
        style="width: 60%; margin: 0 auto"
      >
        <el-form-item label="阶段名称：" prop="stageName">
          <el-input v-model="ruleForm.stageName" maxlength="10" clearable=""></el-input>
        </el-form-item>
        <el-form-item label="顺序：" prop="sort">
          <el-input
           maxlength="5"
            v-model="ruleForm.sort"
            onkeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)));"
          ></el-input>
        </el-form-item>
        <el-form-item label="备注：">
          <el-input v-model="ruleForm.remarks" type="textarea" maxlength="50" show-word-limit></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSure()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      houseId: "",
      houseList: [],
      orgType: localStorage.getItem("orgType"),
      ruleForm: {
        stageName: "", // 公司名称
        sort: "", // 联系人
        remarks: "", // 联系人手机号
      },
      rules: {
        stageName: [
          { required: true, message: "请输入阶段名称：", trigger: "blur" },
        ],
        sort: [{ required: true, message: "请输入顺序", trigger: "blur" }],
      },
      tableData: [],
      dialogVisible: false,
      editFlag: false,
      options: [],
      value: "",
    };
  },
computed: {
    ...mapGetters(["permissions"]),
  },
  created() {
    this.cus_sal_add = this.permissions["cus_sal_add"];
    this.cus_sal_edit = this.permissions["cus_sal_edit"];
    this.cus_sal_del = this.permissions["cus_sal_del"];
  },
  mounted() {
    // 获取楼盘列表
    this.zkhousePage();
  },
  methods: {
    houseChange() {
      this.findbypage();
    },
    clear() {
      this.ruleForm = {
        stageName: "", // 公司名称
        sort: "", // 联系人
        remarks: "", // 联系人手机号
      };
      this.$refs.ruleForm.resetFields()
    },
    zkhousePage() {
      this.$api.api
        .findHouseByUser({
          orgType: localStorage.getItem("orgType"),
        })
        .then((res) => {
          // console.log(res)
          this.houseList = res.data;
          if (localStorage.getItem("orgType") == 3) {
            this.houseId = localStorage.getItem("houseId");
          } else {
            this.houseId = res.data[0].id;
          }
          // 获取列表
          this.findbypage();
        });
    },
    findbypage() {
      let obj = {
        houseId: this.houseId,
      };
      this.$api.api.lifeTrackDefineList(obj).then((res) => {
        this.tableData = res.data;
      });
    },
    //删除
    toinifodelete(row) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          axios({
            url: `/autoSR/lifeTrackDefine/del`,
            method: "post",
            data: {
              id: row.id,
            },
          }).then((res) => {
            if (res.code == 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.findbypage();
            } else {
              this.$message.error(res.message);
            }
          });
        })
        .catch(() => {

        });
    },
    //新增
    infoadd() {
      this.editFlag = false;
      this.dialogVisible = true;
    },
    edit(row) {
      console.log(row);
      // console.log(this.time)
      this.ruleForm = Object.assign({}, row);
      // 获取地区选择数据,在这里对回显的时候进行操作，首先先获取一级省的数据
      this.editFlag = true;
      this.dialogVisible = true;
    },
    addSure() {
      // console.log(this.addressOptions)
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          console.log(this.ruleForm);
          // 编辑
          if (this.editFlag) {
            console.log("编辑");
            this.$api.api.lifeTrackDefineUpdate(this.ruleForm).then((res) => {
              console.log(res);
              if (res.code == 0) {
                this.dialogVisible = false;
                this.$message.success("编辑成功");
                this.findbypage();
              }
            });
          } else {
            // 添加
            console.log("添加");
            this.$api.api
              .lifeTrackDefineAdd({ houseId: this.houseId, ...this.ruleForm })
              .then((res) => {
                console.log(res);
                if (res.code == 0) {
                  this.dialogVisible = false;
                  this.$message.success("新增成功");
                  this.findbypage();
                }
              });
          }
        }
      });
    },
  },
};
</script>

<style scoped="scoped" lang="scss" >
.box-center {
  width: 100%;
  padding: 15px;
  min-width: 1000px;
  padding-bottom: 100px;
}
.cen-tab {
  width: 100%;
  padding: 15px;
  min-height: 400px;
  background: #ffffff;
  margin-top: 15px;
}
.tophove {
  color: #ffffff;
  background: #2671e2;
}
.app-top {
  width: 100%;
  background: #ffffff;
  box-shadow: 0px 0px 12px 0px rgba(0, 0, 0, 0.04);
  border-radius: 4px;
  padding-top: 15px;
  padding-bottom: 15px;
  .app-titel {
    width: 100%;
    display: flex;
    align-items: center;
    .label {
      font-size: 14px;
      font-weight: 400;
      color: #32363d;
      line-height: 14px;
      margin-left: 15px;
    }
    .toptimeqhuan {
      height: 30px;
      background: #ffffff;
      display: flex;
      align-items: center;
    }
    .toptimeqhuan div {
      padding-left: 20px;
      padding-right: 20px;
      text-align: center;
      line-height: 30px;
      font-size: 14px;
      margin-right: 15px;
      border-radius: 4px;
      border: 1px solid #e0e0e0;
    }
  }
}
</style>
