<!--
 * @Descripttion: 
 * @version: 
 * @Author: JC9527
 * @Date: 2023-09-04 10:20:06
 * @LastEditors: JC9527
 * @LastEditTime: 2023-10-10 16:14:06
-->
<template>
    <div class="plan-management">
      <div class="plan-topBox">
        <div class="drup-downs">
          <div class="year">
            <div class="year-span">年月:</div>
            <el-date-picker
              v-model="form.plannedYear"
              type="month"
              format="yyyy-MM"
              :clearable="false"
              value-format="yyyy-MM"
              placeholder="请选择年月">
            </el-date-picker>
          </div>
          <div class="area">
            <!-- <div class="area-span">执法计划情况统计表</div> -->
            <div class="area-span">行政区划:</div>
            <el-cascader
              v-model="form.district"
              :disabled="disabled"
              placeholder="请选择"
              :options="options"
              :props="props"
              @change="handleChange"></el-cascader>
          </div>
          <div class="select-btn reset" @click="reset">重置</div>
        </div>
        <div class="btns">
          <div class="export" @click="exportFile">
            <img src="@/assets/images/export.png" alt="">
            <span>导出excel</span>
          </div>
          <div class="newProject" @click="newProject">
            <img src="@/assets/images/newProject.png" alt="">
            <span>新增计划</span>
          </div>
        </div>
      </div>
      <div class="plan-bottomBox">
        <div class="tables">
          <el-table
            :data="tableData"
            :border="false"
            :height="484"
            v-loading="loading"
            tooltip-effect="light"
            @selection-change="handleSelectionChange"
            row-key="id"
            :row-class-name="tableRowClassName"
          >
            <!-- <el-table-column
              type="selection"
              reserve-selection
              width="55"
              header-align="center"
            >
            </el-table-column> -->
            <el-table-column
              prop="plannedYear"
              label="计划年月"
              width="180"
              header-align="center"
            >
            </el-table-column>
            <el-table-column
              prop="district"
              label="行政区划"
              show-overflow-tooltip
              width="180"
              header-align="center"
            > 
              <template slot-scope="scope">
                <span>{{ componendDistrict(scope.row.district) || '/' }}</span>
              </template>
            </el-table-column>
            <el-table-column
              prop="entprName"
              label="企业名称"
              header-align="center"
            >
            </el-table-column>
            <!-- <el-table-column
              prop="keyCount"
              label="省重点数"
              header-align="center"
            >
            </el-table-column> -->
            <el-table-column
              prop="createTime"
              label="创建时间"
              header-align="center"
            >
            <template slot-scope="scope">
              <span>{{ scope.row.createTime ? scope.row.createTime.split(' ')[0] : scope.row.createTime }}</span>
            </template>
            </el-table-column>
            <el-table-column
              label="操作"
              width="250"
              header-align="center"
            >
              <template slot-scope="scope">
                <div class="tabs-btns">
                  <div class="look" @click="look(scope.row)">
                    <img src="@/assets/images/look.png" alt="">
                    <span>查看</span>
                  </div>
                  <div class="change" @click="change(scope.row)">
                    <img src="@/assets/images/change.png" alt="">
                    <span>修改</span>
                  </div>
                  <div class="delete" @click="deleteItem(scope.row)">
                    <img src="@/assets/images/delete.png" alt="">
                    <span>删除</span>
                  </div>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <!-- <el-table
            v-if="refreshTable"
            v-loading="loading"
            :data="tableData"
            :height="484"
            :border="false"
            tooltip-effect="light"
            row-key="id"
            :default-expand-all="isExpandAll"
            :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
          >
            <el-table-column prop="district" label="行政区划" header-align="center" show-overflow-tooltip></el-table-column>
            <el-table-column prop="amount" label="企业总数" header-align="center"></el-table-column>
            <el-table-column prop="planNumb" label="已计划数" header-align="center"></el-table-column>
            <el-table-column prop="keyCount" label="已计划省重点数" header-align="center"></el-table-column>
            <el-table-column prop="keyCount" label="剩余重点数" header-align="center"></el-table-column>
            <el-table-column label="查看计划明细" align="center" width="200" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <div class="tabs-btns">
                  <div class="look" @click="look(scope.row)">
                    <img src="@/assets/images/look.png" alt="">
                    <span>查看</span>
                  </div>
                  <div class="change" @click="change(scope.row)">
                    <img src="@/assets/images/change.png" alt="">
                    <span>修改</span>
                  </div>
                  <div class="delete" @click="deleteItem(scope.row)">
                    <img src="@/assets/images/delete.png" alt="">
                    <span>删除</span>
                  </div>
                </div>
              </template>
            </el-table-column>
          </el-table> -->
        </div>
        <div class="pagination">
          <my-pagination :total="total" @pagesChange="pagesChange" ref="mypagination"></my-pagination>
        </div>
      </div>
      <my-dialog ref="myDialog" v-on:close="padlockOne"></my-dialog>
      <my-dialogtwo ref="myDialogTwo"></my-dialogtwo>
      <temp-dialog ref="tempDialog" v-on:close="padlock"></temp-dialog>
    </div>
  </template>
  <script>
  import myPagination from "@/views/components/myPagination/index.vue"
  import myDialog from "@/views/components/dialog/index.vue"
  import myDialogtwo from "@/views/components/dialogTwo/index.vue"
  import tempDialog from "@/views/components/tempDialog/index.vue"
  import { Loading } from "element-ui";
  import { mapState } from "vuex";
  export default {
    components:{myPagination,myDialog,myDialogtwo,tempDialog},
    data() {
      return {
        optionsyear:[],
        options:[],
        myDistrict:[],
        form:{
          district:[],
          plannedYear:''
        },
        mydistrict:'',
        props:{
          value:'county',
          label:'institutionName',
          checkStrictly: true
        },
        tableData: [
        //   {
        //   id: 1,
        //   amount: '260',
        //   district: '苏州市',
        //   planNumb: '200',
        //   keyCount:10,
        //   children: [{
        //       id: 31,
        //       amount: '220',
        //       district: '吴中区',
        //       planNumb: '30',
        //       keyCount:10,
        //     }, {
        //       id: 32,
        //       amount: '200',
        //       district: '姑苏区',
        //       planNumb: '20',
        //       keyCount:10,
        //   }]
        // }, {
        //   id: 2,
        //   amount: '330',
        //   district: '木渎镇',
        //   planNumb: '200',
        //   keyCount:10,
        // }, {
        //   id: 256,
        //   amount: '350',
        //   district: '东山镇',
        //   planNumb: '200',
        //   keyCount:10,
        // }, {
        //   id: 257,
        //   amount: '300',
        //   district: '吴江区',
        //   planNumb: '200',
        //   keyCount:10,
        // }
        ],
        pages:{
          pageSize:10,
          pageNum:1
        },
        total:0,
        loading:false,
        disabled:false,
        // 是否展开，默认全部展开
        isExpandAll: true,
        // 重新渲染表格状态
        refreshTable: true,
        county:false,
        multipleSelection:[],
      }
    },
    computed: {
      ...mapState({
        dept: (state) => state.user.dept,
        // district: (state) => state.user.district,
      }),
    },
    watch:{
      form:{
        handler(newForm) {
          this.pages.pageNum = 1;
          this.pages.pageSize = 10;
          this.$refs.mypagination.defaultPages();
          let obj
          // console.log(newForm,'newForm')
          // 如果是苏州市或者是某个区
          if(!this.disabled) {
            if(Array.isArray(newForm.district)){
              if(newForm.district.length == 2) {
                let district = this.form.district[1].slice(0, 11);
                obj = { ...this.pages, ...this.form,district };
              } else if(newForm.district.length == 1) {
                let district = this.form.district[0].slice(0, 8);
                obj = { ...this.pages, ...this.form,district };
              } else {
                obj = { ...this.pages, ...this.form};
              }
              // 不是数组
            } else {
              // 判断是区县
              if(this.county){
                let district = this.form.district.slice(0, 8);
                obj = { ...this.pages, ...this.form,district };
              } else {
                let district = this.form.district.slice(0, 11);
                obj = { ...this.pages, ...this.form,district };
              }
            }
          } else {
            // 不是苏州市是乡镇
            // if(this.dept.ancestors.split(',').length == 2) {
            //   this.subdistrict = this.form.subdistrict.slice(1, 8);
            //   obj = { ...this.pages, ...this.form,subdistrict:this.subdistrict };
            // } else if(this.dept.ancestors.split(',').length == 3){
              // let district = this.form.district.slice(1, 11);
              let district = this.form.district;
              obj = { ...this.pages, ...this.form,district };
            // }
          }
          // console.log(obj,'obj')
          this.getList(obj)
        },
        deep:true,
      }
    },
    methods:{
      // 匹配行政区划
      componendDistrict(district){
        // console.log(district)
        // console.log(this.myDistrict)
        let name 
        this.myDistrict.map((item)=>{
          if(item.county == district) {
            name = item.institutionName
          } else {
            item.children.map((itemTwo)=>{
              if(itemTwo.subdistrict == district) {
                name = item.institutionName + '-' + itemTwo.institutionName
              }
            })
          }
        })
        return name
      },
      // 页码，当前页切换事件
      pagesChange(pages){
        // console.log('123123页码发生变化')
        this.pages = pages
        let district
        if(this.dept.ancestors.split(',').length == 2){
          if(Array.isArray(this.form.district)) {
            if(this.form.district.length == 2) {
              district = this.form.district[1].slice(0, 11);
            }
          } else {
            district = this.form.district.slice(0, 8);
          }
        } else if(this.dept.ancestors.split(',').length == 3){
          district = this.form.district[1].slice(0, 11);
        } else {
          if(this.form.district.length == 2) {
            district = this.form.district[1].slice(0, 11);
          } else if(this.form.district.length == 1){
            district = this.form.district[0].slice(0, 8);
          }
        }
        this.getList({...pages,...this.form,district:district})
      },
      // 选中的数据
      handleSelectionChange(val) {
        // 选中的数组
        this.multipleSelection = val;
      },
      // 导出
      exportFile(){
        // let arr = []
        // this.multipleSelection.map((item)=>{
        //   arr.push(item.enterpriseId)
        // })
        // this.download(
        //   "/pharmaceuticals/bPlanEnterprise/exportplan",
        //   {
        //     enterpriseId:arr.join(',')
        //   },
        //   `计划管理.xlsx`
        // );
        //  this.multipleSelection.length > 0 ? `确认导出选中的数据吗？` : 
        this.$confirm(`确认根据您的搜索条件导出搜索之后的数据吗?`, "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          let district
          if(Array.isArray(this.form.district)){
            if(this.form.district.length == 1){
              district = this.form.district[0];
            } else if(this.form.district.length == 2){
              district = this.form.district[1];
            }
          } else {
            district = this.form.district;
          }
          this.download(
            "/pharmaceuticals/bPlanEnterprise/exportplan",
            {
              ...this.form,
              district
            },
            `计划管理.xlsx`
          );
        })
        .catch(() => {});
      },
      // 新增计划
      newProject(){
        this.$refs.myDialog.open('newlyIncreased',this.dept)
      },
      // 重置筛选
      reset(){
        // this.pages.pageNum = 1;
        // this.pages.pageSize = 10;
        // this.$refs.mypagination.defaultPages();
        // let obj
        if(this.dept.ancestors.split(',').length == 1) {
          this.form = {
            plannedYear:'',
            district:''
          }
          // obj = this.pages
        } else {
          let { district} = this.form;
          this.form.plannedYear='';
          this.form.district=this.mydistrict;
          // obj = {...this.pages,district}
        }
        // this.getList(obj)
      },
      // 修改成功后关闭dialog事件触发
      padlock(){
        // let { district} = this.form;
        this.pages.pageNum = 1;
        this.pages.pageSize = 10;
        this.$refs.mypagination.defaultPages();
        let obj
        if(!this.disabled) {
          if(Array.isArray(this.form.district)){
            if(this.form.district.length == 2) {
              let district = this.form.district[1].slice(0, 11);
              obj = { ...this.pages, ...this.form,district };
            } else if(this.form.district.length == 1) {
              let district = this.form.district[0].slice(0, 8);
              obj = { ...this.pages, ...this.form,district };
            } else {
              obj = { ...this.pages, ...this.form};
            }
            // 不是数组
          } else {
            // 判断是区县
            if(this.county){
              let district = this.form.district.slice(0, 8);
              obj = { ...this.pages, ...this.form,district };
            } else {
              let district = this.form.district.slice(0, 11);
              obj = { ...this.pages, ...this.form,district };
            }
          }
        } else {
          // 不是苏州市是乡镇
          let district = this.form.district;
          obj = { ...this.pages, ...this.form,district };
        }
        // console.log('obj',obj)
        this.getList(obj);
      },
      padlockOne(){
        this.pages.pageNum = 1;
        this.pages.pageSize = 10;
        this.$refs.mypagination.defaultPages();
        if(this.dept.ancestors.split(',').length == 1) {
          if(this.form.district.length == 0 || this.form.plannedYear == '') {
            this.getList(this.pages);
          } else {
            this.form.district = [];
            this.form.plannedYear = '';
          }
        } else {
          if(this.form.district == this.mydistrict || this.form.plannedYear == '') {
            this.getList(this.pages);
          } else {
            this.form.district = this.mydistrict
            this.form.plannedYear = ''
          }
        }
        // this.getList(this.pages);
      },
      // 区划级联选择器选择触发
      handleChange(value){
        // console.log(value)
        // if(value.length == 2) {
        //   this.form.district = value[1]
        // } else {
        //   this.form.district = value[0]
        // }
      },
      // 查看
      look(row){
        this.$refs.tempDialog.open(row,2);
      },
      // 修改
      change(row){
        this.$refs.tempDialog.open(row,1)
      },
      // 删除
      deleteItem(row){
        let	idList = [];
        idList.push(row.id)
        this.$modal.confirm("你确认要删除该条数据吗？", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(async() => {
          this.loading = true;
          try {
            let downloadLoadingInstance = Loading.service({
              text: "正在删除计划，请稍候",
              spinner: "el-icon-loading",
              background: "rgba(0, 0, 0, 0.7)",
            });
            let data = await this.$api.yingji.bPlanEnterprise('delete',_,{idList:idList.join(',')})
            if(data.code == 200) {
              this.loading = false;
              downloadLoadingInstance.close();
              this.$message({
                message: '删除成功',
                type: 'success'
              });
              // let { district} = this.form;
              let district 
              if(this.dept.ancestors.split(',').length == 2) {
                district = this.form.district.slice(0, 8);
              } else if(this.dept.ancestors.split(',').length == 3) {
                district = this.form.district;
              } else {
                if(this.form.district.length == 1) {
                  district = this.form.district.slice(0, 8);
                } else {
                  district = this.form.district;
                }
              }
              let obj = {...this.pages,district}
              this.getList(obj);
            }
          } catch (error) {
            downloadLoadingInstance.close();
            console.log(error)
          }
        })
        .catch(() => {
          console.log('取消删除')
        });
      },
      tableRowClassName({row, rowIndex}) {
        if (rowIndex % 2 !== 0) {
          return 'success-row';
        }
        return '';
      },
      // 获取list表
      async getList(pages){
        this.loading = true;
        let res = await this.$api.yingji.planmx(pages);
        if(res.code == 200) {
          this.loading = false;
          this.tableData = res.data.list;
          this.total = res.data.total
        }
      },
      commentData(item,dept){
        // if(dept.ancestors.split(',').length == 3) {
          item.map((item)=>{
            // if(item.children instanceof Array && item.children.length == 0){
            //   item.county = item.subdistrict
            //   delete item.children;
            // } else {
            //   this.commentData(item.children,this.dept)
            // }
            item.children.map((itemTwo)=>{
              if(itemTwo.children.length == 0){
                delete itemTwo.children;
              }
              itemTwo.county = itemTwo.subdistrict
            })
          })
        // }
        this.options = item;
        if(dept.ancestors.split(',').length == 2) {
          this.options = this.options.filter((item)=>{
            return item.institutionName == dept.deptName
          })
          this.options.map((item)=>{
            if(item.institutionName == dept.deptName) {
              // console.log(item.county)
              this.form.district = item.county;
              this.mydistrict = item.county;
              this.county = true;
              // delete item.children
            }
          })
          // this.disabled = true;
        } else if(dept.ancestors.split(',').length == 3) {
          // console.log(this.options)
          this.options.map((item)=>{
            // console.log(item.children,'item.children')
            item.children.map((itemTwo)=>{
              if(itemTwo.institutionName == dept.deptName) {
                this.form.district = itemTwo.subdistrict;
                // this.form.district.push(item.county,itemTwo.subdistrict);
                // console.log(this.form.district,'item.institutionName')
                this.mydistrict = itemTwo.subdistrict;
                this.county = false;
                // delete itemTwo.children
              }
            })
          })
          this.disabled = true;
        }
      }
    },
    async created() {
      // console.log(this.dept)
      // console.log(this.myDistrict)
      // this.checkListTwo = this.checkList;
      // this.$nextTick(()=>{
        // let dom = document.getElementsByClassName('tables');
        // let height = dom[0].getBoundingClientRect();
        // let windowHeight = window.innerHeight;
        // let relativeHeight = windowHeight - height.top
        // this.main.height = relativeHeight - 80
        // console.log(this.main.height)
      // })
      let data = await this.$api.yingji.tree();
      this.myDistrict = data.data;
      this.commentData(data.data,this.dept)
      if(this.dept.ancestors.split(',').length == 1) {
        this.getList(this.pages);
      }
    },
  }
  </script>
  <style lang="scss" scoped>
  .plan-management {
    padding: 0 20px;
    background-color: #fff;
    height: 100%;
    .plan-topBox {
      display: flex;
      justify-content: space-between;
      padding: 31px 0 12px 0;
      // background-color: yellow;
      .drup-downs {
        display: flex;
        align-items: center;
        flex: 1;
        .year {
          display: flex;
          align-items: center;
          // margin-right: 200px;
          .year-span {
            font-size: 14px;
            font-family: 'Alibaba PuHuiTi';
            font-weight: 400;
            color: #525966;
            line-height: 40px;
            margin-right: 10px;
          }
        }
        .area {
          display: flex;
          align-items: center;
          // justify-content: center;
          // flex: 1;
          .area-span {
            // font-size: 20px;
            // font-family: 'Alibaba PuHuiTi';
            // font-weight: bold;
            // color: #525966;
            // line-height: 40px;
            margin-left: 40px;
            font-size: 14px;
            font-family: 'Alibaba PuHuiTi';
            font-weight: 400;
            color: #525966;
            line-height: 40px;
            margin-right: 10px;
          }
          ::v-deep .el-cascader {
            background-color: transparent;
            width: 276px;
            .el-input__inner {
              height: 33px;
            }
            .el-input .el-input__suffix .el-input__suffix-inner .el-icon-arrow-down::before {
              content: "";
              background: url(../../assets/images/down2.png) center center no-repeat;
              background-size: cover;
              position: absolute;
              width: 10px;
              height: 7px;
              top: 50%;
              left: 50%;
              transform: translate(-50%, -50%);
            }
          }
        }
        ::v-deep .el-select {
          background-color: transparent;
          width: 276px;
          .el-input__inner {
            height: 33px;
          }
          .el-input .el-input__suffix .el-input__suffix-inner .el-icon-arrow-up {
            display: flex;
            align-items: center;
            justify-content: center;
          }
          // border-radius: 2px;
          .el-input .el-input__suffix .el-input__suffix-inner .el-icon-arrow-up::before {
            content: "";
            background: url(../../assets/images/down2.png) center center no-repeat;
            background-size: cover;
            position: absolute;
            width: 10px;
            height: 7px;
            // top: 50%;
            // left: 50%;
            // transform: translate(-50%, -50%);
            transform: rotate(180deg);
          }
        }
        .select-btn {
          width: 120px;
          height: 34px;
          border-radius: 2px;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 15px;
          font-family: 'Alibaba PuHuiTi';
          font-weight: 500;
          color: #ffffff;
          cursor: pointer;
          background: #FDAB5B;
          margin-left: 20px;
        }
      }
      .btns {
        display: flex;
        align-items: center;
        .export,.newProject {
          display: flex;
          align-items: center;
          justify-content: center;
          padding: 6px 19px;
          cursor: pointer;
          img {
            width: 22px;
            margin-right: 10px;
          }
          span {
            color: #ffffff;
            font-size: 14px;
            font-family: 'Alibaba PuHuiTi';
            font-weight: 400;
          }
        }
        .export {
          background-color: #2378EC;
          margin-right: 30px;
        }
        .newProject {
          background-color: #28B384;
        }
      }
    }
    .plan-bottomBox {
      margin-top: 10px;
      .tables {
        ::v-deep .el-table {
          .success-row {
            background-color: rgba(234, 242, 253, 0.25);
          }
          td {
            height: 42px;
            box-sizing: border-box;
            text-align: center;
            font-size: 14px;
            font-family: 'Alibaba PuHuiTi';
            font-weight: 400;
            color: #525966;
          }
          th {
            height: 45px;
            background-color: #EAF2FD;
            font-size: 15px;
            font-family: 'Alibaba PuHuiTi';
            font-weight: bold;
            color: #525966;
            .el-checkbox {
              display: none;
            }
          }
        }
        ::v-deep .el-table::before {
          height: 0;
        }
        .tabs-btns {
          display: flex;
          align-items: center;
          justify-content: center;
          .look,.change,.delete {
            display: flex;
            align-items: center;
            margin-right: 30px;
            cursor: pointer;
            img {
              width: 20px;
              margin-right: 5px;
            }
            span {
              font-size: 15px;
              font-family: Alibaba PuHuiTi;
              font-weight: 400;
              color: #045FFD;
            }
          }
          .delete {
            margin-right: 0;
            span {
              color: #F71052;
            }
          }
        }
      }
      .pagination {
        margin-top: 40px;
      }
    }
  }
  </style>
  