text: import { parse } from 'qs';
  import { 
    getTableArea, loadTablesByAreaId,
    addTable, updateTable, deleteTable,
  
    addArea, updateArea, deleteArea,
  } from '../services/setting';
  
  export default {
    namespace: 'setting',
  
    state: {
      tableArea: [{ areaName: 'area1', id: 1 }, { areaName: 'area2', id: 2 }],
      selectedTableAreaId: null,
      tables: [{ tableName: '1', tableNum: 1 }, { tableName: '2', tableNum: 2 }],
    },
  
    effects: {
      *getTableArea(_, { call, put, select }) {
        const selectedTableAreaId = yield select(state => state.setting.selectedTableAreaId);
        const response = yield call(getTableArea);
        //   if (!response) {
        //     response = {"data":[{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":5,"isCure":2,"isOrder":1,"name":"肾部保养","parentId":11,"serverCreateTime":1534338859756,"serverUpdateTime":1534338859756,"sort":900000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"f45c07c6-1445-4412-b5af-f610bd2f461c"},{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":6,"isCure":2,"isOrder":1,"name":"肾部保养","parentId":11,"serverCreateTime":1534341921890,"serverUpdateTime":1534341921890,"sort":800000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"6f9cab11-58ab-4f00-90d0-b5335f221618"},{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":1,"isCure":2,"isOrder":1,"name":"肾部保养","parentId":11,"serverCreateTime":1534168060069,"serverUpdateTime":1534168060069,"sort":700000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"eae49042-0c88-47d8-8d8f-70f4b1d40910"},{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":3,"isCure":2,"isOrder":1,"name":"肾部保养","parentId":11,"serverCreateTime":1534259632792,"serverUpdateTime":1534259632792,"sort":600000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"f9896b55-27fe-407c-8a0a-7298cc4f534b"},{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":7,"isCure":2,"isOrder":1,"name":"肾部保养","parentId":11,"serverCreateTime":1534345162250,"serverUpdateTime":1534345162250,"sort":300000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"213d2c03-c2ff-4cd6-a550-77117757ed9d"},{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":15,"isCure":2,"isOrder":1,"name":"String","parentId":11,"serverCreateTime":1534082241089,"serverUpdateTime":1534429989860,"sort":300000,"statusFlag":1,"typeCode":"ffff","updatorId":0,"updatorName":"","uuid":"b956a567-2f63-4566-8422-fa16b887408f"},{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":2,"isCure":2,"isOrder":1,"name":"肾部保养","parentId":11,"serverCreateTime":1534168678267,"serverUpdateTime":1534168678267,"sort":200000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"81ea563a-e1f6-468c-814e-a27e134877ac"},{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":4,"isCure":2,"isOrder":1,"name":"肾部保养","parentId":11,"serverCreateTime":1534259705102,"serverUpdateTime":1534259705102,"sort":200000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"acbafa01-74ef-4312-a479-ddc3ff69fb0d"}],"dishTypeDesc":"","enabledFlag":1,"id":11,"isCure":2,"isOrder":1,"name":"保健","parentId":0,"serverCreateTime":1534079891779,"serverUpdateTime":1534079891779,"sort":500000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"8bd17894-38df-49bf-9ad2-72c02509cbff"},{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":8,"isCure":2,"isOrder":1,"name":"护肤类","parentId":0,"serverCreateTime":1534077078830,"serverUpdateTime":1534077078830,"sort":300000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"19d76e88-f84b-47f2-a55d-3c52b61e9a58"}],"message":"","messageType":"ignore","statusCode":200}
        //   }
        yield put({
          type: 'updateState',
          payload: {
            tableArea: response.data,
          },
        });
       
        if (!selectedTableAreaId && (response.data[0] || {}).id) {
          yield put({
            type: 'setting/updateState',
            payload: {
              selectedTableAreaId: (response.data[0] || {}).id,
            },
          });
          yield put({
            type: 'setting/loadTablesByAreaId',
            selectedTableAreaId: (response.data[0] || {}).id,
          });
        }
      },
      *loadTablesByAreaId(payload, { call, put, select }) {
        let selectedTableAreaId = null;
        if (payload && payload.payload && payload.payload.selectedTableAreaId) {
          selectedTableAreaId = payload.payload.selectedTableAreaId;
        } else {
          selectedTableAreaId = yield select(state => state.setting.selectedTableAreaId);
        }
        // const tables = yield select(state => state.setting.tables);
        const response = yield call(loadTablesByAreaId, selectedTableAreaId);
      //   if (!response) {
      //     response = {"data":[{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":5,"isCure":2,"isOrder":1,"name":"肾部保养","parentId":11,"serverCreateTime":1534338859756,"serverUpdateTime":1534338859756,"sort":900000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"f45c07c6-1445-4412-b5af-f610bd2f461c"},{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":6,"isCure":2,"isOrder":1,"name":"肾部保养","parentId":11,"serverCreateTime":1534341921890,"serverUpdateTime":1534341921890,"sort":800000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"6f9cab11-58ab-4f00-90d0-b5335f221618"},{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":1,"isCure":2,"isOrder":1,"name":"肾部保养","parentId":11,"serverCreateTime":1534168060069,"serverUpdateTime":1534168060069,"sort":700000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"eae49042-0c88-47d8-8d8f-70f4b1d40910"},{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":3,"isCure":2,"isOrder":1,"name":"肾部保养","parentId":11,"serverCreateTime":1534259632792,"serverUpdateTime":1534259632792,"sort":600000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"f9896b55-27fe-407c-8a0a-7298cc4f534b"},{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":7,"isCure":2,"isOrder":1,"name":"肾部保养","parentId":11,"serverCreateTime":1534345162250,"serverUpdateTime":1534345162250,"sort":300000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"213d2c03-c2ff-4cd6-a550-77117757ed9d"},{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":15,"isCure":2,"isOrder":1,"name":"String","parentId":11,"serverCreateTime":1534082241089,"serverUpdateTime":1534429989860,"sort":300000,"statusFlag":1,"typeCode":"ffff","updatorId":0,"updatorName":"","uuid":"b956a567-2f63-4566-8422-fa16b887408f"},{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":2,"isCure":2,"isOrder":1,"name":"肾部保养","parentId":11,"serverCreateTime":1534168678267,"serverUpdateTime":1534168678267,"sort":200000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"81ea563a-e1f6-468c-814e-a27e134877ac"},{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":4,"isCure":2,"isOrder":1,"name":"肾部保养","parentId":11,"serverCreateTime":1534259705102,"serverUpdateTime":1534259705102,"sort":200000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"acbafa01-74ef-4312-a479-ddc3ff69fb0d"}],"dishTypeDesc":"","enabledFlag":1,"id":11,"isCure":2,"isOrder":1,"name":"保健","parentId":0,"serverCreateTime":1534079891779,"serverUpdateTime":1534079891779,"sort":500000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"8bd17894-38df-49bf-9ad2-72c02509cbff"},{"aliasName":"","brandIdenty":1,"creatorId":0,"creatorName":"","dishBrandTypeBoList":[],"dishTypeDesc":"","enabledFlag":1,"id":8,"isCure":2,"isOrder":1,"name":"护肤类","parentId":0,"serverCreateTime":1534077078830,"serverUpdateTime":1534077078830,"sort":300000,"statusFlag":1,"typeCode":"123456","updatorId":0,"updatorName":"","uuid":"19d76e88-f84b-47f2-a55d-3c52b61e9a58"}],"message":"","messageType":"ignore","statusCode":200}
      //   }
        yield put({
          type: 'setting/updateState',
          payload: {
            tables: JSON.stringify(response.data) === '{}' ? [] : response.data,
          },
        });
      },
      *addTable(_, { call, put, select }) {
        const selectedTableAreaId = yield select(state => state.setting.selectedTableAreaId);
        const tables = yield select(state => state.setting.tables);
        const table = tables.filter(item => !item.id)[0] || {};
        
        const param = {
          areaId: selectedTableAreaId,
          ...table,
        };
        yield call(addTable, param);
        yield put({
          type: 'setting/loadTablesByAreaId',
        });
      },
      *addArea(_, { call, put, select }) {
        // const selectedTableAreaId = yield select(state => state.setting.selectedTableAreaId);
        const tableArea = yield select(state => state.setting.tableArea);
        const area = tableArea.filter(item => !item.id)[0] || {};
        
        const param = {
          ...area,
        };
        yield call(addArea, param);
        yield put({
          type: 'setting/getTableArea',
        });
        // yield put({
        //   type: 'setting/loadTablesByAreaId',
        // });
      },
      *updateTable({ payload: { table } }, { call, put }) {
        yield call(updateTable, table);
        yield put({
          type: 'setting/loadTablesByAreaId',
        });
      },
      *updateArea({ payload: { area } }, { call, put }) {
        yield call(updateArea, area);
        yield put({
          type: 'setting/getTableArea',
        });
        // yield put({
        //   type: 'setting/loadTablesByAreaId',
        // });
      },
      *deleteTable({ payload: { table } }, { call, put, select }) {
        if (!table.id) {
          let tables = yield select(state => state.setting.tables);
          tables = tables.filter(item => item.id);
          yield put({
            type: 'setting/updateState',
            payload: {
              tables,
            },
          });
          return;
        }
        yield call(deleteTable, table.id);
        yield put({
          type: 'setting/loadTablesByAreaId',
        });
      },
      *deleteArea({ payload: { area } }, { call, put, select }) {
        const selectedTableAreaId = yield select(state => state.setting.selectedTableAreaId);
        let tableArea = yield select(state => state.setting.tableArea);
        tableArea = tableArea.filter(item => item.id);
        if (!area.id) {
          yield put({
            type: 'setting/updateState',
            payload: {
              tableArea,
            },
          });
          return;
        }
        yield call(deleteArea, area.id);
        if (area.id == selectedTableAreaId) { // eslint-disable-line
          yield put({
            type: 'setting/updateState',
            payload: {
              selectedTableAreaId: (tableArea[0] || {}).id,
            },
          });
        }
        yield put({
          type: 'setting/getTableArea',
        });
      },
    },
  
    reducers: {
      updateState(state, { payload }) {
        return {
          ...state,
          ...payload,
        };
      },
    },
    subscriptions: {
      setup({ history, dispatch }) {
        // Subscribe history(url) change, trigger `load` action if pathname is `/`
        return history.listen(({ pathname, search }) => {
          if (pathname === '/setting/workspaceSetting') {
            dispatch({
              type: 'setting/getTableArea',
            });
          }
  
          // if (pathname !== '/product-new') {
          //   const searchParam = parse(search, { ignoreQueryPrefix: true });
          //   const result = {};
          //   if (searchParam.selecteDishTypeId) {
          //     dispatch({
          //       type: 'product/updateState',
          //       payload: {
          //         selecteDishTypeId: searchParam.selecteDishTypeId
          //       }
          //     });
          //   }
          // }
          
        });
      },
    },
  };
  