<template>
  <div class="app-container">
    <el-form size="mini" :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
<!--      <el-form-item label="门店任务组编号" prop="storeGroupCode">-->
<!--        <el-input-->
<!--          v-model="queryParams.storeGroupCode"-->
<!--          placeholder="请输入门店任务组编号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="门店任务名称" prop="storeGroupName">
        <el-input
          v-model="queryParams.storeGroupName"
          placeholder="请输入门店任务组名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item class="treeselectForm" label="组织架构" prop="orgIds">
        <tree @treeselectFn="treeselectFn" :treeval="orgIds"></tree>
      </el-form-item>
      <el-form-item label="门店类型" prop="storeCategorys">
        <el-select size="mini" clearable v-model="storeCategorys" multiple collapse-tags placeholder="请选择门店类型">
          <el-option
            v-for="item in storeTypeOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="门店标签" prop="storeLabels">
        <el-select size="mini"  v-model="storeLabels" multiple collapse-tags placeholder="请选择门店标签" style="width: 178px;">
          <el-option
            v-for="item in labelsOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="城市等级" prop="cityLevels">
        <el-select  v-model="cityLevels" clearable multiple collapse-tags placeholder="请选择城市等级">
          <el-option
            v-for="item in cityLevelOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sfa:storeGroup:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sfa:storeGroup:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sfa:storeGroup:softRemove']"
        >删除</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['sfa:storeGroup:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeGroupList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="门店任务组id" align="center" prop="storeGroupId" />-->
      <el-table-column label="门店任务组编号" align="center" prop="storeGroupCode" />
      <el-table-column label="门店任务组名称" align="center" prop="storeGroupName" :show-overflow-tooltip="true" />
      <el-table-column label="组织架构" align="center" prop="orgNames" :show-overflow-tooltip="true">
      </el-table-column>
      <el-table-column label="门店类型" align="center" prop="storeCategorys" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{storeCategorysFormat(scope.row,scope.column)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="门店标签" align="center" prop="storeLabels" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{storelabelsOptionsFormat(scope.row,scope.column)}}</span>
        </template>
      </el-table-column>

      <el-table-column label="城市等级" align="center" prop="cityLevels" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span>{{cityLevelsFormat(scope.row,scope.column)}}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" :show-overflow-tooltip="true"/>
<!--      <el-table-column label="备注" align="center" prop="remark" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sfa:storeGroup:edit']"
          >编辑</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-view"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['sfa:collectionItem:edit']"-->
<!--          >预览</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sfa:storeGroup:softRemove']"
            class="text-danger"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改门店任务组对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body>
      <el-form ref="form" :model="form" :inline="true" size="mini" :rules="rules" label-width="130px">
        <el-form-item label="门店任务组编号" prop="storeGroupCode" v-show="title!='添加门店任务组'">
          <el-input v-model="form.storeGroupCode" placeholder="请输入门店任务组编号" disabled />
        </el-form-item>
        <el-form-item label="门店任务组名称" prop="storeGroupName">
          <el-input v-model="form.storeGroupName" placeholder="请输入门店任务组名称" />
        </el-form-item>
        <el-form-item label="门店类型" prop="storeCategorys">
          <el-select size="mini"  v-model="form.storeCategorys" multiple collapse-tags placeholder="请选择门店类型" style="width: 178px;">
            <el-option
              v-for="item in storeTypeOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="门店标签" prop="storeLabels">
          <el-select size="mini"  v-model="form.storeLabels" multiple collapse-tags placeholder="请选择门店标签" style="width: 178px;">
            <el-option
              v-for="item in labelsOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="城市等级" prop="cityLevels">
          <el-select size="mini"  v-model="form.cityLevels" multiple collapse-tags placeholder="请选择城市等级" style="width: 178px;">
            <el-option
              v-for="item in cityLevelOptions"
              :key="item.dictValue"
              :label="item.dictLabel"
              :value="item.dictValue">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="treeselectForm" label="组织架构" prop="orgIds">
          <tree @treeselectFn="treeselectUpdateFn" :treeval="form.orgIds" style="width: 178px;"></tree>
        </el-form-item>
        <el-form-item label="是否项目"  prop="projectVisit" style="height: 37px;">
          <el-radio-group size="mini" v-model="form.projectVisit" >
            <el-radio label="1" >是</el-radio>
            <el-radio label="0" >否</el-radio>
          </el-radio-group>
        </el-form-item>
<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" placeholder="请输入备注" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="删除标记" prop="delFlag">-->
<!--          <el-input v-model="form.delFlag" placeholder="请输入删除标记" />-->
<!--        </el-form-item>-->
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <div class="info">任务明细</div>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="addRW"
            v-hasPermi="['sfa:storeGroup:add']"
          >添加</el-button>
        </el-col>
      </el-row>
      <el-table v-loading="loadingRW" :data="form.sfaTaskList">
        <el-table-column label="任务编号" align="center" prop="taskCode" />
        <el-table-column label="任务名称" align="center" prop="taskName" />
        <el-table-column label="是否必填" align="center" >
          <template slot-scope="scope">
            <span>{{isMustFormat(scope.row,scope.column)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="显示周期" align="center" >
          <template slot-scope="scope">
            <span>{{displayCycleFormat(scope.row,scope.column)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否有效" align="center" >
          <template slot-scope="scope">
            <span>{{isValidFormat(scope.row,scope.column)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="taskDelete(scope.row)"
              class="text-danger"
            >删除</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="previewFn(scope.row)"
            >预览</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-top"
              @click="moveUpDown(scope.row,scope.$index,'u')"
              :disabled="scope.$index == 0"
            >上移</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-bottom"
              @click="moveUpDown(scope.row,scope.$index,'d')"
              :disabled="scope.$index == form.sfaTaskList.length-1"
            >下移</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row :gutter="10" class="mt10">
        <el-col :span="1.5">
          <div class="info">任务共：{{form.sfaTaskList.length}}</div>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open=false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 添加任务明细对话框 -->
    <el-dialog title="选择任务" :visible.sync="taskFormVisiable" width="90%" append-to-body>
      <el-form ref="taskForm" :model="taskForm" :inline="true" v-show="showSearchTask" size="mini" label-width="130px">
        <el-form-item label="任务编号" prop="taskCode">
          <el-input v-model="taskForm.taskCode" placeholder="请输入任务编号" clearable />
        </el-form-item>
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="taskForm.taskName" placeholder="请输入任务名称" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="taskQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetTaskQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            :disabled="multipleTask"
            @click="addTaskFn"
          >批量添加</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearchTask" @queryTable="taskQuery"></right-toolbar>
      </el-row>
      <el-table v-loading="loadingTask" :data="taskList" @selection-change="handleSelectionChangeTask">
        <el-table-column type="selection" width="55" align="center" />
<!--        <el-table-column label="任务编号" align="center" prop="storeGroupCode" />-->
        <el-table-column label="任务名称" align="center" prop="taskName" />
        <el-table-column label="是否必填" align="center" prop="isMust" >
          <template slot-scope="scope">
            <span>{{isMustFormat(scope.row,scope.column)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="显示周期" align="center" prop="storeCategorys" >
          <template slot-scope="scope">
            <span>{{displayCycleFormat(scope.row,scope.column)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="是否有效" align="center" prop="cityLevels" >
          <template slot-scope="scope">
            <span>{{isValidFormat(scope.row,scope.column)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-plus"
              @click="addTaskFn(scope.row)"
            >添加</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="previewFn(scope.row)"
            >预览</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="totalTask>0"
        :total="totalTask"
        :page.sync="taskForm.pageNum"
        :limit.sync="taskForm.pageSize"
        @pagination="getListTask"
      />
<!--      <div slot="footer" class="dialog-footer">-->
<!--        <el-button type="primary" @click="taskFormVisiable=false">确 定</el-button>-->
<!--        <el-button @click="taskFormVisiable=false">取 消</el-button>-->
<!--      </div>-->
    </el-dialog>
    <!-- 预览 -->
    <el-dialog title="预览" :visible.sync="preview" width="60%" append-to-body>
      <el-form class="previewFormItem" :model="item" label-width="110px" v-for="(item,index) in previewForm" :key="index">
        <el-form-item>
          <div>
            {{item.collectionName}}
            <icon :answerType="item.answerType"></icon>
          </div>
        </el-form-item>
        <el-form-item label="" v-show="item.answerType == 'dx'">
          <el-radio-group v-model="item.values">
            <el-radio :label="itemc.collectionOptionId" v-for="itemc in item.collectionOptionList" :key="itemc.collectionOptionId">{{itemc.collectionOption}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <div v-show="item.answerType == 'dx'">
          <div v-for="childItem  in item.collectionOptionList" style="padding-left: 40px">
            <div v-show="childItem.collectionOptionId == item.values">
              <div v-for="itemT  in childItem.collectionItems">
                <el-form-item>
                  <div>
                    <span class="font-red" v-if="itemT.isMust == 0">*</span>{{itemT.collectionName}}
                    <icon :answerType="itemT.answerType"></icon>
                  </div>
                </el-form-item>
                <el-form-item label="" v-show="itemT.answerType == 'dx'">
                  <!--              <radio :radioValF="itemT.answer" :radioOptions="itemT.collectionOptionList"></radio>-->
                  <el-radio-group v-model="itemT.values">
                    <el-radio :label="itemTS.collectionOptionId" v-for="itemTS in itemT.collectionOptionList" :key="itemTS.collectionOptionId">{{itemTS.collectionOption}}</el-radio>
                  </el-radio-group>
                </el-form-item>
                <div v-show="itemT.answerType == 'dx'">
                  <div v-for="childItemT  in itemT.collectionOptionList" style="padding-left: 80px">
                    <div v-show="childItemT.collectionOptionId == itemT.values">
                      <div v-for="itemTT  in childItemT.collectionItems">
                        <el-form-item>
                          <div>
                            <span class="font-red" v-if="itemTT.isMust == 0">*</span>{{itemTT.collectionName}}
                            <icon :answerType="itemTT.answerType"></icon>
                          </div>
                        </el-form-item>
                        <el-form-item label="" v-show="itemTT.answerType == 'dx'">
                          <radio :radioValF="itemTT.answer" :radioOptions="itemTT.collectionOptionList"></radio>
                        </el-form-item>
                        <el-form-item v-show="itemTT.answerType == 'duox'">
                          <checkbox :checkboxValF="itemTT.answer" :checkboxOptions="itemTT.collectionOptionList"></checkbox>
                        </el-form-item>
                        <el-form-item class="mobile-input" v-if="itemTT.answerType == 'wb' || itemTT.answerType == 'sz'||itemTT.answerType == 'tel'||itemTT.answerType == 'date'">
                          <div v-if="itemTT.collectionOptionList.length>0">
                            <el-input size="mini" disabled v-model="itemTT.collectionOptionList[0].collectionOption"
                                      type="textarea" :rows="10"  resize="none"/>
                          </div>
                        </el-form-item>
                      </div>
                    </div>
                  </div>
                </div>
                <el-form-item v-show="itemT.answerType == 'duox'">
                  <checkbox :checkboxValF="itemT.answer" :checkboxOptions="itemT.collectionOptionList"></checkbox>
                </el-form-item>
                <el-form-item class="mobile-input" v-if="itemT.answerType == 'wb' || itemT.answerType == 'sz'||itemT.answerType == 'tel'||itemT.answerType == 'date'">
                  <div v-if="itemT.collectionOptionList.length>0">
                    <el-input size="mini" disabled v-model="itemT.collectionOptionList[0].collectionOption"
                              type="textarea" :rows="10"  resize="none"/>
                  </div>
                </el-form-item>
              </div>
            </div>
          </div>
        </div>
        <el-form-item v-show="item.answerType == 'duox'">
          <checkbox :checkboxValF="item.answer" :checkboxOptions="item.collectionOptionList"></checkbox>
        </el-form-item>
        <el-form-item v-if="item.answerType == 'wb' || item.answerType == 'sz'">
          <div v-if="item.collectionOptionList.length>0">{{item.collectionOptionList[0].collectionOption}}</div>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import tree from "@/views/components/sfa/treeselectParent2";
import { listStoreGroup, getStoreGroup, delStoreGroup, addStoreGroup, updateStoreGroup,storeGroupSoftRemove,removeStoreGroupTask } from "@/api/sfa/storeGroup";
import { listTask,getTask } from "@/api/sfa/task";
import radio from "@/views/components/sfa/radio";
import checkbox from "@/views/components/sfa/checkbox";
import icon from "@/views/components/sfa/icon";
export default {
  name: "StoreGroup",
  components: { tree,radio,checkbox,icon },
  data() {
    return {
      // 遮罩层
      loading: true,
      loadingTask: true,
        loadingRW: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
        multipleTask:true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      totalTask: 0,
      // 门店任务组表格数据
      storeGroupList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      preview: false,
      taskFormVisiable: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        storeGroupCode: null,
        storeGroupName: null,
        storeCategorys: null,
        cityLevels: null,
        storeLabels:null
      },
      // 表单参数
      form: {
          sfaTaskList:[],
      },
      // 表单校验
      rules: {
          storeGroupName: [ { required: true, message: '请输入门店任务名称', trigger: 'blur' }],
          storeCategorys: [ { required: true, message: '请选择门店类型', trigger: 'blur' }],
          // storeLabels: [ { required: true, message: '请选择门店类型', trigger: 'blur' }],
          cityLevels: [ { required: true, message: '请选择城市等级', trigger: 'blur' }],
          orgIds: [ { required: true, message: '请选择组织架构', trigger: 'blur' }],
      },
      storeTypeOptions:[],
      cityLevelOptions:[],
        isNotOptions:[],
        displayCycleOptions:[],
      taskForm:{
          pageNum: 1,
          pageSize: 10,
      },
        cityLevels:[],
      storeLabels:[],
        storeCategorys:[],
        orgIds:[],
        taskList:[],
        showSearchTask:true,
        taskArr:[],
        taskIds:[],
        previewForm:[],
        taskAddList:[],
      labelsOptions:[]
    };
  },
  created() {
      // 门店类型
      if(this.$store.state.user.type == '2'){
          this.getDicts("ysl_store_type").then(response => {
              this.storeTypeOptions = response.data;
          });
      }else{
          this.getDicts("sfa_store_type").then(response => {
              this.storeTypeOptions = response.data;
          });
      }
      // 城市等级
      this.getDicts("sfa_city_level").then(response => {
          this.cityLevelOptions = response.data;
      });
      // 是否
      this.getDicts("is_not").then(response => {
          this.isNotOptions = response.data;
      });
      // 显示周期
      this.getDicts("sfa_task_display_cycle").then(response => {
          this.displayCycleOptions = response.data;
      });
    //  标签
    this.getDicts("store_labels").then(response => {
      this.labelsOptions = response.data;
    });
    this.getList();

  },
  methods: {
    /** 查询门店任务组列表 */
    getList() {
      this.loading = true;
      this.queryParams.cityLevels = this.cityLevels.join(',');
      this.queryParams.storeLabels=this.storeLabels.join(',');
      this.queryParams.storeCategorys = this.storeCategorys.join(',');
      this.queryParams.orgIds = this.orgIds.join(',');
      listStoreGroup(this.queryParams).then(response => {
        this.storeGroupList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
      // 门店类型
      storeCategorysFormat(row, column){
          return this.selectDictLabels(this.storeTypeOptions, row.storeCategorys);
      },
      // 城市等级
      cityLevelsFormat(row, column){
          return this.selectDictLabels(this.cityLevelOptions, row.cityLevels);
      },
    storelabelsOptionsFormat(row, column){
      if(row.storeLabels!=null){
      return this.selectDictLabels(this.labelsOptions, row.storeLabels);
      }
    },
      // 是否有效中文转换
      isValidFormat(row, column){
          return this.selectDictLabel(this.isNotOptions, row.isValid);
      },
      // 是否必填中文转换
      isMustFormat(row, column){
        if(row.isMust==1){
          return "否"
        }else{
          return  "是"
        }
      },
      // 任务周期
      displayCycleFormat(row, column){
          return this.selectDictLabel(this.displayCycleOptions, row.displayCycle);
      },
    // 表单重置
    reset() {
      this.form = {
        storeGroupId: null,
        storeGroupCode: null,
        storeGroupName: null,
        storeCategorys: null,
        storeLabels:null,
        cityLevels: null,
        orgIds: null,
        remark: null,
        delFlag: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.orgIds = [];
      this.cityLevels = [];;
      this.storeCategorys = [];
      this.storeLabels=[]
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.storeGroupId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.resetForm("form");
      this.form = {
          cityLevels: [],
          createBy: null,
          createTime: null,
          delFlag: "",
          orgIds: [],
          remark: null,
          searchValue: null,
          sfaTaskList: [],
          storeCategorys: [],
          storeLabels:[],
          storeGroupCode: "",
          storeGroupId: null,
          storeGroupName: "",
        projectVisit:"0",
          updateBy: "",
          updateTime: "",
      };
        this.loadingRW = false;
        this.taskAddList = [];
      this.open = true;
      this.title = "添加门店任务组";
    },
      getStoreGroupData(storeGroupId){
          this.loadingRW = true;
          getStoreGroup(storeGroupId).then(response => {
              response.data.storeCategorys = response.data.storeCategorys.split(',');
              response.data.cityLevels = response.data.cityLevels.split(',');
              if(response.data.storeLabels!=null){
                response.data.storeLabels = response.data.storeLabels.split(',');

              }else{
                response.data.storeLabels = []
              }

              response.data.orgIds = response.data.orgIds.split(',');
              // response.data.orgIds.forEach((item,index)=>{
              //     response.data.orgIds[index] = item-0;
              // })
              // response.data.orgIds = [104, 105, 106, 126, 286];
              this.form = response.data;
              this.loadingRW = false;
              this.open = true;
              this.title = "修改门店任务组";
          });
      },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.resetForm("form");
        this.form = {
            cityLevels: [],
            createBy: null,
            createTime: null,
            delFlag: "",
            orgIds: [],
            remark: null,
            searchValue: null,
            sfaTaskList: [],
            storeCategorys: [],
            storeGroupCode: "",
            storeGroupId: null,
            storeGroupName: "",
            updateBy: "",
            updateTime: "",
        };
        this.taskAddList = [];
      const storeGroupId = row.storeGroupId || this.ids;
      this.getStoreGroupData(storeGroupId);
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
            let obj =  this.form;
            obj.storeCategorys = this.form.storeCategorys.join(',');
            obj.cityLevels = this.form.cityLevels.join(',');
            obj.orgIds = this.form.orgIds.join(',');
          obj.storeLabels=this.form.storeLabels.join(',');
          if (this.form.storeGroupId != null) {
            updateStoreGroup(obj).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStoreGroup(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const storeGroupIds = row.storeGroupId || this.ids.join(',');
      let that = this;
      // this.$modal.confirm('是否确认删除门店任务组编号为"' + storeGroupIds + '"的数据项？').then(function() {
      this.$modal.confirm('是否确认删除选中信息？').then(function() {
        return storeGroupSoftRemove({storeGroupIds:storeGroupIds}).then(response => {
            that.$modal.msgSuccess("删除成功");
            that.getList();
        });
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('sfa/storeGroup/export', {
        ...this.queryParams
      }, `storeGroup_${new Date().getTime()}.xlsx`)
    },
    treeselectFn(val){
      this.orgIds = val;
    },
    treeselectUpdateFn(val){
        this.form.orgIds = val;
    },
      // 获取任务列表
      getListTask(){
          this.taskForm.taskList = this.taskAddList;
          this.loadingTask = true;
          this.taskForm.storeGroupId = this.form.storeGroupId;
          listTask(this.taskForm).then(response => {
              this.taskList = response.rows;
              this.totalTask = response.total;
              this.loadingTask = false;
          });
      },
      // 点击添加任务弹窗显示
    addRW(){
      this.taskFormVisiable = true;
      this.taskForm.pageNum = 1;
      this.getListTask();
    },
      // 任务搜索
    taskQuery(){
        this.taskForm.pageNum = 1;
        this.getListTask();
    },
      // 重置任务form
      resetTaskQuery(){
          this.resetForm("taskForm");
      },
      // 任务列表多选
      handleSelectionChangeTask(selection){
          this.taskIds = selection.map(item => item.taskCode);
          this.taskArr = selection;
          this.multipleTask = !selection.length
      },
      // 点击任务列表中的添加
      addTaskFn(row){
          let that = this;
          let task = [];
          row.taskId ? task.push(row) : task = this.taskArr;
          const taskIds = row.taskCode || this.taskIds;
          let taskList = that.form.sfaTaskList;
          // this.$modal.confirm('是否确认添加任务编号为"' + taskIds + '"的数据项？').then(function() {
          this.$modal.confirm('是否确认添加选中信息？').then(function() {
              let arr = [];
              task.forEach((item,index)=>{
                  // that.taskList.forEach((rw,i)=>{
                  //     if(item.taskCode == rw.taskCode){
                  //         that.taskList.splice(i,1);
                  //     }
                  // })
                  that.taskAddList.push(item.taskId);
                  that.form.sfaTaskList.forEach((rw,ind)=>{
                      rw.taskCode != item.taskCode ? arr.push(false): arr.push(true);
                      //
                  })
                  arr.indexOf(true) == -1 ? that.form.sfaTaskList.push(item) : that.form.sfaTaskList;
              })
          }).then(() => {
              this.getListTask();
              that.$modal.msgSuccess("添加成功");
          }).catch(() => {});
      },
      // 删除任务组中的任务项
      taskDelete(row){
          // if(row.storeGroupId != null){
          //     let obj = {
          //         storeGroupTaskId:row.storeGroupTaskId
          //     }
          //     removeStoreGroupTask(obj).then(response => {
          //         this.$modal.msgSuccess("删除成功");
          //         this.getStoreGroupData(this.form.storeGroupId);
          //     });
          // }else{
              this.form.sfaTaskList.forEach((item,index)=>{
                  if(item.taskId == row.taskId){
                      this.form.sfaTaskList.splice(index,1);
                      // this.$modal.msgSuccess("删除成功");
                  }
              })
          this.taskAddList.forEach((item,index)=>{
              if(item == row.taskId){
                  this.taskAddList.splice(index,1);
              }
          })
          // }
      },
      moveUpDown(row,index,isUPD){
          let obj = {
              taskId:row.taskId,
              upDown:isUPD
          };
          let msg = '';
          if(isUPD == 'u'){
              let upData = this.form.sfaTaskList[index - 1];
              this.form.sfaTaskList.splice(index - 1, 1);
              this.form.sfaTaskList.splice(index, 0, upData);
              msg =  '上移成功';
          }else{
              let downData = this.form.sfaTaskList[index + 1];
              this.form.sfaTaskList.splice(index + 1, 1);
              this.form.sfaTaskList.splice(index, 0, downData);
              msg =  '下移成功';
          }
          this.$modal.msgSuccess(msg);
      },
      previewFn(row){
          this.previewForm = [];
          getTask(row.taskId).then(response => {
              this.previewForm = response.data.collectionItemList;
              this.preview = true;
          });
      },
  }
};
</script>
<style>
  .treeselectForm .el-form-item__content{
    width: 178px;
  }
</style>
