<template>

  <div class="box-center">
    <!-- 头 -->
    <div class="app-top">
      <div class="app-titel" style="margin-top: 5px">
        <div class="label" v-if="orgType!=3">楼盘：</div>
        <div v-if="orgType!=3">
          <el-select
            v-model="projectId"
            filterable
            @change="houseChange"
            placeholder="请选择"

          >
            <el-option
              v-for="item in houseList"
              :key="item.id"
              :label="item.propertyName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
        <div class="label">状态：</div>
        <div>
          <el-select
            v-model="statetest"
             clearable
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="item in stateList"
              :key="item.id"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </div>
        <div style="margin-left: 20px">
          <el-button  @click="Screening()" type="primary">筛选</el-button>
        </div>
         <div style="margin-left: 20px">
          <el-button v-if="tem_pwl_add" @click="editorinfo()" type="primary"
            >添加模板</el-button
          >
        </div>
        <!-- <div style="margin-left: auto;margin-right:10px;" v-if="tem_pwl_downLoad">
          <el-button @click="downLoad">导出</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="name"
            label="状态"
             align="center"
           >
            <template slot-scope="scope">
                <p v-if="scope.row.status==1">停用</p>
                <p v-if="scope.row.status==0">启用</p>
            </template>
          </el-table-column>
          <el-table-column
            prop="count"
            label="大类个数"
             align="center">
          </el-table-column>

          <el-table-column
            prop="createTime"
            label="创建时间"
            align="center"
          >
          </el-table-column>
         <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="text" size="small" v-if="scope.row.status==1&&tem_pwl_stop" @click="clickstateis(scope.row)">启用</el-button>
              <el-button type="text" size="small" v-if="scope.row.status==0&&tem_pwl_stop" @click="clickstateno(scope.row)">停用</el-button>
              <el-button type="text" size="small" v-if="tem_pwl_edit" @click="clickbianji(scope.row)">编辑</el-button>
              <el-button type="text" size="small"  v-if="tem_pwl_del" @click="templatedel(scope.row)">删除</el-button>
              <el-button type="text" size="small" @click="goinfo(scope.row)">查看</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div style="display: flex;justify-content:flex-end;margin-top: 10px;">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pageNum"
              :page-sizes="[ 10, 30, 50]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
            </el-pagination>
        </div>
    </div>
     <el-dialog title="查看" :visible.sync="dialogFormVisible">
       <div style="height: 400px;overflow-y: auto;width:80%;margin: 0 auto;">
          <div style="border: 2px dashed #CCCCCC;margin-top: 10px;padding:6px;border-radius: 6px;font-size: 14px;"
            v-for="(item,index) in newlist" :key="index">
            <div style="display: flex;align-items: center;height: 30px;">
            <div >{{item.name||''}}：{{item.fraction}}分</div>
              </div>
              <div class="erji" style="margin-left: 30px;" v-for="(asd,idx) in item.children" :key="idx">
              <div style="display: flex;align-items: center;height: 30px;">
                <div>{{asd.name||''}}：{{asd.fraction}}分</div>
              <div>
              </div>
                </div>
                <div class="sanji" style="width:90%;" >
                <div style="display: flex;align-items: center;height: 30px;" v-for="(sanji,indx) in asd.children" :key="indx">
                <div style="width:85% ;margin-left: 30px;">{{sanji.name||''}}</div>
                </div>
                </div>
              </div>
          </div>
       </div>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
        </div>
      </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { exportMethodPost} from "@/util/util";
export default {
  data() {
    return {
      tableData: [],
      currentPage4:1,
      total:0,//总条数
      pageNum:1,
      pageSize:10,
      houseList:[],
      projectId:"",
      orgType:localStorage.getItem('orgType'),
      stateList:[
        {label:'停用',value:'1'},
        {label:'启用',value:'0'},
      ],
      statetest:'',
      dialogFormVisible:false,
      nodelist:[],
      newlist:[]
    };
  },
      computed: {
    ...mapGetters(["permissions"]),
  },
  created() {
    this.tem_pwl_add = this.permissions["tem_pwl_add"];
    this.tem_pwl_edit = this.permissions["tem_pwl_edit"];
    this.tem_pwl_stop = this.permissions["tem_pwl_stop"];
    this.tem_pwl_del = this.permissions["tem_pwl_del"];
    this.tem_pwl_downLoad = this.permissions["tem_pwl_downLoad"];
  },
  mounted() {
    this.zkhousePage()
  },
  methods: {
    downLoad(){
      let parmest={
          houseId:this.projectId,
          status:this.statetest
      }

    },
    goinfo(row){
      this.nodelist=[]
      this.newlist=[]
      this.dialogFormVisible=true;
      this.$api.http.templatedelfindByTempId({tempId:row.id,}).then((res) => {
          this.nodelist = res.data.list;
          this.listCheckedchaifen()
      })
    },
    listCheckedchaifen() {
      let arr = [];
      let level2 = [];
      this.nodelist.forEach((item, index) => {
        if (item.pid == 0) {
          item.children = [];
          arr.push(item);
        } else {
          level2.push(item);
        }
      });
      let resultArr = []; //去重后的数组
      var flag;
      for (var i in arr) {
        flag = true;
        for (var j in resultArr) {
          if (resultArr[j].id == arr[i].id) {
            flag = false;
            break;
          }
        }
        if (flag) {
          resultArr.push(arr[i]);
        }
      }
      let resultArrlist = []; //去重后的数组
      var flags;
      for (var i in level2) {
        flags = true;
        for (var j in resultArrlist) {
          if (resultArrlist[j].id == level2[i].id) {
            flags = false;
            break;
          }
        }
        if (flags) {
          resultArrlist.push(level2[i]);
        }
      }
      let level3 = [];
      resultArrlist.forEach((res, index) => {
        resultArr.forEach((asd) => {
          if (res.pid == asd.id) {
            res.children = [];
            asd.children.push(res);
          }
        });
      });
      resultArrlist.forEach((xzd) => {
        resultArr.forEach((asd) => {
          asd.children.forEach((qwe) => {
            if (xzd.pid == qwe.id) {
              qwe.children.push(xzd);
            }
          });
        });
      });
      this.newlist = resultArr;
    },
    //停用
    clickstateno(item){
      this.$confirm('此操作将停用此模板, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.http.templatestartOrStop({
            id:item.id,
            status:1
          }).then((res) => {
              console.log(res);
              if(res.code==0){
                   this.$message({
                    type: 'success',
                    message: '停用成功!'
                  });
                   this.getcompanyList()
              }else{
                 this.$message({
                  type: 'info',
                  message: res.msg
                });
              }
          });
        }).catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消'
          // });
        });
    },
    //启用
    clickstateis(item){
        this.$confirm('此操作将启用此模板, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.http.templatestartOrStop({
            id:item.id,
            status:0
          }).then((res) => {
              console.log(res);
              if(res.code==0){
                   this.$message({
                    type: 'success',
                    message: '启用成功!'
                  });
                  this.getcompanyList()
              }else{
                 this.$message({
                  type: 'info',
                  message: res.msg
                });
              }
          });
        }).catch(() => {
          // this.$message({
          //   type: 'info',
          //   message: '已取消'
          // });
        });
    },
    //筛选
    Screening(){
      this.pageNum=1;
      this.getcompanyList()
    },
     //获取楼盘
     zkhousePage() {
      this.$api.api.findHouseByUser({
        orgType: localStorage.getItem("orgType"),
      }).then((res) => {
          this.houseList = res.data;
          if(localStorage.getItem('orgType')==3){
            this.projectId=localStorage.getItem('houseId')
          }else{
            this.projectId = res.data[0].id;
          }
          // this.projectId = res.data[0].id;
          this.getcompanyList()
        });
    },
    //切换楼盘
    houseChange(){
      this.pageNum=1;
       this.getcompanyList()
    },
    // 获取列表
    getcompanyList() {
      this.tableData=[];
      let parmest={
          current: this.pageNum,
          size: this.pageSize,
          houseId:this.projectId,
          status:this.statetest
      }
      this.$api.http.templatefindByPage(parmest).then((res) => {
          this.tableData=res.data.records;
          this.total=res.data.total
        });
    },
    //templatedel删除模板
    templatedel(item){
     this.$confirm('此操作将删除此模板, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$api.http.templatedel({
            id:item.id,
          }).then((res) => {
              console.log(res);
              if(res.code==0){
                   this.$message({
                    type: 'success',
                    message: '删除成功!'
                  });
                  this.getcompanyList()
              }else{
                 this.$message({
                  type: 'info',
                  message: res.msg
                });
              }
          });
        }).catch(() => {

        });
    },
    handleSizeChange(val) {
      this.pageSize=val;
      this.getcompanyList()
    },
    handleCurrentChange(val) {
      this.pageNum=val;
      this.getcompanyList()
    },


    //編輯
    clickbianji(row){
          let Userrecord={
					id:row.id,
          houseId:this.projectId,
				}
      this.$router.push({ path: "/Template/Pinspeakwords", query: { flag: Userrecord } });
    },
    //添加
     editorinfo(){
        let Userrecord={
					id:'',
          houseId:this.projectId,
				}
      this.$router.push({ path: "/Template/Pinspeakwords", query: { flag: Userrecord } });
    },

  },
};
</script>

<style scoped="scoped" lang="scss" >
  .custom-tree-node {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 14px;
    padding-right: 8px;
  }
.box-center {
  width: 100%;
  padding: 15px;
  min-width: 1000px;
  padding-bottom: 40px;
}
.cen-tab{
  width: 100%;
  padding: 15px;
  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;
      // min-width: 100px;
      text-align: right;
    }
    .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>
